Skip to content

Commit df05942

Browse files
author
David Roberts
authored
[ML] Make logger test more robust to slow VMs (#101)
The non-ASCII logging test could hang indefinitely if the VM it was running on stalled for 200ms before reconfiguring the logger to use the test pipe. This change makes it more robust. A stall of up to 5 seconds is now tolerated, but with checks every 50ms to avoid slowing the test down.
1 parent a6e65b6 commit df05942

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/core/unittest/CLoggerTest.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,18 @@ void CLoggerTest::testNonAsciiJsonLogging() {
150150

151151
std::ostringstream loggedData;
152152
std::thread reader([&loggedData] {
153-
// wait a bit so that pipe has been created
154-
ml::core::CSleep::sleep(200);
155-
std::ifstream strm(TEST_PIPE_NAME);
156-
std::copy(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>(),
157-
std::ostreambuf_iterator<char>(loggedData));
153+
for (std::size_t attempt = 1; attempt <= 100; ++attempt) {
154+
// wait a bit so that pipe has been created
155+
ml::core::CSleep::sleep(50);
156+
std::ifstream strm(TEST_PIPE_NAME);
157+
if (strm.is_open()) {
158+
std::copy(std::istreambuf_iterator<char>(strm),
159+
std::istreambuf_iterator<char>(),
160+
std::ostreambuf_iterator<char>(loggedData));
161+
return;
162+
}
163+
}
164+
CPPUNIT_FAIL("Failed to connect to logging pipe within a reasonable time");
158165
});
159166

160167
ml::core::CLogger& logger = ml::core::CLogger::instance();

0 commit comments

Comments
 (0)