use std::{thread, time::Duration};
use r2r::QosProfile;
const N_NODE_PER_CONTEXT: usize = 5;
const N_CONCURRENT_ROS_CONTEXT: usize = 2;
const N_TEARDOWN_CYCLES: usize = 2;
#[test]
fn doesnt_crash() -> Result<(), Box<dyn std::error::Error>> {
let threads = (0..N_CONCURRENT_ROS_CONTEXT).map(|i_context| {
std::thread::spawn(move || {
for _i_cycle in 0..N_TEARDOWN_CYCLES {
let ctx = r2r::Context::create().unwrap();
for c in 0..10 {
let mut ths = Vec::new();
for i_node in 0..N_NODE_PER_CONTEXT {
let ctx = ctx.clone();
ths.push(thread::spawn(move || {
let mut node = r2r::Node::create(
ctx,
&format!("testnode_{}_{}", i_context, i_node),
"",
)
.unwrap();
for _j in 0..10 {
let p = node
.create_publisher::<r2r::std_msgs::msg::String>(
&format!("/r2r{}", i_node),
QosProfile::default(),
)
.unwrap();
let to_send = r2r::std_msgs::msg::String {
data: format!("[node{}]: {}", i_node, c),
};
thread::spawn(move || loop {
let res = p.publish(&to_send);
thread::sleep(Duration::from_millis(1));
match res {
Ok(_) => (),
Err(_) => {
break;
}
}
});
}
for _j in 0..100 {
node.spin_once(Duration::from_millis(10));
}
}));
}
for t in ths {
t.join().unwrap();
}
}
}
})
});
for thread in threads.into_iter() {
thread.join().unwrap();
}
Ok(())
}