Skip to content

Commit

Permalink
Fix flaky Log tests (#4582)
Browse files Browse the repository at this point in the history
* Refs #20655. Improve HELPER_WaitForEntries.

Signed-off-by: Miguel Company <[email protected]>

* Refs #20655. Fix default_macros_test.

Signed-off-by: Miguel Company <[email protected]>

---------

Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany authored Mar 20, 2024
1 parent 42340ea commit bdef74e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
7 changes: 3 additions & 4 deletions test/unittest/logging/LogTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,12 @@ TEST_F(LogTests, stdouterr_consumer_stream)
std::vector<Log::Entry> LogTests::HELPER_WaitForEntries(
uint32_t amount)
{
size_t entries = 0;
for (uint32_t i = 0; i != AsyncTries; i++)
{
entries = mockConsumer->ConsumedEntries().size();
if (entries == amount)
auto entries = mockConsumer->ConsumedEntries();
if (entries.size() == amount)
{
break;
return entries;
}
this_thread::sleep_for(chrono::milliseconds(AsyncWaitMs));
}
Expand Down
7 changes: 3 additions & 4 deletions test/unittest/logging/log_macros/LogMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ class LogMacrosTests : public ::testing::Test
std::vector<Log::Entry> HELPER_WaitForEntries(
uint32_t amount)
{
size_t entries = 0;
for (uint32_t i = 0; i != AsyncTries; i++)
{
entries = mockConsumer->ConsumedEntries().size();
if (entries == amount)
auto entries = mockConsumer->ConsumedEntries();
if (entries.size() == amount)
{
break;
return entries;
}
this_thread::sleep_for(chrono::milliseconds(AsyncWaitMs));
}
Expand Down
15 changes: 10 additions & 5 deletions test/unittest/logging/log_macros/LogMacrosDefaultTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ TEST_F(LogMacrosTests, default_macros_test)
# endif // Visual Studio specific behavior
#endif // Check default macro values

#if !HAVE_LOG_NO_INFO && \
// Result depends on the log configuration
#if !HAVE_LOG_NO_INFO && \
(defined(FASTDDS_ENFORCE_LOG_INFO) || \
((defined(__INTERNALDEBUG) || defined(_INTERNALDEBUG)) && (defined(_DEBUG) || defined(__DEBUG) || \
!defined(NDEBUG)))
static constexpr unsigned int expected_result = 3;
!defined(NDEBUG))))
constexpr unsigned int expected_result = 3;
#else
static constexpr unsigned int expected_result = 2;
constexpr unsigned int expected_result = 2;
#endif // debug macros check

auto consumedEntries = HELPER_WaitForEntries(expected_result);
// Warning and error should always be logged.
// Info might be logged depending on the log configuration.
// We always wait for 3 entries, though not all of them might be present.
auto consumedEntries = HELPER_WaitForEntries(3u);
ASSERT_EQ(expected_result, consumedEntries.size());
}

Expand Down

0 comments on commit bdef74e

Please sign in to comment.