@@ -633,6 +633,114 @@ TEST_F(TestSubscriptionUse, ignore_local_publications) {
633633 }
634634}
635635
636+ TEST_F (TestSubscriptionUse, ignore_local_publications_serialized) {
637+ rmw_ret_t ret;
638+
639+ // Create publisher
640+ rmw_publisher_options_t pub_options = rmw_get_default_publisher_options ();
641+ rmw_publisher_t * pub = rmw_create_publisher (node, ts, topic_name, &qos_profile, &pub_options);
642+ ASSERT_NE (nullptr , pub) << rmw_get_error_string ().str ;
643+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
644+ {
645+ EXPECT_EQ (RMW_RET_OK, rmw_destroy_publisher (node, pub)) << rmw_get_error_string ().str ;
646+ });
647+
648+ // Create subscription with ignore_local_publications = true
649+ rmw_subscription_options_t sub_options_ignorelocal = rmw_get_default_subscription_options ();
650+ sub_options_ignorelocal.ignore_local_publications = true ;
651+ rmw_subscription_t * sub_ignorelocal =
652+ rmw_create_subscription (node, ts, topic_name, &qos_profile, &sub_options_ignorelocal);
653+ ASSERT_NE (nullptr , sub_ignorelocal) << rmw_get_error_string ().str ;
654+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
655+ {
656+ EXPECT_EQ (
657+ RMW_RET_OK, rmw_destroy_subscription (node, sub_ignorelocal)) << rmw_get_error_string ().str ;
658+ });
659+
660+ size_t subscription_count = 0u ;
661+ SLEEP_AND_RETRY_UNTIL (rmw_intraprocess_discovery_delay, rmw_intraprocess_discovery_delay * 10 ) {
662+ ret = rmw_publisher_count_matched_subscriptions (pub, &subscription_count);
663+ if (RMW_RET_OK == ret && 2u == subscription_count) { // Early return on failure.
664+ break ;
665+ }
666+ }
667+
668+ // Roundtrip message from publisher to both subscriptions
669+ test_msgs__msg__BasicTypes original_message{};
670+ ASSERT_TRUE (test_msgs__msg__BasicTypes__init (&original_message));
671+ original_message.bool_value = true ;
672+ original_message.char_value = ' k' ;
673+ original_message.float32_value = 3 .14159f ;
674+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
675+ {
676+ test_msgs__msg__BasicTypes__fini (&original_message);
677+ });
678+
679+ rmw_publisher_allocation_t * null_allocation_p{nullptr };
680+ rmw_subscription_allocation_t * null_allocation_s{nullptr };
681+
682+ ret = rmw_publish (pub, &original_message, null_allocation_p);
683+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
684+
685+ rmw_subscriptions_t subscriptions;
686+ void * subscriptions_storage[2 ];
687+ subscriptions_storage[0 ] = sub_ignorelocal->data ;
688+ subscriptions_storage[1 ] = sub->data ;
689+ subscriptions.subscribers = subscriptions_storage;
690+ subscriptions.subscriber_count = 2 ;
691+
692+ rmw_wait_set_t * wait_set = rmw_create_wait_set (&context, 2 );
693+ ASSERT_NE (nullptr , wait_set) << rmw_get_error_string ().str ;
694+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
695+ {
696+ EXPECT_EQ (
697+ RMW_RET_OK, rmw_destroy_wait_set (wait_set)) << rmw_get_error_string ().str ;
698+ });
699+ rmw_time_t timeout = {1 , 0 }; // 1000ms
700+ ret = rmw_wait (&subscriptions, nullptr , nullptr , nullptr , nullptr , wait_set, &timeout);
701+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
702+ ASSERT_NE (nullptr , subscriptions.subscribers [1 ]);
703+
704+ rcutils_allocator_t allocator = rcutils_get_default_allocator ();
705+
706+ // ignore_local_publications = true
707+ {
708+ bool taken_serialized = false ;
709+ rmw_serialized_message_t msg = rmw_get_zero_initialized_serialized_message ();
710+ ASSERT_EQ (
711+ RMW_RET_OK, rmw_serialized_message_init (
712+ &msg, 0 , &allocator)) << rmw_get_error_string ().str ;
713+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
714+ {
715+ EXPECT_EQ (
716+ RMW_RET_OK, rmw_serialized_message_fini (&msg)) << rmw_get_error_string ().str ;
717+ });
718+
719+ ret = rmw_take_serialized_message (sub_ignorelocal, &msg, &taken_serialized, null_allocation_s);
720+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
721+ EXPECT_FALSE (taken_serialized);
722+ }
723+
724+ // ignore_local_publications = false
725+ {
726+ bool taken_serialized = false ;
727+ rmw_serialized_message_t msg = rmw_get_zero_initialized_serialized_message ();
728+ ASSERT_EQ (
729+ RMW_RET_OK, rmw_serialized_message_init (
730+ &msg, 0 , &allocator)) << rmw_get_error_string ().str ;
731+ OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT (
732+ {
733+ EXPECT_EQ (
734+ RMW_RET_OK, rmw_serialized_message_fini (&msg)) << rmw_get_error_string ().str ;
735+ });
736+
737+ ret = rmw_take_serialized_message (sub, &msg, &taken_serialized, null_allocation_s);
738+ EXPECT_EQ (RMW_RET_OK, ret) << rmw_get_error_string ().str ;
739+ EXPECT_TRUE (taken_serialized);
740+ // Optionally, could deserialize and compare, but not needed for this test.
741+ }
742+ }
743+
636744TEST_F (TestSubscriptionUse, take_sequence) {
637745 size_t count = 1u ;
638746 size_t taken = 10u ; // Non-zero value to check variable update
0 commit comments