@@ -1223,175 +1223,75 @@ TEST_F(CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_query_functio
12231223 9 ); // number of retries
12241224}
12251225
1226- /* Test the graph guard condition notices below changes.
1227- * publisher create/destroy, subscription create/destroy
1228- * service create/destroy, client create/destroy
1229- * Other node added/removed
1226+ /* Test the graph guard condition notices topic changes.
12301227 *
12311228 * Note: this test could be impacted by other communications on the same ROS Domain.
12321229 */
1233- TEST_F (CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_condition_trigger_check) {
1234- #define CHECK_GUARD_CONDITION_CHANGE (EXPECTED_RESULT ) do { \
1235- ret = rcl_wait_set_clear (&wait_set); \
1236- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ; \
1237- ret = rcl_wait_set_add_guard_condition (&wait_set, graph_guard_condition, NULL ); \
1238- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ; \
1239- ret = rcl_wait (&wait_set, time_to_sleep.count ()); \
1240- ASSERT_EQ (EXPECTED_RESULT, ret) << rcl_get_error_string ().str ; \
1241- } while (0 )
1242-
1230+ TEST_F (CLASSNAME(TestGraphFixture, RMW_IMPLEMENTATION), test_graph_guard_condition_topics) {
12431231 rcl_ret_t ret;
1244- std::chrono::nanoseconds time_to_sleep = std::chrono::milliseconds (400 );
1245-
1246- rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set ();
1247- ret = rcl_wait_set_init (
1248- &wait_set, 0 , 1 , 0 , 0 , 0 , 0 , context_ptr, rcl_get_default_allocator ());
1249- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1250- OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1251- {
1252- EXPECT_EQ (RCL_RET_OK, rcl_wait_set_fini (&wait_set)) << rcl_get_error_string ().str ;
1253- });
1254-
1232+ // Create a thread to sleep for a time, then create a publisher, sleep more, then a subscriber,
1233+ // sleep more, destroy the subscriber, sleep more, and then destroy the publisher.
1234+ std::promise<bool > topic_changes_promise;
1235+ std::thread topic_thread (
1236+ [this , &topic_changes_promise]() {
1237+ // sleep
1238+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
1239+ // create the publisher
1240+ rcl_publisher_t pub = rcl_get_zero_initialized_publisher ();
1241+ rcl_publisher_options_t pub_ops = rcl_publisher_get_default_options ();
1242+ rcl_ret_t ret = rcl_publisher_init (
1243+ &pub, this ->node_ptr , ROSIDL_GET_MSG_TYPE_SUPPORT (test_msgs, msg, BasicTypes),
1244+ " /chatter_test_graph_guard_condition_topics" , &pub_ops);
1245+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1246+ // sleep
1247+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
1248+ // create the subscription
1249+ rcl_subscription_t sub = rcl_get_zero_initialized_subscription ();
1250+ rcl_subscription_options_t sub_ops = rcl_subscription_get_default_options ();
1251+ ret = rcl_subscription_init (
1252+ &sub, this ->node_ptr , ROSIDL_GET_MSG_TYPE_SUPPORT (test_msgs, msg, BasicTypes),
1253+ " /chatter_test_graph_guard_condition_topics" , &sub_ops);
1254+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1255+ // sleep
1256+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
1257+ // destroy the subscription
1258+ ret = rcl_subscription_fini (&sub, this ->node_ptr );
1259+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1260+ // sleep
1261+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
1262+ // destroy the publication
1263+ ret = rcl_publisher_fini (&pub, this ->node_ptr );
1264+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1265+ // notify that the thread is done
1266+ topic_changes_promise.set_value (true );
1267+ });
1268+ // Wait for the graph state to change, expecting it to do so at least 4 times,
1269+ // once for each change in the topics thread.
12551270 const rcl_guard_condition_t * graph_guard_condition =
1256- rcl_node_get_graph_guard_condition (node_ptr);
1257-
1258- // Wait for no graph change condition
1259- int idx = 0 ;
1260- for (; idx < 100 ; idx++) {
1261- ret = rcl_wait_set_clear (&wait_set);
1271+ rcl_node_get_graph_guard_condition (this ->node_ptr );
1272+ ASSERT_NE (nullptr , graph_guard_condition) << rcl_get_error_string ().str ;
1273+ std::shared_future<bool > future = topic_changes_promise.get_future ();
1274+ size_t graph_changes_count = 0 ;
1275+ // while the topic thread is not done, wait and count the graph changes
1276+ while (future.wait_for (std::chrono::seconds (0 )) != std::future_status::ready) {
1277+ ret = rcl_wait_set_clear (this ->wait_set_ptr );
12621278 ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1263- ret = rcl_wait_set_add_guard_condition (&wait_set , graph_guard_condition, NULL );
1279+ ret = rcl_wait_set_add_guard_condition (this -> wait_set_ptr , graph_guard_condition, NULL );
12641280 ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1265- ret = rcl_wait (&wait_set, time_to_sleep.count ());
1266- if (RCL_RET_TIMEOUT == ret) {
1267- break ;
1268- } else {
1269- RCUTILS_LOG_INFO_NAMED (
1270- ROS_PACKAGE_NAME,
1271- " waiting for no graph change condition ..." );
1281+ std::chrono::nanoseconds time_to_sleep = std::chrono::milliseconds (400 );
1282+ RCUTILS_LOG_INFO_NAMED (
1283+ ROS_PACKAGE_NAME,
1284+ " waiting up to '%s' nanoseconds for graph changes" ,
1285+ std::to_string (time_to_sleep.count ()).c_str ());
1286+ ret = rcl_wait (this ->wait_set_ptr , time_to_sleep.count ());
1287+ if (ret == RCL_RET_TIMEOUT) {
1288+ continue ;
12721289 }
1290+ graph_changes_count++;
12731291 }
1274- ASSERT_NE (idx, 100 );
1275-
1276- // Graph change since creating the publisher
1277- rcl_publisher_t pub = rcl_get_zero_initialized_publisher ();
1278- rcl_publisher_options_t pub_ops = rcl_publisher_get_default_options ();
1279- ret = rcl_publisher_init (
1280- &pub, node_ptr, ROSIDL_GET_MSG_TYPE_SUPPORT (test_msgs, msg, BasicTypes),
1281- " /chatter_test_graph_guard_condition_topics" , &pub_ops);
1282- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1283-
1284- {
1285- SCOPED_TRACE (" Check guard condition change failed !" );
1286- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1287- }
1288-
1289- // Graph change since destroying the publisher
1290- ret = rcl_publisher_fini (&pub, node_ptr);
1291- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1292-
1293- {
1294- SCOPED_TRACE (" Check guard condition change failed !" );
1295- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1296- }
1297-
1298- // Graph change since creating the subscription
1299- rcl_subscription_t sub = rcl_get_zero_initialized_subscription ();
1300- rcl_subscription_options_t sub_ops = rcl_subscription_get_default_options ();
1301- ret = rcl_subscription_init (
1302- &sub, node_ptr, ROSIDL_GET_MSG_TYPE_SUPPORT (test_msgs, msg, BasicTypes),
1303- " /chatter_test_graph_guard_condition_topics" , &sub_ops);
1304- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1305-
1306- {
1307- SCOPED_TRACE (" Check guard condition change failed !" );
1308- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1309- }
1310-
1311- // Graph change since destroying the subscription
1312- ret = rcl_subscription_fini (&sub, node_ptr);
1313- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1314-
1315- {
1316- SCOPED_TRACE (" Check guard condition change failed !" );
1317- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1318- }
1319-
1320- // Graph change since creating service
1321- rcl_service_t service = rcl_get_zero_initialized_service ();
1322- rcl_service_options_t service_options = rcl_service_get_default_options ();
1323- ret = rcl_service_init (
1324- &service,
1325- node_ptr,
1326- ROSIDL_GET_SRV_TYPE_SUPPORT (test_msgs, srv, BasicTypes),
1327- " test_graph_guard_condition_service" ,
1328- &service_options);
1329- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1330-
1331- {
1332- SCOPED_TRACE (" Check guard condition change failed !" );
1333- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1334- }
1335-
1336- // Graph change since destroy service
1337- ret = rcl_service_fini (&service, node_ptr);
1338- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1339-
1340- {
1341- SCOPED_TRACE (" Check guard condition change failed !" );
1342- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1343- }
1344-
1345- // Graph change since creating client
1346- rcl_client_t client = rcl_get_zero_initialized_client ();
1347- rcl_client_options_t client_options = rcl_client_get_default_options ();
1348- ret = rcl_client_init (
1349- &client,
1350- node_ptr,
1351- ROSIDL_GET_SRV_TYPE_SUPPORT (test_msgs, srv, BasicTypes),
1352- " test_graph_guard_condition_service" ,
1353- &client_options);
1354- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1355-
1356- {
1357- SCOPED_TRACE (" Check guard condition change failed !" );
1358- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1359- }
1360-
1361- // Graph change since destroying client
1362- ret = rcl_client_fini (&client, node_ptr);
1363- EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1364-
1365- {
1366- SCOPED_TRACE (" Check guard condition change failed !" );
1367- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1368- }
1369-
1370- // Graph change since adding new node
1371- rcl_node_t node_new = rcl_get_zero_initialized_node ();
1372- rcl_node_options_t node_options = rcl_node_get_default_options ();
1373- ret = rcl_node_init (&node_new, " test_graph2" , " " , context_ptr, &node_options);
1374- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1375-
1376- {
1377- SCOPED_TRACE (" Check guard condition change failed !" );
1378- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1379- }
1380-
1381- // Graph change since destroying new node
1382- ret = rcl_node_fini (&node_new);
1383- ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1384-
1385- {
1386- SCOPED_TRACE (" Check guard condition change failed !" );
1387- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_OK);
1388- }
1389-
1390- // Should not get graph change if no change
1391- {
1392- SCOPED_TRACE (" Check guard condition change failed !" );
1393- CHECK_GUARD_CONDITION_CHANGE (RCL_RET_TIMEOUT);
1394- }
1292+ topic_thread.join ();
1293+ // expect at least 4 changes
1294+ ASSERT_GE (graph_changes_count, 4ul );
13951295}
13961296
13971297/* Test the rcl_service_server_is_available function.
0 commit comments