Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iox-#5 Add RouDiEnvironment for testing #22

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ cpp_build = "0.5"
[dependencies]
cpp = "0.5"
thiserror = "1.0"

[dev-dependencies]
anyhow = "1.0"
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -136,10 +137,16 @@ 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");

#[cfg(target_os = "linux")]
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"))]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ pub mod sb;
// re-export structs
pub use error::IceOryxError;
pub use runtime::Runtime;

#[cfg(test)]
mod testing;
#[cfg(test)]
mod tests;
8 changes: 8 additions & 0 deletions src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions src/testing/roudi_environment_ffi.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
unsafe {
let raw = cpp!([] -> *mut RouDiEnvironment as "RouDiEnvironment*"
{
return new RouDiEnvironment();
});

Box::from_raw(raw)
}
}
}
50 changes: 50 additions & 0 deletions src/tests/basic_pub_sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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();
dkroenke marked this conversation as resolved.
Show resolved Hide resolved

Runtime::init("basic_pub_sub");

let topic = sb::TopicBuilder::<CounterTopic>::new("Test", "BasicPubSub", "Counter")
.queue_capacity(5)
.build();

let (subscriber, sample_receive_token) = topic.subscribe();

let topic = pb::TopicBuilder::<CounterTopic>::new("Test", "BasicPubSub", "Counter").build()?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, unfortunately the subscribe/offer on create feature broke this. One could think of something like

let topic = TopicBuilder::<CounterTopic>::new("Test", "BasicPubSub", "Counter").build()?;
let (subscriber, sample_receive_token) = topic.subscriber().history_request(1).create();
let publisher = topic.publisher().history_capacity(5).create();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... oh, and ideally it would be TopicBuilder::<CounterTopic>::new("/Test/BasicPubSub/Counter").build()?; ... maybe for the 0.2 release :)


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);

elBoberido marked this conversation as resolved.
Show resolved Hide resolved
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(())
}
6 changes: 6 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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;