Skip to content
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
176 changes: 0 additions & 176 deletions roslibrust_ros2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,179 +264,3 @@ impl roslibrust_common::ServiceProvider for ZenohClient {
Ok(ZenohServiceServer { cancellation_token })
}
}

#[cfg(test)]
mod tests {

#[cfg(feature = "ros2_zenoh_test")]
mod integration_tests {
use crate::ZenohClient;
use ros_z::context::ZContext;
use roslibrust_common::traits::*;

fn make_test_context() -> ZContext {
use ros_z::context::ZContextBuilder;
use ros_z::Builder;

ZContextBuilder::default()
.with_domain_id(0)
.with_connect_endpoints(["tcp/[::]:7447"])
.build()
.unwrap()
}

#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_subscribe_basic() {
let ctx = make_test_context();
let client = ZenohClient::new(&ctx, "test_subscribe_basic_node")
.await
.unwrap();
let mut subscriber = client
.subscribe::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

#[allow(clippy::zombie_processes)]
let mut pub_cmd = std::process::Command::new("ros2")
.arg("topic")
.arg("pub")
.arg("-t")
.arg("10")
.arg("/chatter")
.arg("std_msgs/msg/String")
.arg("data: Hello World")
.spawn()
.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
let msg = subscriber.next().await.unwrap();
assert_eq!(msg.data, "Hello World");
})
.await
.unwrap();

pub_cmd.kill().unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn test_pubsub_basic() {
let ctx = make_test_context();
let client = ZenohClient::new(&ctx, "test_publish_basic_node")
.await
.unwrap();

let publisher = client
.advertise::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

let mut subscriber = client
.subscribe::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

let msg = roslibrust_test::ros2::std_msgs::String {
data: "Hello World".to_string(),
};

publisher.publish(&msg).await.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
let msg = subscriber.next().await.unwrap();
assert_eq!(msg.data, "Hello World");
})
.await
.expect("Failed to receive message within 2 seconds");
}

#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_service_server_callable() {
let ctx = make_test_context();
let client = ZenohClient::new(&ctx, "test_service_server_callable_node")
.await
.unwrap();

let state = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let state_copy = state.clone();
let server_fn = move |request: roslibrust_test::ros2::std_srvs::SetBoolRequest| {
state_copy.store(request.data, std::sync::atomic::Ordering::SeqCst);
Ok(roslibrust_test::ros2::std_srvs::SetBoolResponse {
message: "You set my bool!".to_string(),
success: request.data,
})
};

let _service = client
.advertise_service::<roslibrust_test::ros2::std_srvs::SetBool, _>(
"/test_service_server_callable_node/set_bool",
server_fn,
)
.await
.unwrap();

#[allow(clippy::zombie_processes)]
let mut srv_call_cmd = std::process::Command::new("ros2")
.arg("service")
.arg("call")
.arg("/test_service_server_callable_node/set_bool")
.arg("std_srvs/srv/SetBool")
.arg("data: true")
.spawn()
.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
while !state.load(std::sync::atomic::Ordering::SeqCst) {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
})
.await
.expect("Bool should be set true within 2 seconds");

srv_call_cmd.kill().unwrap()
}

#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_service_zenoh_to_zenoh() {
let ctx = make_test_context();
let node = ZenohClient::new(&ctx, "test_service_server_zenoh")
.await
.unwrap();

let state = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let state_copy = state.clone();

let server_fn = move |request: roslibrust_test::ros2::std_srvs::SetBoolRequest| {
state_copy.store(request.data, std::sync::atomic::Ordering::SeqCst);
Ok(roslibrust_test::ros2::std_srvs::SetBoolResponse {
message: "You set my bool!".to_string(),
success: request.data,
})
};

let _service = node
.advertise_service::<roslibrust_test::ros2::std_srvs::SetBool, _>(
"/test_service_zenoh_to_zenoh_set_bool",
server_fn,
)
.await
.unwrap();

tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

let response = node
.call_service::<roslibrust_test::ros2::std_srvs::SetBool>(
"/test_service_zenoh_to_zenoh_set_bool",
roslibrust_test::ros2::std_srvs::SetBoolRequest { data: true },
)
.await
.expect("Service call should succeed");

assert!(response.success);
assert_eq!(response.message, "You set my bool!");
assert!(state.load(std::sync::atomic::Ordering::SeqCst));
}
}
}
11 changes: 11 additions & 0 deletions roslibrust_ros2/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use ros_z::context::ZContext;
use ros_z::context::ZContextBuilder;
use ros_z::Builder;

pub fn make_test_context() -> ZContext {
ZContextBuilder::default()
.with_domain_id(0)
.with_connect_endpoints(["tcp/[::]:7447"])
.build()
.unwrap()
}
37 changes: 37 additions & 0 deletions roslibrust_ros2/tests/test_pubsub_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![cfg(feature = "ros2_zenoh_test")]

mod common;

use roslibrust_common::traits::*;
use roslibrust_ros2::ZenohClient;

#[tokio::test(flavor = "multi_thread")]
async fn test_pubsub_basic() {
let ctx = common::make_test_context();
let client = ZenohClient::new(&ctx, "test_publish_basic_node")
.await
.unwrap();

let publisher = client
.advertise::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

let mut subscriber = client
.subscribe::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

let msg = roslibrust_test::ros2::std_msgs::String {
data: "Hello World".to_string(),
};

publisher.publish(&msg).await.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
let msg = subscriber.next().await.unwrap();
assert_eq!(msg.data, "Hello World");
})
.await
.expect("Failed to receive message within 2 seconds");
}
52 changes: 52 additions & 0 deletions roslibrust_ros2/tests/test_service_server_callable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![cfg(feature = "ros2_zenoh_test")]

mod common;

use roslibrust_common::traits::*;
use roslibrust_ros2::ZenohClient;

#[tokio::test(flavor = "multi_thread")]
async fn test_service_server_callable() {
let ctx = common::make_test_context();
let client = ZenohClient::new(&ctx, "test_service_server_callable_node")
.await
.unwrap();

let state = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let state_copy = state.clone();
let server_fn = move |request: roslibrust_test::ros2::std_srvs::SetBoolRequest| {
state_copy.store(request.data, std::sync::atomic::Ordering::SeqCst);
Ok(roslibrust_test::ros2::std_srvs::SetBoolResponse {
message: "You set my bool!".to_string(),
success: request.data,
})
};

let _service = client
.advertise_service::<roslibrust_test::ros2::std_srvs::SetBool, _>(
"/test_service_server_callable_node/set_bool",
server_fn,
)
.await
.unwrap();

#[allow(clippy::zombie_processes)]
let mut srv_call_cmd = std::process::Command::new("ros2")
.arg("service")
.arg("call")
.arg("/test_service_server_callable_node/set_bool")
.arg("std_srvs/srv/SetBool")
.arg("data: true")
.spawn()
.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
while !state.load(std::sync::atomic::Ordering::SeqCst) {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
})
.await
.expect("Bool should be set true within 2 seconds");

srv_call_cmd.kill().unwrap()
}
47 changes: 47 additions & 0 deletions roslibrust_ros2/tests/test_service_zenoh_to_zenoh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![cfg(feature = "ros2_zenoh_test")]

mod common;

use roslibrust_common::traits::*;
use roslibrust_ros2::ZenohClient;

#[tokio::test(flavor = "multi_thread")]
async fn test_service_zenoh_to_zenoh() {
let ctx = common::make_test_context();
let node = ZenohClient::new(&ctx, "test_service_server_zenoh")
.await
.unwrap();

let state = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let state_copy = state.clone();

let server_fn = move |request: roslibrust_test::ros2::std_srvs::SetBoolRequest| {
state_copy.store(request.data, std::sync::atomic::Ordering::SeqCst);
Ok(roslibrust_test::ros2::std_srvs::SetBoolResponse {
message: "You set my bool!".to_string(),
success: request.data,
})
};

let _service = node
.advertise_service::<roslibrust_test::ros2::std_srvs::SetBool, _>(
"/test_service_zenoh_to_zenoh_set_bool",
server_fn,
)
.await
.unwrap();

tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

let response = node
.call_service::<roslibrust_test::ros2::std_srvs::SetBool>(
"/test_service_zenoh_to_zenoh_set_bool",
roslibrust_test::ros2::std_srvs::SetBoolRequest { data: true },
)
.await
.expect("Service call should succeed");

assert!(response.success);
assert_eq!(response.message, "You set my bool!");
assert!(state.load(std::sync::atomic::Ordering::SeqCst));
}
39 changes: 39 additions & 0 deletions roslibrust_ros2/tests/test_subscribe_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![cfg(feature = "ros2_zenoh_test")]

mod common;

use roslibrust_common::traits::*;
use roslibrust_ros2::ZenohClient;

#[tokio::test(flavor = "multi_thread")]
async fn test_subscribe_basic() {
let ctx = common::make_test_context();
let client = ZenohClient::new(&ctx, "test_subscribe_basic_node")
.await
.unwrap();
let mut subscriber = client
.subscribe::<roslibrust_test::ros2::std_msgs::String>("/chatter")
.await
.unwrap();

#[allow(clippy::zombie_processes)]
let mut pub_cmd = std::process::Command::new("ros2")
.arg("topic")
.arg("pub")
.arg("-t")
.arg("10")
.arg("/chatter")
.arg("std_msgs/msg/String")
.arg("data: Hello World")
.spawn()
.unwrap();

tokio::time::timeout(tokio::time::Duration::from_secs(2), async {
let msg = subscriber.next().await.unwrap();
assert_eq!(msg.data, "Hello World");
})
.await
.unwrap();

pub_cmd.kill().unwrap();
}
Loading