Skip to content

Commit

Permalink
[eclipse-iceoryx#133] Move Sample, SampleMut tests in its own test fi…
Browse files Browse the repository at this point in the history
…les; add SampleMut tests
  • Loading branch information
elfenpiff committed Mar 6, 2024
1 parent 4565ada commit 299de2f
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 126 deletions.
161 changes: 161 additions & 0 deletions iceoryx2/tests/sample_mut_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[generic_tests::define]
mod sample_mut {
use iceoryx2::port::publisher::Publisher;
use iceoryx2::port::subscriber::Subscriber;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::publish_subscribe::PortFactory;
use iceoryx2::service::Service;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_bb_testing::assert_that;

const MAX_LOANED_SAMPLES: usize = 5;

fn generate_name() -> ServiceName {
ServiceName::new(&format!(
"service_tests_{}",
UniqueSystemId::new().unwrap().value()
))
.unwrap()
}

struct TestFixture<Sut: Service> {
service_name: ServiceName,
service: PortFactory<Sut, u64>,
publisher: Publisher<Sut, u64>,
subscriber: Subscriber<Sut, u64>,
}

impl<Sut: Service> TestFixture<Sut> {
fn new() -> Self {
let service_name = generate_name();
let service = Sut::new(&service_name)
.publish_subscribe()
.create::<u64>()
.unwrap();

let publisher = service
.publisher()
.max_loaned_samples(MAX_LOANED_SAMPLES)
.create()
.unwrap();

let subscriber = service.subscriber().create().unwrap();

Self {
service_name,
service,
publisher,
subscriber,
}
}

fn publisher(&self) -> &Publisher<Sut, u64> {
&self.publisher
}

fn subscriber(&self) -> &Subscriber<Sut, u64> {
&self.subscriber
}
}

#[test]
fn when_going_out_of_scope_it_is_released<Sut: Service>() {
let test = TestFixture::<Sut>::new();

let mut sample_vec = vec![];

for _ in 0..4 {
while let Ok(sample) = test.publisher().loan() {
sample_vec.push(sample);
}

assert_that!(sample_vec, len MAX_LOANED_SAMPLES);
sample_vec.clear();
}
}

#[test]
fn header_tracks_correct_origin<Sut: Service>() {
let test = TestFixture::<Sut>::new();
let sample = test.publisher().loan().unwrap();
assert_that!(sample.header().publisher_id(), eq test.publisher().id());
}

#[test]
fn write_payload_works<Sut: Service>() {
const PAYLOAD_1: u64 = 891283689123555;
const PAYLOAD_2: u64 = 71820;
let test = TestFixture::<Sut>::new();
let sample = test.publisher().loan_uninit().unwrap();
let mut sample = sample.write_payload(PAYLOAD_1);

assert_that!(*sample.payload(), eq PAYLOAD_1);
assert_that!(*sample.payload_mut(), eq PAYLOAD_1);

*sample.payload_mut() = PAYLOAD_2;

assert_that!(*sample.payload(), eq PAYLOAD_2);
assert_that!(*sample.payload_mut(), eq PAYLOAD_2);
}

#[test]
fn assume_init_works<Sut: Service>() {
const PAYLOAD: u64 = 7182055123;
let test = TestFixture::<Sut>::new();
let mut sample = test.publisher().loan_uninit().unwrap();
let _ = *sample.payload_mut().write(PAYLOAD);
let mut sample = unsafe { sample.assume_init() };

assert_that!(*sample.payload(), eq PAYLOAD);
assert_that!(*sample.payload_mut(), eq PAYLOAD);
}

#[test]
fn send_works<Sut: Service>() {
const PAYLOAD: u64 = 3215357;
let test = TestFixture::<Sut>::new();
let sample = test.publisher().loan_uninit().unwrap();
let sample = sample.write_payload(PAYLOAD);

assert_that!(sample.send(), eq Ok(1));

let received_sample = test.subscriber().receive().unwrap().unwrap();
assert_that!(*received_sample, eq PAYLOAD);
}

#[test]
fn sample_of_dropped_service_does_not_block_new_service_creation<Sut: Service>() {
//TODO: sample_mut_from_dropped_publisher
//TODO: drop tests for events
//TODO: refactor sample tests with TestFixture like SampleMut tests

let test = TestFixture::<Sut>::new();
let service_name = test.service_name.clone();
let _sample = test.publisher().loan_uninit().unwrap();

drop(test);

assert_that!(
Sut::new(&service_name).publish_subscribe().create::<u64>(),
is_ok
);
}

#[instantiate_tests(<iceoryx2::service::zero_copy::Service>)]
mod zero_copy {}

#[instantiate_tests(<iceoryx2::service::process_local::Service>)]
mod process_local {}
}
156 changes: 156 additions & 0 deletions iceoryx2/tests/sample_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[generic_tests::define]
mod sample {
use iceoryx2::prelude::*;
use iceoryx2::service::Service;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_bb_testing::assert_that;

fn generate_name() -> ServiceName {
ServiceName::new(&format!(
"service_tests_{}",
UniqueSystemId::new().unwrap().value()
))
.unwrap()
}

#[test]
fn origin_is_tracked_correctly<Sut: Service>() {
let service_name = generate_name();
let sut = Sut::new(&service_name)
.publish_subscribe()
.create::<u64>()
.unwrap();

let publisher_1 = sut.publisher().create().unwrap();
let publisher_2 = sut.publisher().create().unwrap();
let subscriber = sut.subscriber().create().unwrap();

assert_that!(publisher_1.send_copy(123), eq Ok(1));
let sample = subscriber.receive().unwrap().unwrap();
assert_that!(sample.origin(), eq publisher_1.id());

assert_that!(publisher_2.send_copy(123), eq Ok(1));
let sample = subscriber.receive().unwrap().unwrap();
assert_that!(sample.origin(), eq publisher_2.id());
}

#[test]
fn sample_of_dropped_service_does_not_block_new_service_creation<Sut: Service>() {
let service_name = generate_name();
let sut = Sut::new(&service_name)
.publish_subscribe()
.create::<u64>()
.unwrap();

let subscriber = sut.subscriber().create().unwrap();
let publisher = sut.publisher().create().unwrap();

assert_that!(publisher.send_copy(5), eq Ok(1));
let sample = subscriber.receive().unwrap();
assert_that!(sample, is_some);

drop(sut);
drop(subscriber);
drop(publisher);

assert_that!(
Sut::new(&service_name).publish_subscribe().create::<u64>(),
is_ok
);
}

#[test]
fn when_everything_is_dropped_the_sample_can_still_be_consumed<Sut: Service>() {
let service_name = generate_name();
let sut = Sut::new(&service_name)
.publish_subscribe()
.create::<u64>()
.unwrap();

let subscriber = sut.subscriber().create().unwrap();
let publisher = sut.publisher().create().unwrap();

drop(sut);

const PAYLOAD: u64 = 8761238679123;

assert_that!(publisher.send_copy(PAYLOAD), eq Ok(1));
let sample = subscriber.receive().unwrap().unwrap();

drop(subscriber);
drop(publisher);

assert_that!(*sample, eq PAYLOAD);
}

#[test]
fn sample_received_from_dropped_publisher_does_not_block_new_publishers<Sut: Service>() {
let service_name = generate_name();
let sut = Sut::new(&service_name)
.publish_subscribe()
.max_publishers(1)
.create::<u64>()
.unwrap();

let subscriber = sut.subscriber().create().unwrap();
let publisher = sut.publisher().create().unwrap();

assert_that!(publisher.send_copy(123554), eq Ok(1));
let sample = subscriber.receive().unwrap().unwrap();

drop(publisher);

const PAYLOAD: u64 = 123981235645;

let publisher = sut.publisher().create().unwrap();
assert_that!(publisher.send_copy(PAYLOAD), eq Ok(1));
assert_that!(*sample, eq 123554);
let sample_2 = subscriber.receive().unwrap().unwrap();
assert_that!(*sample_2, eq PAYLOAD);
}

#[test]
fn sample_from_dropped_subscriber_does_not_block_new_subscribers<Sut: Service>() {
let service_name = generate_name();
let sut = Sut::new(&service_name)
.publish_subscribe()
.max_subscribers(1)
.history_size(0)
.create::<u64>()
.unwrap();

let subscriber = sut.subscriber().create().unwrap();
let publisher = sut.publisher().create().unwrap();

assert_that!(publisher.send_copy(1234), eq Ok(1));
let sample = subscriber.receive().unwrap().unwrap();

drop(subscriber);

const PAYLOAD: u64 = 123666645;

let subscriber = sut.subscriber().create().unwrap();
assert_that!(publisher.send_copy(PAYLOAD), eq Ok(1));
let sample_2 = subscriber.receive().unwrap().unwrap();
assert_that!(*sample, eq 1234);
assert_that!(*sample_2, eq PAYLOAD);
}

#[instantiate_tests(<iceoryx2::service::zero_copy::Service>)]
mod zero_copy {}

#[instantiate_tests(<iceoryx2::service::process_local::Service>)]
mod process_local {}
}
Loading

0 comments on commit 299de2f

Please sign in to comment.