From dd47d76bc5694c563a96cabb1a670a5990c6ea6d Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Tue, 21 Jun 2022 00:10:57 +0200 Subject: [PATCH] iox-#5 Basic pub sub test --- .github/workflows/rust.yml | 2 +- Cargo.toml | 3 +++ src/lib.rs | 3 +++ src/tests/basic_pub_sub.rs | 49 ++++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 6 +++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/tests/basic_pub_sub.rs create mode 100644 src/tests/mod.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 79107c4..db49cc1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -32,4 +32,4 @@ jobs: - name: Build run: cargo build --all --examples - name: Run tests - run: cargo test + run: cargo test -- --test-threads=1 # prevent running multiple RouDiEnvironments in parallel diff --git a/Cargo.toml b/Cargo.toml index 2505647..845a10d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,6 @@ cpp_build = "0.5" [dependencies] cpp = "0.5" thiserror = "1.0" + +[dev-dependencies] +anyhow = "1.0" diff --git a/src/lib.rs b/src/lib.rs index f5b9efe..d9610cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,3 +18,6 @@ pub mod sb; // re-export structs pub use error::IceOryxError; pub use runtime::Runtime; + +#[cfg(test)] +mod tests; diff --git a/src/tests/basic_pub_sub.rs b/src/tests/basic_pub_sub.rs new file mode 100644 index 0000000..cf0aa02 --- /dev/null +++ b/src/tests/basic_pub_sub.rs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +// SPDX-FileContributor: Mathias Kraus + +use crate::pb::{self, POD}; +use crate::sb; +use crate::testing::RouDiEnvironment; +use crate::Runtime; + +use anyhow::{anyhow, Result}; + +#[repr(C)] +struct CounterTopic { + counter: u32, +} + +unsafe impl POD for CounterTopic {} + +#[test] +fn basic_pub_sub() -> Result<()> { + let _roudi = RouDiEnvironment::new(); + + Runtime::init("basic_pub_sub"); + + let topic = sb::TopicBuilder::::new("Test", "BasicPubSub", "Counter") + .queue_capacity(5) + .build(); + + let (subscriber, sample_receive_token) = topic.subscribe(); + + let topic = pb::TopicBuilder::::new("Test", "BasicPubSub", "Counter").build()?; + + let publisher = topic.offer(); + let mut sample = publisher.allocate_sample()?; + const SEND_COUNTER: u32 = 42; + sample.counter = SEND_COUNTER; + publisher.publish(sample); + + let sample_receiver = subscriber.get_sample_receiver(sample_receive_token); + + assert!(sample_receiver.has_samples()); + + match sample_receiver.get_sample() { + Some(sample) => assert_eq!(sample.counter, SEND_COUNTER), + _ => return Err(anyhow!("Could not read sample")), + } + + Ok(()) +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..abb89a6 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +// SPDX-FileContributor: Mathias Kraus + +// minimal setup with one publisher and one subscriber exchanging data +mod basic_pub_sub;