@@ -524,6 +524,107 @@ TEST_F(CLASSNAME(TestSubscriptionUse, RMW_IMPLEMENTATION), take_with_info_with_b
524524 sub->implementation_identifier = implementation_identifier;
525525}
526526
527+ TEST_F (CLASSNAME(TestSubscriptionUse, RMW_IMPLEMENTATION), ignore_local_publications) {
528+ rmw_ret_t ret;
529+ bool taken = false ;
530+
531+ // Create publisher
532+ rmw_publisher_options_t pub_options = rmw_get_default_publisher_options ();
533+ rmw_publisher_t * pub = rmw_create_publisher (node, ts, topic_name, &qos_profile, &pub_options);
534+ ASSERT_NE (nullptr , pub) << rmw_get_error_string ().str ;
535+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
536+ {
537+ EXPECT_EQ (RMW_RET_OK, rmw_destroy_publisher (node, pub)) << rmw_get_error_string ().str ;
538+ });
539+
540+ // Create subscription with ignore_local_publications = true
541+ rmw_subscription_options_t sub_options_ignorelocal = rmw_get_default_subscription_options ();
542+ sub_options_ignorelocal.ignore_local_publications = true ;
543+ rmw_subscription_t * sub_ignorelocal =
544+ rmw_create_subscription (node, ts, topic_name, &qos_profile, &sub_options_ignorelocal);
545+ ASSERT_NE (nullptr , sub_ignorelocal) << rmw_get_error_string ().str ;
546+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
547+ {
548+ EXPECT_EQ (
549+ RMW_RET_OK, rmw_destroy_subscription (node, sub_ignorelocal)) << rmw_get_error_string ().str ;
550+ });
551+
552+ size_t subscription_count = 0u ;
553+ SLEEP_AND_RETRY_UNTIL (rmw_intraprocess_discovery_delay, rmw_intraprocess_discovery_delay * 10 ) {
554+ ret = rmw_publisher_count_matched_subscriptions (pub, &subscription_count);
555+ if (RMW_RET_OK == ret && 2u == subscription_count) { // Early return on failure.
556+ break ;
557+ }
558+ }
559+
560+ // Roundtrip message from publisher to both subscriptions
561+ test_msgs__msg__BasicTypes original_message{};
562+ ASSERT_TRUE (test_msgs__msg__BasicTypes__init (&original_message));
563+ original_message.bool_value = true ;
564+ original_message.char_value = ' k' ;
565+ original_message.float32_value = 3 .14159f ;
566+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
567+ {
568+ test_msgs__msg__BasicTypes__fini (&original_message);
569+ });
570+
571+ rmw_publisher_allocation_t * null_allocation_p{nullptr };
572+ rmw_subscription_allocation_t * null_allocation_s{nullptr };
573+
574+ ret = rmw_publish (pub, &original_message, null_allocation_p);
575+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
576+
577+ rmw_subscriptions_t subscriptions;
578+ void * subscriptions_storage[2 ];
579+ subscriptions_storage[0 ] = sub_ignorelocal->data ;
580+ subscriptions_storage[1 ] = sub->data ;
581+ subscriptions.subscribers = subscriptions_storage;
582+ subscriptions.subscriber_count = 2 ;
583+
584+ rmw_wait_set_t * wait_set = rmw_create_wait_set (&context, 2 );
585+ ASSERT_NE (nullptr , wait_set) << rmw_get_error_string ().str ;
586+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
587+ {
588+ EXPECT_EQ (
589+ RMW_RET_OK, rmw_destroy_wait_set (wait_set)) << rmw_get_error_string ().str ;
590+ });
591+ rmw_time_t timeout = {1 , 0 }; // 1000ms
592+ ret = rmw_wait (&subscriptions, nullptr , nullptr , nullptr , nullptr , wait_set, &timeout);
593+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
594+ // Subscriptions that ignore local publications may or may not be awaken by locally sent messages.
595+ // ASSERT_NE(nullptr, subscriptions.subscribers[0]);
596+ ASSERT_NE (nullptr , subscriptions.subscribers [1 ]);
597+
598+ // ignore_local_publications = true
599+ {
600+ test_msgs__msg__BasicTypes output_message{};
601+ ASSERT_TRUE (test_msgs__msg__BasicTypes__init (&output_message));
602+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
603+ {
604+ test_msgs__msg__BasicTypes__fini (&output_message);
605+ });
606+
607+ ret = rmw_take (sub_ignorelocal, &output_message, &taken, null_allocation_s);
608+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
609+ EXPECT_FALSE (taken);
610+ }
611+
612+ // ignore_local_publications = false
613+ {
614+ test_msgs__msg__BasicTypes output_message{};
615+ ASSERT_TRUE (test_msgs__msg__BasicTypes__init (&output_message));
616+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
617+ {
618+ test_msgs__msg__BasicTypes__fini (&output_message);
619+ });
620+
621+ ret = rmw_take (sub, &output_message, &taken, null_allocation_s);
622+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
623+ EXPECT_TRUE (taken);
624+ EXPECT_EQ (original_message, output_message);
625+ }
626+ }
627+
527628TEST_F (CLASSNAME(TestSubscriptionUse, RMW_IMPLEMENTATION), take_sequence) {
528629 size_t count = 1u ;
529630 size_t taken = 10u ; // Non-zero value to check variable update
0 commit comments