From 5d7ffc2cd4e853f5487910c2640b4883dd488f6d Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 30 Oct 2024 13:59:55 +0100 Subject: [PATCH 1/2] [#493] Add rust tests --- iceoryx2/tests/service_event_tests.rs | 76 ++++++++++++ .../tests/service_publish_subscribe_tests.rs | 108 ++++++++++++++++++ 2 files changed, 184 insertions(+) diff --git a/iceoryx2/tests/service_event_tests.rs b/iceoryx2/tests/service_event_tests.rs index 2064fbe44..4dfabcc0a 100644 --- a/iceoryx2/tests/service_event_tests.rs +++ b/iceoryx2/tests/service_event_tests.rs @@ -832,6 +832,82 @@ mod service_event { assert_that!(node.service_builder(&service_name).event().create(), is_ok); } + #[test] + fn service_can_be_opened_when_there_is_a_notifier() { + let event_id = EventId::new(76); + let service_name = generate_name(); + let config = generate_isolated_config(); + let node = NodeBuilder::new().config(&config).create::().unwrap(); + let sut = node + .service_builder(&service_name) + .event() + .create() + .unwrap(); + let listener = sut.listener_builder().create().unwrap(); + let notifier = sut.notifier_builder().create().unwrap(); + + drop(sut); + let sut = node.service_builder(&service_name).event().open(); + assert_that!(sut, is_ok); + drop(sut); + let sut = node.service_builder(&service_name).event().create(); + assert_that!(sut.err().unwrap(), eq EventCreateError::AlreadyExists); + drop(listener); + + let sut = node.service_builder(&service_name).event().open().unwrap(); + let listener = sut.listener_builder().create().unwrap(); + notifier.notify_with_custom_event_id(event_id).unwrap(); + let notification = listener.try_wait_one().unwrap(); + assert_that!(notification, eq Some(event_id)); + + drop(listener); + drop(sut); + drop(notifier); + + let sut = node.service_builder(&service_name).event().open(); + assert_that!(sut.err().unwrap(), eq EventOpenError::DoesNotExist); + let sut = node.service_builder(&service_name).event().create(); + assert_that!(sut, is_ok); + } + + #[test] + fn service_can_be_opened_when_there_is_a_listener() { + let event_id = EventId::new(93); + let service_name = generate_name(); + let config = generate_isolated_config(); + let node = NodeBuilder::new().config(&config).create::().unwrap(); + let sut = node + .service_builder(&service_name) + .event() + .create() + .unwrap(); + let listener = sut.listener_builder().create().unwrap(); + let notifier = sut.notifier_builder().create().unwrap(); + + drop(sut); + let sut = node.service_builder(&service_name).event().open(); + assert_that!(sut, is_ok); + drop(sut); + let sut = node.service_builder(&service_name).event().create(); + assert_that!(sut.err().unwrap(), eq EventCreateError::AlreadyExists); + drop(notifier); + + let sut = node.service_builder(&service_name).event().open().unwrap(); + let notifier = sut.notifier_builder().create().unwrap(); + notifier.notify_with_custom_event_id(event_id).unwrap(); + let notification = listener.try_wait_one().unwrap(); + assert_that!(notification, eq Some(event_id)); + + drop(notifier); + drop(sut); + drop(listener); + + let sut = node.service_builder(&service_name).event().open(); + assert_that!(sut.err().unwrap(), eq EventOpenError::DoesNotExist); + let sut = node.service_builder(&service_name).event().create(); + assert_that!(sut, is_ok); + } + #[test] fn try_wait_does_not_block() { let _watch_dog = Watchdog::new(); diff --git a/iceoryx2/tests/service_publish_subscribe_tests.rs b/iceoryx2/tests/service_publish_subscribe_tests.rs index a5fc20d75..d3e935f51 100644 --- a/iceoryx2/tests/service_publish_subscribe_tests.rs +++ b/iceoryx2/tests/service_publish_subscribe_tests.rs @@ -2256,6 +2256,114 @@ mod service_publish_subscribe { ); } + #[test] + fn service_can_be_opened_when_there_is_a_publisher() { + let payload = 1809723987; + let service_name = generate_name(); + let config = generate_isolated_config(); + let node = NodeBuilder::new().config(&config).create::().unwrap(); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create() + .unwrap(); + let subscriber = sut.subscriber_builder().create().unwrap(); + let publisher = sut.publisher_builder().create().unwrap(); + + drop(sut); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open(); + assert_that!(sut, is_ok); + drop(sut); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create(); + assert_that!(sut.err().unwrap(), eq PublishSubscribeCreateError::AlreadyExists); + drop(subscriber); + + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open() + .unwrap(); + let subscriber = sut.subscriber_builder().create().unwrap(); + publisher.send_copy(payload).unwrap(); + let sample = subscriber.receive().unwrap().unwrap(); + assert_that!(*sample.payload(), eq payload); + + drop(subscriber); + drop(sut); + drop(publisher); + + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open(); + assert_that!(sut.err().unwrap(), eq PublishSubscribeOpenError::DoesNotExist); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create(); + assert_that!(sut, is_ok); + } + + #[test] + fn service_can_be_opened_when_there_is_a_subscriber() { + let payload = 59123544; + let service_name = generate_name(); + let config = generate_isolated_config(); + let node = NodeBuilder::new().config(&config).create::().unwrap(); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create() + .unwrap(); + let subscriber = sut.subscriber_builder().create().unwrap(); + let publisher = sut.publisher_builder().create().unwrap(); + + drop(sut); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open(); + assert_that!(sut, is_ok); + drop(sut); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create(); + assert_that!(sut.err().unwrap(), eq PublishSubscribeCreateError::AlreadyExists); + drop(publisher); + + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open() + .unwrap(); + let publisher = sut.publisher_builder().create().unwrap(); + publisher.send_copy(payload).unwrap(); + let sample = subscriber.receive().unwrap().unwrap(); + assert_that!(*sample.payload(), eq payload); + + drop(publisher); + drop(sut); + drop(subscriber); + + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .open(); + assert_that!(sut.err().unwrap(), eq PublishSubscribeOpenError::DoesNotExist); + let sut = node + .service_builder(&service_name) + .publish_subscribe::() + .create(); + assert_that!(sut, is_ok); + } + #[test] fn subscriber_can_decrease_buffer_size() { const BUFFER_SIZE: usize = 16; From c72fd7c7aee9e68dee16d23b5b81ea46d73b6b0e Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 30 Oct 2024 15:19:59 +0100 Subject: [PATCH 2/2] [#493] Add cxx tests --- .../cxx/tests/src/service_event_tests.cpp | 84 +++++++++++++++++ .../src/service_publish_subscribe_tests.cpp | 91 +++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/iceoryx2-ffi/cxx/tests/src/service_event_tests.cpp b/iceoryx2-ffi/cxx/tests/src/service_event_tests.cpp index cbe30fbd5..d0f21ced1 100644 --- a/iceoryx2-ffi/cxx/tests/src/service_event_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/service_event_tests.cpp @@ -277,4 +277,88 @@ TYPED_TEST(ServiceEventTest, timed_wait_one_does_not_deadlock) { TYPED_TEST(ServiceEventTest, timed_wait_all_does_not_deadlock) { this->listener.timed_wait_all([](auto) {}, TIMEOUT).expect(""); } + +TYPED_TEST(ServiceEventTest, service_can_be_opened_when_there_is_a_notifier) { + constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; + const auto event_id = EventId(54); + const auto service_name = iox2_testing::generate_service_name(); + + auto node = NodeBuilder().create().expect(""); + auto sut = + iox::optional>(node.service_builder(service_name).event().create().expect("")); + auto listener = iox::optional>(sut->listener_builder().create().expect("")); + auto notifier = iox::optional>(sut->notifier_builder().create().expect("")); + + sut.reset(); + { + auto temp_sut = node.service_builder(service_name).event().open(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } + { + auto temp_sut = node.service_builder(service_name).event().create(); + ASSERT_THAT(temp_sut.error(), Eq(EventCreateError::AlreadyExists)); + } + listener.reset(); + + sut = iox::optional>(node.service_builder(service_name).event().open().expect("")); + listener = iox::optional>(sut->listener_builder().create().expect("")); + notifier->notify_with_custom_event_id(event_id).expect(""); + auto notification = listener->try_wait_one().expect(""); + ASSERT_THAT(notification->as_value(), Eq(event_id.as_value())); + + listener.reset(); + sut.reset(); + notifier.reset(); + + { + auto temp_sut = node.service_builder(service_name).event().open(); + ASSERT_THAT(temp_sut.error(), Eq(EventOpenError::DoesNotExist)); + } + { + auto temp_sut = node.service_builder(service_name).event().create(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } +} + +TYPED_TEST(ServiceEventTest, service_can_be_opened_when_there_is_a_listener) { + constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; + const auto event_id = EventId(24); + const auto service_name = iox2_testing::generate_service_name(); + + auto node = NodeBuilder().create().expect(""); + auto sut = + iox::optional>(node.service_builder(service_name).event().create().expect("")); + auto listener = iox::optional>(sut->listener_builder().create().expect("")); + auto notifier = iox::optional>(sut->notifier_builder().create().expect("")); + + sut.reset(); + { + auto temp_sut = node.service_builder(service_name).event().open(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } + { + auto temp_sut = node.service_builder(service_name).event().create(); + ASSERT_THAT(temp_sut.error(), Eq(EventCreateError::AlreadyExists)); + } + notifier.reset(); + + sut = iox::optional>(node.service_builder(service_name).event().open().expect("")); + notifier = iox::optional>(sut->notifier_builder().create().expect("")); + notifier->notify_with_custom_event_id(event_id).expect(""); + auto notification = listener->try_wait_one().expect(""); + ASSERT_THAT(notification->as_value(), Eq(event_id.as_value())); + + notifier.reset(); + sut.reset(); + listener.reset(); + + { + auto temp_sut = node.service_builder(service_name).event().open(); + ASSERT_THAT(temp_sut.error(), Eq(EventOpenError::DoesNotExist)); + } + { + auto temp_sut = node.service_builder(service_name).event().create(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } +} } // namespace diff --git a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp index d9bb1c071..3e4be6f07 100644 --- a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp @@ -419,4 +419,95 @@ TYPED_TEST(ServicePublishSubscribeTest, has_sample_works) { auto sample = sut_subscriber.receive().expect(""); ASSERT_FALSE(*sut_subscriber.has_samples()); } + +TYPED_TEST(ServicePublishSubscribeTest, service_can_be_opened_when_there_is_a_publisher) { + constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; + const uint64_t payload = 9871273; + const auto service_name = iox2_testing::generate_service_name(); + + auto node = NodeBuilder().create().expect(""); + auto sut = iox::optional>( + node.service_builder(service_name).template publish_subscribe().create().expect("")); + auto subscriber = + iox::optional>(sut->subscriber_builder().create().expect("")); + auto publisher = + iox::optional>(sut->publisher_builder().create().expect("")); + + sut.reset(); + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().open(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().create(); + ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeCreateError::AlreadyExists)); + } + subscriber.reset(); + + sut = iox::optional>( + node.service_builder(service_name).template publish_subscribe().open().expect("")); + subscriber = iox::optional>(sut->subscriber_builder().create().expect("")); + publisher->send_copy(payload).expect(""); + auto sample = subscriber->receive().expect(""); + ASSERT_THAT(sample->payload(), Eq(payload)); + + subscriber.reset(); + sut.reset(); + publisher.reset(); + + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().open(); + ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeOpenError::DoesNotExist)); + } + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().create(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } +} + +TYPED_TEST(ServicePublishSubscribeTest, service_can_be_opened_when_there_is_a_subscriber) { + constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; + const uint64_t payload = 57812; + const auto service_name = iox2_testing::generate_service_name(); + + auto node = NodeBuilder().create().expect(""); + auto sut = iox::optional>( + node.service_builder(service_name).template publish_subscribe().create().expect("")); + auto subscriber = + iox::optional>(sut->subscriber_builder().create().expect("")); + auto publisher = + iox::optional>(sut->publisher_builder().create().expect("")); + + sut.reset(); + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().open(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().create(); + ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeCreateError::AlreadyExists)); + } + publisher.reset(); + + sut = iox::optional>( + node.service_builder(service_name).template publish_subscribe().open().expect("")); + publisher = iox::optional>(sut->publisher_builder().create().expect("")); + publisher->send_copy(payload).expect(""); + auto sample = subscriber->receive().expect(""); + ASSERT_THAT(sample->payload(), Eq(payload)); + + publisher.reset(); + sut.reset(); + subscriber.reset(); + + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().open(); + ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeOpenError::DoesNotExist)); + } + { + auto temp_sut = node.service_builder(service_name).template publish_subscribe().create(); + ASSERT_THAT(temp_sut.has_value(), Eq(true)); + } +} + } // namespace