From fa1cde30de1ba39289ea4244cecaa7f22199c8aa Mon Sep 17 00:00:00 2001 From: Kerstin Keller Date: Mon, 12 Aug 2024 16:48:13 +0200 Subject: [PATCH] [core] Rewrite GetTopicsParallel test to be more robust. --- .../src/registration_gettopics.cpp | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/ecal/tests/cpp/registration_test_2/src/registration_gettopics.cpp b/ecal/tests/cpp/registration_test_2/src/registration_gettopics.cpp index c0127164aa..7c66fa3499 100644 --- a/ecal/tests/cpp/registration_test_2/src/registration_gettopics.cpp +++ b/ecal/tests/cpp/registration_test_2/src/registration_gettopics.cpp @@ -135,12 +135,17 @@ TEST(core_cpp_registration_2, GetTopics) eCAL::Finalize(); } +// This test creates a reall big number of publishers. +// It then checks, if they have all been seen using GetTopics() +// And the count is back to 0 upon completion. TEST(core_cpp_registration_2, GetTopicsParallel) { constexpr const int max_publisher_count(2000); constexpr const int waiting_time_thread(4000); constexpr const int parallel_threads(1); + std::atomic testing_completed = false; + // initialize eCAL API eCAL::Initialize(0, nullptr, "core_cpp_registration_2"); @@ -150,37 +155,36 @@ TEST(core_cpp_registration_2, GetTopicsParallel) auto create_publishers = [&]() { std::string topic_name = "Test.ParallelUtilFunctions"; std::atomic call_back_count{ 0 }; - - std::vector> publishers; - for (int pub_count = 0; pub_count < max_publisher_count; pub_count++) { - std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count)); - publishers.push_back(std::move(publisher)); + { + std::vector> publishers; + for (int pub_count = 0; pub_count < max_publisher_count; pub_count++) { + std::unique_ptr publisher = std::make_unique(topic_name + std::to_string(pub_count)); + publishers.push_back(std::move(publisher)); + } + std::this_thread::sleep_for(std::chrono::milliseconds(waiting_time_thread)); } std::this_thread::sleep_for(std::chrono::milliseconds(waiting_time_thread)); + testing_completed = true; }; auto get_topics_from_ecal = [&]() { - size_t found_topics = 0; + size_t number_publishers_seen = 0; + size_t max_number_publishers_seen = 0; + std::set tmp_topic_names; std::map topics; - do { - eCAL::Registration::GetTopicNames(tmp_topic_names); - eCAL::Registration::GetTopics(topics); - - found_topics = tmp_topic_names.size(); - std::cout << "Number of topics found by ecal: " << found_topics << "\n"; - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } while (found_topics < max_publisher_count); - // do it again until all publishers are deleted do { eCAL::Registration::GetTopicNames(tmp_topic_names); eCAL::Registration::GetTopics(topics); - found_topics = tmp_topic_names.size(); - std::cout << "Number of topics found by ecal: " << found_topics << "\n"; + number_publishers_seen = tmp_topic_names.size(); + max_number_publishers_seen = std::max(max_number_publishers_seen, number_publishers_seen); std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } while (found_topics != 0); + } while (!testing_completed); + + EXPECT_EQ(number_publishers_seen, 0); + EXPECT_EQ(max_number_publishers_seen, max_publisher_count); }; std::vector threads_container; @@ -194,14 +198,6 @@ TEST(core_cpp_registration_2, GetTopicsParallel) th.join(); } - std::set final_topic_names; - std::map final_topics; - eCAL::Registration::GetTopicNames(final_topic_names); - eCAL::Registration::GetTopics(final_topics); - - EXPECT_EQ(final_topic_names.size(), 0); - EXPECT_EQ(final_topics.size(), 0); - // finalize eCAL API eCAL::Finalize(); }