Skip to content

Commit

Permalink
Merge pull request #494 from elfenpiff/iox2-493-add-tests
Browse files Browse the repository at this point in the history
[#493] add tests
  • Loading branch information
elfenpiff authored Oct 30, 2024
2 parents 98a4142 + c72fd7c commit 336513d
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 0 deletions.
84 changes: 84 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/service_event_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SERVICE_TYPE>().expect("");
auto sut =
iox::optional<PortFactoryEvent<SERVICE_TYPE>>(node.service_builder(service_name).event().create().expect(""));
auto listener = iox::optional<Listener<SERVICE_TYPE>>(sut->listener_builder().create().expect(""));
auto notifier = iox::optional<Notifier<SERVICE_TYPE>>(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<PortFactoryEvent<SERVICE_TYPE>>(node.service_builder(service_name).event().open().expect(""));
listener = iox::optional<Listener<SERVICE_TYPE>>(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<SERVICE_TYPE>().expect("");
auto sut =
iox::optional<PortFactoryEvent<SERVICE_TYPE>>(node.service_builder(service_name).event().create().expect(""));
auto listener = iox::optional<Listener<SERVICE_TYPE>>(sut->listener_builder().create().expect(""));
auto notifier = iox::optional<Notifier<SERVICE_TYPE>>(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<PortFactoryEvent<SERVICE_TYPE>>(node.service_builder(service_name).event().open().expect(""));
notifier = iox::optional<Notifier<SERVICE_TYPE>>(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
91 changes: 91 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SERVICE_TYPE>().expect("");
auto sut = iox::optional<PortFactoryPublishSubscribe<SERVICE_TYPE, uint64_t, void>>(
node.service_builder(service_name).template publish_subscribe<uint64_t>().create().expect(""));
auto subscriber =
iox::optional<Subscriber<SERVICE_TYPE, uint64_t, void>>(sut->subscriber_builder().create().expect(""));
auto publisher =
iox::optional<Publisher<SERVICE_TYPE, uint64_t, void>>(sut->publisher_builder().create().expect(""));

sut.reset();
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().open();
ASSERT_THAT(temp_sut.has_value(), Eq(true));
}
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().create();
ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeCreateError::AlreadyExists));
}
subscriber.reset();

sut = iox::optional<PortFactoryPublishSubscribe<SERVICE_TYPE, uint64_t, void>>(
node.service_builder(service_name).template publish_subscribe<uint64_t>().open().expect(""));
subscriber = iox::optional<Subscriber<SERVICE_TYPE, uint64_t, void>>(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<uint64_t>().open();
ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeOpenError::DoesNotExist));
}
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().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<SERVICE_TYPE>().expect("");
auto sut = iox::optional<PortFactoryPublishSubscribe<SERVICE_TYPE, uint64_t, void>>(
node.service_builder(service_name).template publish_subscribe<uint64_t>().create().expect(""));
auto subscriber =
iox::optional<Subscriber<SERVICE_TYPE, uint64_t, void>>(sut->subscriber_builder().create().expect(""));
auto publisher =
iox::optional<Publisher<SERVICE_TYPE, uint64_t, void>>(sut->publisher_builder().create().expect(""));

sut.reset();
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().open();
ASSERT_THAT(temp_sut.has_value(), Eq(true));
}
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().create();
ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeCreateError::AlreadyExists));
}
publisher.reset();

sut = iox::optional<PortFactoryPublishSubscribe<SERVICE_TYPE, uint64_t, void>>(
node.service_builder(service_name).template publish_subscribe<uint64_t>().open().expect(""));
publisher = iox::optional<Publisher<SERVICE_TYPE, uint64_t, void>>(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<uint64_t>().open();
ASSERT_THAT(temp_sut.error(), Eq(PublishSubscribeOpenError::DoesNotExist));
}
{
auto temp_sut = node.service_builder(service_name).template publish_subscribe<uint64_t>().create();
ASSERT_THAT(temp_sut.has_value(), Eq(true));
}
}

} // namespace
76 changes: 76 additions & 0 deletions iceoryx2/tests/service_event_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sut: Service>() {
let event_id = EventId::new(76);
let service_name = generate_name();
let config = generate_isolated_config();
let node = NodeBuilder::new().config(&config).create::<Sut>().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<Sut: Service>() {
let event_id = EventId::new(93);
let service_name = generate_name();
let config = generate_isolated_config();
let node = NodeBuilder::new().config(&config).create::<Sut>().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<Sut: Service>() {
let _watch_dog = Watchdog::new();
Expand Down
108 changes: 108 additions & 0 deletions iceoryx2/tests/service_publish_subscribe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,114 @@ mod service_publish_subscribe {
);
}

#[test]
fn service_can_be_opened_when_there_is_a_publisher<Sut: Service>() {
let payload = 1809723987;
let service_name = generate_name();
let config = generate_isolated_config();
let node = NodeBuilder::new().config(&config).create::<Sut>().unwrap();
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.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::<u64>()
.open();
assert_that!(sut, is_ok);
drop(sut);
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.create();
assert_that!(sut.err().unwrap(), eq PublishSubscribeCreateError::AlreadyExists);
drop(subscriber);

let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.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::<u64>()
.open();
assert_that!(sut.err().unwrap(), eq PublishSubscribeOpenError::DoesNotExist);
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.create();
assert_that!(sut, is_ok);
}

#[test]
fn service_can_be_opened_when_there_is_a_subscriber<Sut: Service>() {
let payload = 59123544;
let service_name = generate_name();
let config = generate_isolated_config();
let node = NodeBuilder::new().config(&config).create::<Sut>().unwrap();
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.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::<u64>()
.open();
assert_that!(sut, is_ok);
drop(sut);
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.create();
assert_that!(sut.err().unwrap(), eq PublishSubscribeCreateError::AlreadyExists);
drop(publisher);

let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.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::<u64>()
.open();
assert_that!(sut.err().unwrap(), eq PublishSubscribeOpenError::DoesNotExist);
let sut = node
.service_builder(&service_name)
.publish_subscribe::<u64>()
.create();
assert_that!(sut, is_ok);
}

#[test]
fn subscriber_can_decrease_buffer_size<Sut: Service>() {
const BUFFER_SIZE: usize = 16;
Expand Down

0 comments on commit 336513d

Please sign in to comment.