@@ -1396,6 +1396,150 @@ TEST_F(TestSubscriptionFixtureInit, test_subscription_bad_take) {
13961396 rcl_reset_error ();
13971397}
13981398
1399+ TEST_F (TestSubscriptionFixture, test_subscription_option_ignore_local_publications) {
1400+ // The current Implementations for ignoring local publications
1401+ // While creating a subscription, the ignore_local_publications option is set to true
1402+ // Notification of message
1403+ // Connected to local publications from local publication Take data
1404+ // ------------------------------- ----------------------- ---------
1405+ // rmw_fastrtps Yes Yes No
1406+ // rmw_cyclonedds No No No
1407+ // rmw_zenoh Yes No No
1408+ // rmw_connextdds Yes Yes No
1409+
1410+ rcl_ret_t ret;
1411+
1412+ // Create a publisher
1413+ rcl_publisher_t pub = rcl_get_zero_initialized_publisher ();
1414+ const rosidl_message_type_support_t * ts =
1415+ ROSIDL_GET_MSG_TYPE_SUPPORT (test_msgs, msg, Strings);
1416+ constexpr char topic[] = " rcl_test_subscription_option_ignore_local_publications" ;
1417+ rcl_publisher_options_t publisher_options = rcl_publisher_get_default_options ();
1418+ ret = rcl_publisher_init (&pub, this ->node_ptr , ts, topic, &publisher_options);
1419+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1420+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1421+ {
1422+ rcl_ret_t ret = rcl_publisher_fini (&pub, this ->node_ptr );
1423+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1424+ });
1425+
1426+ // Create two subscriptions: one with ignore_local_publications set to false,
1427+ // and another with it set to true.
1428+ rcl_subscription_t sub = rcl_get_zero_initialized_subscription ();
1429+ rcl_subscription_t sub_ignorelocal = rcl_get_zero_initialized_subscription ();
1430+ rcl_subscription_options_t sub_opts = rcl_subscription_get_default_options ();
1431+
1432+ // sub with ignore_local_publications set to false
1433+ ret = rcl_subscription_init (&sub, this ->node_ptr , ts, topic, &sub_opts);
1434+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1435+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1436+ {
1437+ rcl_ret_t ret = rcl_subscription_fini (&sub, this ->node_ptr );
1438+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1439+ });
1440+
1441+ // sub_ignorelocal with ignore_local_publications set to true
1442+ sub_opts.rmw_subscription_options .ignore_local_publications = true ;
1443+ ret = rcl_subscription_init (&sub_ignorelocal, this ->node_ptr , ts, topic, &sub_opts);
1444+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1445+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1446+ {
1447+ rcl_ret_t ret = rcl_subscription_fini (&sub_ignorelocal, this ->node_ptr );
1448+ EXPECT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1449+ });
1450+
1451+ // Wait for at least 1 subscription to be established
1452+ ASSERT_TRUE (wait_for_established_subscription (&pub, 5 , 1000 ));
1453+
1454+ // Test with rcl_take
1455+ // publish a message
1456+ constexpr char test_string[] = " message" ;
1457+ {
1458+ test_msgs__msg__Strings msg;
1459+ test_msgs__msg__Strings__init (&msg);
1460+ ASSERT_TRUE (rosidl_runtime_c__String__assign (&msg.string_value , test_string));
1461+ ret = rcl_publish (&pub, &msg, nullptr );
1462+ test_msgs__msg__Strings__fini (&msg);
1463+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1464+ }
1465+
1466+ // Wait for one subscription to be ready
1467+ ASSERT_TRUE (wait_for_subscription_to_be_ready (&sub, context_ptr, 2 , 1000 ));
1468+
1469+ // ignore_local_publications is false
1470+ {
1471+ test_msgs__msg__Strings msg;
1472+ test_msgs__msg__Strings__init (&msg);
1473+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1474+ {
1475+ test_msgs__msg__Strings__fini (&msg);
1476+ });
1477+ ret = rcl_take (&sub, &msg, nullptr , nullptr );
1478+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1479+ ASSERT_EQ (
1480+ std::string (test_string),
1481+ std::string (msg.string_value .data , msg.string_value .size ));
1482+ }
1483+
1484+ // ignore_local_publications is true
1485+ {
1486+ test_msgs__msg__Strings msg;
1487+ test_msgs__msg__Strings__init (&msg);
1488+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1489+ {
1490+ test_msgs__msg__Strings__fini (&msg);
1491+ });
1492+ ret = rcl_take (&sub, &msg, nullptr , nullptr );
1493+ ASSERT_EQ (RCL_RET_SUBSCRIPTION_TAKE_FAILED, ret);
1494+ }
1495+
1496+ // Test with rcl_take_serialized_message
1497+ // publish message again
1498+ {
1499+ test_msgs__msg__Strings msg;
1500+ test_msgs__msg__Strings__init (&msg);
1501+ ASSERT_TRUE (rosidl_runtime_c__String__assign (&msg.string_value , test_string));
1502+ ret = rcl_publish (&pub, &msg, nullptr );
1503+ test_msgs__msg__Strings__fini (&msg);
1504+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1505+ }
1506+
1507+ // Wait for one subscription to be ready
1508+ ASSERT_TRUE (wait_for_subscription_to_be_ready (&sub, context_ptr, 2 , 1000 ));
1509+
1510+ // ignore_local_publications is false
1511+ {
1512+ rmw_serialized_message_t serialized_msg = rmw_get_zero_initialized_serialized_message ();
1513+ size_t initial_serialization_capacity = 0u ;
1514+ auto allocator = rcl_get_default_allocator ();
1515+ rmw_message_info_t message_info;
1516+ ASSERT_EQ (RCL_RET_OK,
1517+ rmw_serialized_message_init (&serialized_msg, initial_serialization_capacity, &allocator));
1518+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1519+ {
1520+ EXPECT_EQ (RCL_RET_OK, rmw_serialized_message_fini (&serialized_msg));
1521+ });
1522+ ret = rcl_take_serialized_message (&sub, &serialized_msg, &message_info, nullptr );
1523+ ASSERT_EQ (RCL_RET_OK, ret) << rcl_get_error_string ().str ;
1524+ }
1525+
1526+ // ignore_local_publications is true
1527+ {
1528+ rmw_serialized_message_t serialized_msg = rmw_get_zero_initialized_serialized_message ();
1529+ size_t initial_serialization_capacity = 0u ;
1530+ auto allocator = rcl_get_default_allocator ();
1531+ rmw_message_info_t message_info;
1532+ ASSERT_EQ (RCL_RET_OK,
1533+ rmw_serialized_message_init (&serialized_msg, initial_serialization_capacity, &allocator));
1534+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
1535+ {
1536+ EXPECT_EQ (RCL_RET_OK, rmw_serialized_message_fini (&serialized_msg));
1537+ });
1538+ ret = rcl_take_serialized_message (&sub_ignorelocal, &serialized_msg, &message_info, nullptr );
1539+ ASSERT_EQ (RCL_RET_SUBSCRIPTION_TAKE_FAILED, ret);
1540+ }
1541+ }
1542+
13991543/* bad take_serialized
14001544 */
14011545TEST_F (TestSubscriptionFixtureInit, test_subscription_bad_take_serialized) {
0 commit comments