From f9f390bf7320358a65d27e27cbfe5565db3207bc Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Thu, 14 Oct 2021 11:24:55 -0700 Subject: [PATCH] Wait for publisher and subscription to match Fix #1775 Connext takes significantly longer for discovery to happened compared to the other RMWs. So, waiting an arbitrary amount of time for a message to be received is brittle. By instead waiting for the publisher and subscription to match, the tests become more robust. Signed-off-by: Jacob Perron --- .../test_subscription_with_type_adapter.cpp | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/rclcpp/test/rclcpp/test_subscription_with_type_adapter.cpp b/rclcpp/test/rclcpp/test_subscription_with_type_adapter.cpp index 54c11bf1b1..7668feadb0 100644 --- a/rclcpp/test/rclcpp/test_subscription_with_type_adapter.cpp +++ b/rclcpp/test/rclcpp/test_subscription_with_type_adapter.cpp @@ -77,7 +77,9 @@ class CLASSNAME (test_intra_process_within_one_node, RMW_IMPLEMENTATION) : publi public: static void SetUpTestCase() { - rclcpp::init(0, nullptr); + if (!rclcpp::ok()) { + rclcpp::init(0, nullptr); + } } static void TearDownTestCase() @@ -128,6 +130,20 @@ void wait_for_message_to_be_received( executor.spin_once(g_sleep_per_loop); } } + +bool wait_for_match( + const std::shared_ptr sub, + const std::shared_ptr pub) +{ + int i = 0; + bool matched = false; + while (!matched && i < g_max_loops) { + matched = sub->get_publisher_count() > 0 && pub->get_subscription_count() > 0; + std::this_thread::sleep_for(g_sleep_per_loop); + } + return matched; +} + /* * Testing publisher creation signatures with a type adapter. */ @@ -175,6 +191,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -191,6 +208,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -206,6 +224,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), (*msg).c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -222,6 +241,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -237,6 +257,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), (*msg).c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -253,6 +274,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -268,6 +290,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg.data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -284,6 +307,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -299,6 +323,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg->data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -315,6 +340,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -330,6 +356,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg->data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -346,6 +373,7 @@ TEST_F( ASSERT_TRUE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -381,6 +409,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -398,6 +427,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -413,6 +443,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), (*msg).c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -430,6 +461,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -445,6 +477,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), (*msg).c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -461,6 +494,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -476,6 +510,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg.data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -492,6 +527,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -507,6 +543,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg->data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -523,6 +560,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -538,6 +576,7 @@ TEST_F( ASSERT_STREQ(message_data.c_str(), msg->data.c_str()); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node); @@ -554,6 +593,7 @@ TEST_F( ASSERT_FALSE(message_info.get_rmw_message_info().from_intra_process); }; auto sub = node->create_subscription(topic_name, 1, callback); + ASSERT_TRUE(wait_for_match(sub, pub)); pub->publish(msg); ASSERT_FALSE(is_received); wait_for_message_to_be_received(is_received, node);