Skip to content

Commit

Permalink
iox-#5 Basic pub sub test
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Jun 20, 2022
1 parent 1e729d2 commit 24213dd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
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"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub mod sb;
// re-export structs
pub use error::IceOryxError;
pub use runtime::Runtime;

#[cfg(test)]
mod tests;
49 changes: 49 additions & 0 deletions src/tests/basic_pub_sub.rs
Original file line number Diff line number Diff line change
@@ -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::<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()?;

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

0 comments on commit 24213dd

Please sign in to comment.