From 8602ced50cb60555cd211cdf459372404f5673d1 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Mon, 20 Jun 2022 22:41:58 +0200 Subject: [PATCH 1/4] iox-#5 Add RouDiEnvironment for testing --- build.rs | 7 +++++++ src/lib.rs | 1 + src/testing/mod.rs | 8 ++++++++ src/testing/roudi_environment_ffi.rs | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/testing/mod.rs create mode 100644 src/testing/roudi_environment_ffi.rs diff --git a/build.rs b/build.rs index 61513ab..ad95cfd 100644 --- a/build.rs +++ b/build.rs @@ -32,6 +32,7 @@ fn make_and_install(source_dir: &str, build_dir: &str, install_dir: &str) -> std .args(&[ "-DCMAKE_BUILD_TYPE=Release", "-DBUILD_SHARED_LIBS=OFF", + "-DROUDI_ENVIRONMENT=ON", &cmake_prefix_path, &cmake_install_prefix, &component_source_dir, @@ -136,14 +137,20 @@ fn main() -> std::io::Result<()> { println!("cargo:rustc-link-search={}", iceoryx_lib_dir); + println!("cargo:rustc-link-lib=iceoryx_posh_testing"); + println!("cargo:rustc-link-lib=iceoryx_posh_roudi"); println!("cargo:rustc-link-lib=iceoryx_posh"); println!("cargo:rustc-link-lib=iceoryx_hoofs"); println!("cargo:rustc-link-lib=iceoryx_platform"); + + println!("cargo:rustc-link-lib=acl"); + #[cfg(not(any(target_os = "windows", target_os = "macos")))] println!("cargo:rustc-link-lib=stdc++"); #[cfg(any(target_os = "macos"))] println!("cargo:rustc-link-lib=c++"); + Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 333f678..f5b9efe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate cpp; mod error; mod runtime; +mod testing; pub mod introspection; pub mod pb; diff --git a/src/testing/mod.rs b/src/testing/mod.rs new file mode 100644 index 0000000..7ba9d94 --- /dev/null +++ b/src/testing/mod.rs @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +// SPDX-FileContributor: Mathias Kraus + +mod roudi_environment_ffi; + +// re-exports +pub (crate) use roudi_environment_ffi::RouDiEnvironment; diff --git a/src/testing/roudi_environment_ffi.rs b/src/testing/roudi_environment_ffi.rs new file mode 100644 index 0000000..76d5995 --- /dev/null +++ b/src/testing/roudi_environment_ffi.rs @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: © Contributors to the iceoryx-rs project +// SPDX-FileContributor: Mathias Kraus + +cpp! {{ + #include "iceoryx_posh/testing/roudi_environment/roudi_environment.hpp" + + using iox::roudi::RouDiEnvironment; +}} + +cpp_class!(pub unsafe struct RouDiEnvironment as "RouDiEnvironment"); + +impl RouDiEnvironment { + pub(crate) fn new() -> Box { + unsafe { + let raw = cpp!([] -> *mut RouDiEnvironment as "RouDiEnvironment*" + { + return new RouDiEnvironment(); + }); + + Box::from_raw(raw) + } + } +} From da8771b6025d6e08ea19723166766cdf30f4c2db Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Tue, 21 Jun 2022 00:10:57 +0200 Subject: [PATCH 2/4] 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; From 68629712e847e3166f89f6e1dfda76be1469a906 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Tue, 21 Jun 2022 00:17:02 +0200 Subject: [PATCH 3/4] iox-#5 Fix macOS build --- build.rs | 2 +- src/testing/mod.rs | 2 +- src/tests/basic_pub_sub.rs | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index ad95cfd..5cefa6b 100644 --- a/build.rs +++ b/build.rs @@ -144,6 +144,7 @@ fn main() -> std::io::Result<()> { println!("cargo:rustc-link-lib=iceoryx_hoofs"); println!("cargo:rustc-link-lib=iceoryx_platform"); + #[cfg(target_os = "linux")] println!("cargo:rustc-link-lib=acl"); #[cfg(not(any(target_os = "windows", target_os = "macos")))] @@ -151,6 +152,5 @@ fn main() -> std::io::Result<()> { #[cfg(any(target_os = "macos"))] println!("cargo:rustc-link-lib=c++"); - Ok(()) } diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 7ba9d94..473a0ba 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -5,4 +5,4 @@ mod roudi_environment_ffi; // re-exports -pub (crate) use roudi_environment_ffi::RouDiEnvironment; +pub(crate) use roudi_environment_ffi::RouDiEnvironment; diff --git a/src/tests/basic_pub_sub.rs b/src/tests/basic_pub_sub.rs index cf0aa02..c72b97c 100644 --- a/src/tests/basic_pub_sub.rs +++ b/src/tests/basic_pub_sub.rs @@ -32,6 +32,7 @@ fn basic_pub_sub() -> Result<()> { let publisher = topic.offer(); let mut sample = publisher.allocate_sample()?; + const SEND_COUNTER: u32 = 42; sample.counter = SEND_COUNTER; publisher.publish(sample); From a9ba2f7af3bdf7b1f112b58abfb08d8a28348d30 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Tue, 21 Jun 2022 22:39:05 +0200 Subject: [PATCH 4/4] iox-#5 Build testing module only for tests --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d9610cf..517c34c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ extern crate cpp; mod error; mod runtime; -mod testing; pub mod introspection; pub mod pb; @@ -19,5 +18,7 @@ pub mod sb; pub use error::IceOryxError; pub use runtime::Runtime; +#[cfg(test)] +mod testing; #[cfg(test)] mod tests;