From 4154079eced0bac4c0b01173e887dd0faea1c52b Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 13 Jun 2024 11:53:15 +0100 Subject: [PATCH] Smart wait for msg to arrive in channel test (#6260) --- src/node/test/channels.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/node/test/channels.cpp b/src/node/test/channels.cpp index b42e3fd51..8c18f1353 100644 --- a/src/node/test/channels.cpp +++ b/src/node/test/channels.cpp @@ -1550,13 +1550,40 @@ TEST_CASE_FIXTURE(IORingbuffersFixture, "Key rotation") std::this_thread::sleep_for(std::chrono::milliseconds(20)); } - // Assume this sleep is long enough for channel threads to fully catch up - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // Wait for channel threads to fully catch up. + constexpr int wait_finish_attempts = 100; + for (int attempt = 0; attempt < wait_finish_attempts; attempt++) + { + bool skip_waiting{false}; + { + std::lock_guard guard(sent_by_1.lock); + skip_waiting |= sent_by_1.to_send.empty(); + } + { + std::lock_guard guard(sent_by_2.lock); + skip_waiting |= sent_by_2.to_send.empty(); + } + if (skip_waiting) + { + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + finished.store(true); channels1.join(); channels2.join(); + { + std::lock_guard guard(sent_by_1.lock); + REQUIRE(sent_by_1.to_send.empty()); + } + { + std::lock_guard guard(sent_by_2.lock); + REQUIRE(sent_by_2.to_send.empty()); + } + // Validate results REQUIRE(received_by_1 == expected_received_by_1); REQUIRE(received_by_2 == expected_received_by_2);