From c6dce9372af432f7bf086c0a74569be06c6e27af Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Thu, 23 Jun 2022 18:25:34 +0200 Subject: [PATCH] iox-#751 replace adaptive_wait with Barrier Signed-off-by: Christian Eltzschig --- .../moduletests/test_posix_signal_watcher.cpp | 8 ++++---- .../integrationtests/test_client_server.cpp | 13 ++++++------ ...est_publisher_subscriber_communication.cpp | 7 ++++--- .../test_popo_chunk_distributor.cpp | 19 +++++++++--------- .../test_popo_condition_variable.cpp | 20 +++++++++---------- .../test/moduletests/test_posh_runtime.cpp | 20 +++++++++---------- .../moduletests/test_roudi_portmanager.cpp | 7 ++++--- 7 files changed, 49 insertions(+), 45 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_posix_signal_watcher.cpp b/iceoryx_hoofs/test/moduletests/test_posix_signal_watcher.cpp index 3a9d2ffc5f..70d6825883 100644 --- a/iceoryx_hoofs/test/moduletests/test_posix_signal_watcher.cpp +++ b/iceoryx_hoofs/test/moduletests/test_posix_signal_watcher.cpp @@ -14,8 +14,8 @@ // // SPDX-License-Identifier: Apache-2.0 -#include "iceoryx_hoofs/internal/cxx/adaptive_wait.hpp" #include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp" +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "test.hpp" #include @@ -85,7 +85,7 @@ void unblocksWhenSignalWasRaisedForWaiters(SignalWatcher_test& test, const uint64_t numberOfWaiters, const std::function& wait) { - std::atomic isThreadStarted{0}; + Barrier isThreadStarted(numberOfWaiters); std::atomic isThreadFinished{0}; std::vector threads; @@ -93,13 +93,13 @@ void unblocksWhenSignalWasRaisedForWaiters(SignalWatcher_test& test, for (uint64_t i = 0; i < numberOfWaiters; ++i) { threads.emplace_back([&] { - ++isThreadStarted; + isThreadStarted.notify(); wait(); ++isThreadFinished; }); } - iox::cxx::internal::adaptive_wait().wait_loop([&] { return (isThreadStarted != numberOfWaiters); }); + isThreadStarted.wait(); std::this_thread::sleep_for(test.waitingTime); diff --git a/iceoryx_posh/test/integrationtests/test_client_server.cpp b/iceoryx_posh/test/integrationtests/test_client_server.cpp index 318bb82bb1..5e9e3a3d27 100644 --- a/iceoryx_posh/test/integrationtests/test_client_server.cpp +++ b/iceoryx_posh/test/integrationtests/test_client_server.cpp @@ -14,6 +14,7 @@ // // SPDX-License-Identifier: Apache-2.0 +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "iceoryx_posh/popo/client.hpp" #include "iceoryx_posh/popo/server.hpp" @@ -348,7 +349,7 @@ TEST_F(ClientServer_test, ServerTakeRequestUnblocksClientSendingRequest) std::atomic_bool wasRequestSent{false}; // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingClient([&] { auto sendRequest = [&]() { auto loanResult = client.loan(); @@ -363,14 +364,14 @@ TEST_F(ClientServer_test, ServerTakeRequestUnblocksClientSendingRequest) } // signal that an blocking send is expected - isThreadStarted = true; + isThreadStarted.notify(); sendRequest(); wasRequestSent = true; }); // wait some time to check if the client is blocked constexpr std::chrono::milliseconds SLEEP_TIME{100U}; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(SLEEP_TIME); EXPECT_THAT(wasRequestSent.load(), Eq(false)); @@ -409,7 +410,7 @@ TEST_F(ClientServer_test, ClientTakesResponseUnblocksServerSendingResponse) std::atomic_bool wasResponseSent{false}; // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingServer([&] { auto processRequest = [&]() { auto takeResult = server.take(); @@ -424,14 +425,14 @@ TEST_F(ClientServer_test, ClientTakesResponseUnblocksServerSendingResponse) processRequest(); } - isThreadStarted = true; + isThreadStarted.notify(); processRequest(); wasResponseSent = true; }); // wait some time to check if the server is blocked constexpr std::chrono::milliseconds SLEEP_TIME{100U}; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(SLEEP_TIME); EXPECT_THAT(wasResponseSent.load(), Eq(false)); diff --git a/iceoryx_posh/test/integrationtests/test_publisher_subscriber_communication.cpp b/iceoryx_posh/test/integrationtests/test_publisher_subscriber_communication.cpp index d07ee37da4..9176e1e742 100644 --- a/iceoryx_posh/test/integrationtests/test_publisher_subscriber_communication.cpp +++ b/iceoryx_posh/test/integrationtests/test_publisher_subscriber_communication.cpp @@ -21,6 +21,7 @@ #include "iceoryx_hoofs/cxx/string.hpp" #include "iceoryx_hoofs/cxx/variant.hpp" #include "iceoryx_hoofs/cxx/vector.hpp" +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "iceoryx_posh/popo/publisher.hpp" #include "iceoryx_posh/popo/subscriber.hpp" @@ -551,16 +552,16 @@ TEST_F(PublisherSubscriberCommunication_test, PublisherBlocksWhenBlockingActivat EXPECT_FALSE(publisher->publishCopyOf("and hypnotoad will smile back").has_error()); std::atomic_bool wasSampleDelivered{false}; - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread t1([&] { - isThreadStarted = true; + isThreadStarted.notify(); EXPECT_FALSE(publisher->publishCopyOf("oh no hypnotoad is staring at me").has_error()); wasSampleDelivered.store(true); }); constexpr int64_t TIMEOUT_IN_MS = 100; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT_IN_MS)); EXPECT_FALSE(wasSampleDelivered.load()); diff --git a/iceoryx_posh/test/moduletests/test_popo_chunk_distributor.cpp b/iceoryx_posh/test/moduletests/test_popo_chunk_distributor.cpp index 164eb82e6c..5652d47a91 100644 --- a/iceoryx_posh/test/moduletests/test_popo_chunk_distributor.cpp +++ b/iceoryx_posh/test/moduletests/test_popo_chunk_distributor.cpp @@ -16,6 +16,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "iceoryx_hoofs/cxx/variant_queue.hpp" +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "iceoryx_posh/internal/mepoo/shared_chunk.hpp" #include "iceoryx_posh/internal/popo/building_blocks/chunk_distributor.hpp" @@ -620,16 +621,16 @@ TYPED_TEST(ChunkDistributor_test, DeliverToQueueWithBlockingOptionBlocksDelivery ASSERT_FALSE(sut.deliverToQueue(queueData->m_uniqueId, EXPECTED_QUEUE_INDEX, chunk).has_error()); } - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); auto chunk = this->allocateChunk(7373); std::atomic_bool wasChunkDelivered{false}; std::thread t1([&] { - isThreadStarted = true; + isThreadStarted.notify(); ASSERT_FALSE(sut.deliverToQueue(queueData->m_uniqueId, EXPECTED_QUEUE_INDEX, chunk).has_error()); wasChunkDelivered = true; }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); std::this_thread::sleep_for(this->BLOCKING_DURATION); EXPECT_THAT(wasChunkDelivered.load(), Eq(false)); @@ -738,15 +739,15 @@ TYPED_TEST(ChunkDistributor_test, DeliverToSingleQueueBlocksWhenOptionsAreSetToB ASSERT_FALSE(sut.tryAddQueue(queueData.get(), 0U).has_error()); sut.deliverToAllStoredQueues(this->allocateChunk(155U)); - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::atomic_bool wasChunkDelivered{false}; std::thread t1([&] { - isThreadStarted = true; + isThreadStarted.notify(); sut.deliverToAllStoredQueues(this->allocateChunk(152U)); wasChunkDelivered = true; }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); std::this_thread::sleep_for(this->BLOCKING_DURATION); EXPECT_THAT(wasChunkDelivered.load(), Eq(false)); @@ -785,15 +786,15 @@ TYPED_TEST(ChunkDistributor_test, MultipleBlockingQueuesWillBeFilledWhenThereBec sut.deliverToAllStoredQueues(this->allocateChunk(425U)); - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::atomic_bool wasChunkDelivered{false}; std::thread t1([&] { - isThreadStarted.store(true); + isThreadStarted.notify(); sut.deliverToAllStoredQueues(this->allocateChunk(1152U)); wasChunkDelivered = true; }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); std::this_thread::sleep_for(this->BLOCKING_DURATION); EXPECT_THAT(wasChunkDelivered.load(), Eq(false)); diff --git a/iceoryx_posh/test/moduletests/test_popo_condition_variable.cpp b/iceoryx_posh/test/moduletests/test_popo_condition_variable.cpp index b0c1dc065c..32b3970195 100644 --- a/iceoryx_posh/test/moduletests/test_popo_condition_variable.cpp +++ b/iceoryx_posh/test/moduletests/test_popo_condition_variable.cpp @@ -15,7 +15,7 @@ // // SPDX-License-Identifier: Apache-2.0 -#include "iceoryx_hoofs/internal/cxx/adaptive_wait.hpp" +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/timing_test.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "iceoryx_posh/internal/popo/building_blocks/condition_listener.hpp" @@ -117,14 +117,14 @@ TEST_F(ConditionVariable_test, WaitAndNotifyResultsInImmediateTriggerMultiThread { ::testing::Test::RecordProperty("TEST_ID", "39b40c73-3dcc-4af6-9682-b62816c69854"); std::atomic counter{0}; - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread waiter([&] { EXPECT_THAT(counter, Eq(0)); - isThreadStarted = true; + isThreadStarted.notify(); m_waiter.wait(); EXPECT_THAT(counter, Eq(1)); }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); counter++; m_signaler.notify(); @@ -366,18 +366,18 @@ TIMING_TEST_F(ConditionVariable_test, WaitBlocks, Repeat(5), [&] { ConditionNotifier notifier(m_condVarData, EVENT_INDEX); ConditionListener listener(m_condVarData); NotificationVector_t activeNotifications; - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::atomic_bool hasWaited{false}; std::thread waiter([&] { - isThreadStarted = true; + isThreadStarted.notify(); activeNotifications = listener.wait(); hasWaited.store(true, std::memory_order_relaxed); ASSERT_THAT(activeNotifications.size(), Eq(1U)); EXPECT_THAT(activeNotifications[0], Eq(EVENT_INDEX)); }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); EXPECT_THAT(hasWaited, Eq(false)); @@ -409,9 +409,9 @@ TIMING_TEST_F(ConditionVariable_test, SecondWaitBlocksUntilNewNotification, Repe Watchdog watchdogSecondWait(m_timeToWait); watchdogSecondWait.watchAndActOnFailure([&] { listener.destroy(); }); - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread waiter([&] { - isThreadStarted = true; + isThreadStarted.notify(); activeNotifications = listener.wait(); hasWaited.store(true, std::memory_order_relaxed); ASSERT_THAT(activeNotifications.size(), Eq(1U)); @@ -422,7 +422,7 @@ TIMING_TEST_F(ConditionVariable_test, SecondWaitBlocksUntilNewNotification, Repe } }); - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted; }); + isThreadStarted.wait(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); EXPECT_THAT(hasWaited, Eq(false)); diff --git a/iceoryx_posh/test/moduletests/test_posh_runtime.cpp b/iceoryx_posh/test/moduletests/test_posh_runtime.cpp index 684ad92089..2c92a0cc03 100644 --- a/iceoryx_posh/test/moduletests/test_posh_runtime.cpp +++ b/iceoryx_posh/test/moduletests/test_posh_runtime.cpp @@ -16,7 +16,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "iceoryx_hoofs/cxx/convert.hpp" -#include "iceoryx_hoofs/internal/cxx/adaptive_wait.hpp" +#include "iceoryx_hoofs/testing/barrier.hpp" #include "iceoryx_hoofs/testing/timing_test.hpp" #include "iceoryx_hoofs/testing/watch_dog.hpp" #include "iceoryx_posh/iceoryx_posh_types.hpp" @@ -957,15 +957,15 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingPublisher) deadlockWatchdog.watchAndActOnFailure([] { std::terminate(); }); // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingPublisher([&] { - isThreadStarted = true; + isThreadStarted.notify(); ASSERT_FALSE(publisher.publishCopyOf(42U).has_error()); wasSampleSent = true; }); // wait some time to check if the publisher is blocked - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); constexpr std::chrono::milliseconds SLEEP_TIME{100U}; std::this_thread::sleep_for(SLEEP_TIME); EXPECT_THAT(wasSampleSent.load(), Eq(false)); @@ -1005,7 +1005,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingClient) deadlockWatchdog.watchAndActOnFailure([] { std::terminate(); }); // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingClient([&] { auto sendRequest = [&](bool expectError) { auto clientLoanResult = client.loan(sizeof(uint64_t), alignof(uint64_t)); @@ -1026,7 +1026,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingClient) } // signal that an blocking send is expected - isThreadStarted = true; + isThreadStarted.notify(); constexpr bool EXPECT_ERROR_INDICATOR{true}; sendRequest(EXPECT_ERROR_INDICATOR); wasRequestSent = true; @@ -1034,7 +1034,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingClient) // wait some time to check if the client is blocked constexpr std::chrono::milliseconds SLEEP_TIME{100U}; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(SLEEP_TIME); EXPECT_THAT(wasRequestSent.load(), Eq(false)); @@ -1081,7 +1081,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingServer) deadlockWatchdog.watchAndActOnFailure([] { std::terminate(); }); // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingServer([&] { auto processRequest = [&](bool expectError) { auto takeResult = server.take(); @@ -1103,7 +1103,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingServer) processRequest(EXPECT_ERROR_INDICATOR); } - isThreadStarted = true; + isThreadStarted.notify(); constexpr bool EXPECT_ERROR_INDICATOR{true}; processRequest(EXPECT_ERROR_INDICATOR); wasResponseSent = true; @@ -1111,7 +1111,7 @@ TEST_F(PoshRuntime_test, ShutdownUnblocksBlockingServer) // wait some time to check if the server is blocked constexpr std::chrono::milliseconds SLEEP_TIME{100U}; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(SLEEP_TIME); EXPECT_THAT(wasResponseSent.load(), Eq(false)); diff --git a/iceoryx_posh/test/moduletests/test_roudi_portmanager.cpp b/iceoryx_posh/test/moduletests/test_roudi_portmanager.cpp index 0f99b35f4f..2982cfcfe4 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_portmanager.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_portmanager.cpp @@ -15,6 +15,7 @@ // // SPDX-License-Identifier: Apache-2.0 +#include "iceoryx_hoofs/testing/barrier.hpp" #include "test_roudi_portmanager_fixture.hpp" namespace iox_test_roudi_portmanager @@ -842,18 +843,18 @@ void PortManager_test::setupAndTestBlockingPublisher(const iox::RuntimeName_t& p deadlockWatchdog.watchAndActOnFailure([] { std::terminate(); }); // block in a separate thread - std::atomic_bool isThreadStarted{false}; + Barrier isThreadStarted(1U); std::thread blockingPublisher([&] { auto maybeChunk = publisher.tryAllocateChunk(42U, 8U); ASSERT_FALSE(maybeChunk.has_error()); - isThreadStarted = true; + isThreadStarted.notify(); publisher.sendChunk(maybeChunk.value()); wasChunkSent = true; }); // wait some time to check if the publisher is blocked constexpr int64_t SLEEP_IN_MS = 100; - iox::cxx::internal::adaptive_wait().wait_loop([&] { return !isThreadStarted.load(); }); + isThreadStarted.wait(); std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_IN_MS)); EXPECT_THAT(wasChunkSent.load(), Eq(false));