Skip to content

Commit

Permalink
Merge pull request #383 from maspe36/remove_rclrs_tests
Browse files Browse the repository at this point in the history
Move the tests in the `rclrs_tests` crate into `rclrs`.
  • Loading branch information
jhdcs authored Mar 26, 2024
2 parents 996c091 + 42e0b5c commit 12bca36
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 403 deletions.
5 changes: 2 additions & 3 deletions rclrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ libloading = { version = "0.8", optional = true }
# Needed for the Message trait, among others
rosidl_runtime_rs = "0.4"

[dependencies.builtin_interfaces]
version = "*"

[dev-dependencies]
# Needed for e.g. writing yaml files in tests
tempfile = "3.3.0"
# Needed for publisher and subscriber tests
test_msgs = {version = "*"}

[build-dependencies]
# Needed for FFI
Expand Down
2 changes: 2 additions & 0 deletions rclrs/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<depend>builtin_interfaces</depend>
<depend>rcl_interfaces</depend>
<depend>rosgraph_msgs</depend>

<test_depend>test_msgs</test_depend>

<export>
<build_type>ament_cargo</build_type>
Expand Down
35 changes: 35 additions & 0 deletions rclrs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,38 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use test_msgs::srv;

#[test]
fn traits() {
assert_send::<Client<srv::Arrays>>();
assert_sync::<Client<srv::Arrays>>();
}

#[test]
fn test_clients() -> Result<(), RclrsError> {
let namespace = "/test_clients_graph";
let graph = construct_test_graph(namespace)?;
let _node_2_empty_client = graph
.node2
.create_client::<srv::Empty>("graph_test_topic_4")?;

std::thread::sleep(std::time::Duration::from_millis(200));

let client_names_and_types = graph
.node2
.get_client_names_and_types_by_node(&graph.node2.name(), &graph.node2.namespace())?;
let types = client_names_and_types
.get("/test_clients_graph/graph_test_topic_4")
.unwrap();

assert!(types.contains(&"test_msgs/srv/Empty".to_string()));

Ok(())
}
}
7 changes: 3 additions & 4 deletions rclrs/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ unsafe impl Send for rcl_clock_t {}
mod tests {
use super::*;

fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}

#[test]
fn clock_is_send_and_sync() {
fn traits() {
use crate::test_helpers::*;

assert_send::<Clock>();
assert_sync::<Clock>();
}
Expand Down
7 changes: 3 additions & 4 deletions rclrs/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ impl Context {
mod tests {
use super::*;

fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}

#[test]
fn context_is_send_and_sync() {
fn traits() {
use crate::test_helpers::*;

assert_send::<Context>();
assert_sync::<Context>();
}
Expand Down
7 changes: 3 additions & 4 deletions rclrs/src/dynamic_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,10 @@ impl DynamicMessageMetadata {
mod tests {
use super::*;

fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}

#[test]
fn all_types_are_sync_and_send() {
fn traits() {
use crate::test_helpers::*;

assert_send::<DynamicMessageMetadata>();
assert_sync::<DynamicMessageMetadata>();
}
Expand Down
3 changes: 3 additions & 0 deletions rclrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ mod time_source;
mod vendor;
mod wait;

#[cfg(test)]
mod test_helpers;

mod rcl_bindings;

#[cfg(feature = "dyn_msg")]
Expand Down
51 changes: 47 additions & 4 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,56 @@ pub(crate) unsafe fn call_string_getter_with_handle(
#[cfg(test)]
mod tests {
use super::*;

fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
use crate::test_helpers::*;

#[test]
fn node_is_send_and_sync() {
fn traits() {
assert_send::<Node>();
assert_sync::<Node>();
}

#[test]
fn test_topic_names_and_types() -> Result<(), RclrsError> {
use crate::QOS_PROFILE_SYSTEM_DEFAULT;
use test_msgs::msg;

let graph = construct_test_graph("test_topics_graph")?;

let _node_1_defaults_subscription = graph.node1.create_subscription::<msg::Defaults, _>(
"graph_test_topic_3",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::Defaults| {},
)?;
let _node_2_empty_subscription = graph.node2.create_subscription::<msg::Empty, _>(
"graph_test_topic_1",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::Empty| {},
)?;
let _node_2_basic_types_subscription =
graph.node2.create_subscription::<msg::BasicTypes, _>(
"graph_test_topic_2",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::BasicTypes| {},
)?;

std::thread::sleep(std::time::Duration::from_millis(100));

let topic_names_and_types = graph.node1.get_topic_names_and_types()?;

let types = topic_names_and_types
.get("/test_topics_graph/graph_test_topic_1")
.unwrap();
assert!(types.contains(&"test_msgs/msg/Empty".to_string()));
let types = topic_names_and_types
.get("/test_topics_graph/graph_test_topic_2")
.unwrap();
assert!(types.contains(&"test_msgs/msg/BasicTypes".to_string()));

let types = topic_names_and_types
.get("/test_topics_graph/graph_test_topic_3")
.unwrap();
assert!(types.contains(&"test_msgs/msg/Defaults".to_string()));

Ok(())
}
}
1 change: 0 additions & 1 deletion rclrs/src/node/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ fn convert_names_and_types(

#[cfg(test)]
mod tests {

use super::*;
use crate::Context;

Expand Down
78 changes: 78 additions & 0 deletions rclrs/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,81 @@ impl<'a, T: Message> MessageCow<'a, T> for &'a T {
Cow::Borrowed(self)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;

#[test]
fn traits() {
assert_send::<Publisher<test_msgs::msg::BoundedSequences>>();
assert_sync::<Publisher<test_msgs::msg::BoundedSequences>>();
}

#[test]
fn test_publishers() -> Result<(), RclrsError> {
use crate::TopicEndpointInfo;
use crate::QOS_PROFILE_SYSTEM_DEFAULT;
use test_msgs::msg;

let namespace = "/test_publishers_graph";
let graph = construct_test_graph(namespace)?;

let node_1_empty_publisher = graph
.node1
.create_publisher::<msg::Empty>("graph_test_topic_1", QOS_PROFILE_SYSTEM_DEFAULT)?;
let topic1 = node_1_empty_publisher.topic_name();
let node_1_basic_types_publisher = graph.node1.create_publisher::<msg::BasicTypes>(
"graph_test_topic_2",
QOS_PROFILE_SYSTEM_DEFAULT,
)?;
let topic2 = node_1_basic_types_publisher.topic_name();
let node_2_default_publisher = graph
.node2
.create_publisher::<msg::Defaults>("graph_test_topic_3", QOS_PROFILE_SYSTEM_DEFAULT)?;
let topic3 = node_2_default_publisher.topic_name();

std::thread::sleep(std::time::Duration::from_millis(100));

// Test count_publishers()
assert_eq!(graph.node1.count_publishers(&topic1)?, 1);
assert_eq!(graph.node1.count_publishers(&topic2)?, 1);
assert_eq!(graph.node1.count_publishers(&topic3)?, 1);

// Test get_publisher_names_and_types_by_node()
let node_1_publisher_names_and_types = graph
.node1
.get_publisher_names_and_types_by_node(&graph.node1.name(), namespace)?;

let types = node_1_publisher_names_and_types.get(&topic1).unwrap();
assert!(types.contains(&"test_msgs/msg/Empty".to_string()));

let types = node_1_publisher_names_and_types.get(&topic2).unwrap();
assert!(types.contains(&"test_msgs/msg/BasicTypes".to_string()));

let node_2_publisher_names_and_types = graph
.node1
.get_publisher_names_and_types_by_node(&graph.node2.name(), namespace)?;

let types = node_2_publisher_names_and_types.get(&topic3).unwrap();
assert!(types.contains(&"test_msgs/msg/Defaults".to_string()));

// Test get_publishers_info_by_topic()
let expected_publishers_info = vec![TopicEndpointInfo {
node_name: String::from("graph_test_node_1"),
node_namespace: String::from(namespace),
topic_type: String::from("test_msgs/msg/Empty"),
}];
assert_eq!(
graph.node1.get_publishers_info_by_topic(&topic1)?,
expected_publishers_info
);
assert_eq!(
graph.node2.get_publishers_info_by_topic(&topic1)?,
expected_publishers_info
);

Ok(())
}
}
13 changes: 13 additions & 0 deletions rclrs/src/publisher/loaned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn traits() {
use crate::test_helpers::*;

assert_send::<LoanedMessage<test_msgs::msg::rmw::BoundedSequences>>();
assert_sync::<LoanedMessage<test_msgs::msg::rmw::BoundedSequences>>();
}
}
51 changes: 51 additions & 0 deletions rclrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,54 @@ where
.ok()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;

#[test]
fn traits() {
assert_send::<Service<test_msgs::srv::Arrays>>();
assert_sync::<Service<test_msgs::srv::Arrays>>();
}

#[test]
fn test_services() -> Result<(), RclrsError> {
use crate::TopicNamesAndTypes;
use test_msgs::srv;

let namespace = "/test_services_graph";
let graph = construct_test_graph(namespace)?;
let check_names_and_types = |names_and_types: TopicNamesAndTypes| {
let types = names_and_types
.get("/test_services_graph/graph_test_topic_4")
.unwrap();
assert!(types.contains(&"test_msgs/srv/Empty".to_string()));
};

let _node_1_empty_service =
graph
.node1
.create_service::<srv::Empty, _>("graph_test_topic_4", |_, _| {
srv::Empty_Response {
structure_needs_at_least_one_member: 0,
}
})?;
let _node_2_empty_client = graph
.node2
.create_client::<srv::Empty>("graph_test_topic_4")?;

std::thread::sleep(std::time::Duration::from_millis(100));

let service_names_and_types = graph.node1.get_service_names_and_types()?;
check_names_and_types(service_names_and_types);

let service_names_and_types = graph
.node1
.get_service_names_and_types_by_node(&graph.node1.name(), namespace)?;
check_names_and_types(service_names_and_types);

Ok(())
}
}
Loading

0 comments on commit 12bca36

Please sign in to comment.