Skip to content

Commit 6954069

Browse files
authored
Fix a bug in graph query function and add regression test (#283)
1 parent 0c26297 commit 6954069

File tree

2 files changed

+79
-70
lines changed

2 files changed

+79
-70
lines changed

rclrs/src/node/graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct NodeNameInfo {
4747
}
4848

4949
/// Contains topic endpoint information
50+
#[derive(Debug, PartialEq, Eq)]
5051
pub struct TopicEndpointInfo {
5152
/// The name of the endpoint node
5253
pub node_name: String,
@@ -374,7 +375,7 @@ impl Node {
374375
&*self.rcl_node_mtx.lock().unwrap(),
375376
&mut rcutils_get_default_allocator(),
376377
topic.as_ptr(),
377-
true,
378+
false,
378379
&mut rcl_publishers_info,
379380
)
380381
.ok()?;

rclrs_tests/src/graph_tests.rs

+77-69
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rclrs::{
2-
Context, Node, NodeBuilder, RclrsError, TopicNamesAndTypes, QOS_PROFILE_SYSTEM_DEFAULT,
2+
Context, Node, NodeBuilder, RclrsError, TopicEndpointInfo, TopicNamesAndTypes,
3+
QOS_PROFILE_SYSTEM_DEFAULT,
34
};
45
use test_msgs::{msg, srv};
56

@@ -22,122 +23,127 @@ fn construct_test_graph(namespace: &str) -> Result<TestGraph, RclrsError> {
2223

2324
#[test]
2425
fn test_publishers() -> Result<(), RclrsError> {
25-
let graph = construct_test_graph("test_publishers_graph")?;
26+
let namespace = "/test_publishers_graph";
27+
let graph = construct_test_graph(namespace)?;
2628

27-
let _node_1_empty_publisher = graph
29+
let node_1_empty_publisher = graph
2830
.node1
2931
.create_publisher::<msg::Empty>("graph_test_topic_1", QOS_PROFILE_SYSTEM_DEFAULT)?;
30-
let _node_1_basic_types_publisher = graph
32+
let topic1 = node_1_empty_publisher.topic_name();
33+
let node_1_basic_types_publisher = graph
3134
.node1
3235
.create_publisher::<msg::BasicTypes>("graph_test_topic_2", QOS_PROFILE_SYSTEM_DEFAULT)?;
33-
let _node_2_default_publisher = graph
36+
let topic2 = node_1_basic_types_publisher.topic_name();
37+
let node_2_default_publisher = graph
3438
.node2
3539
.create_publisher::<msg::Defaults>("graph_test_topic_3", QOS_PROFILE_SYSTEM_DEFAULT)?;
40+
let topic3 = node_2_default_publisher.topic_name();
3641

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

39-
assert_eq!(
40-
graph
41-
.node1
42-
.count_publishers(&(graph.node1.namespace() + "/graph_test_topic_1"))?,
43-
1
44-
);
45-
assert_eq!(
46-
graph
47-
.node1
48-
.count_publishers(&(graph.node1.namespace() + "/graph_test_topic_2"))?,
49-
1
50-
);
44+
// Test count_publishers()
45+
assert_eq!(graph.node1.count_publishers(&topic1)?, 1);
46+
assert_eq!(graph.node1.count_publishers(&topic2)?, 1);
47+
assert_eq!(graph.node1.count_publishers(&topic3)?, 1);
48+
49+
// Test get_publisher_names_and_types_by_node()
5150
let node_1_publisher_names_and_types = graph
5251
.node1
53-
.get_publisher_names_and_types_by_node(&graph.node1.name(), &graph.node1.namespace())?;
52+
.get_publisher_names_and_types_by_node(&graph.node1.name(), namespace)?;
5453

55-
let types = node_1_publisher_names_and_types
56-
.get(&(graph.node1.namespace() + "/graph_test_topic_1"))
57-
.unwrap();
54+
let types = node_1_publisher_names_and_types.get(&topic1).unwrap();
5855
assert!(types.contains(&"test_msgs/msg/Empty".to_string()));
5956

60-
let types = node_1_publisher_names_and_types
61-
.get(&(graph.node1.namespace() + "/graph_test_topic_2"))
62-
.unwrap();
57+
let types = node_1_publisher_names_and_types.get(&topic2).unwrap();
6358
assert!(types.contains(&"test_msgs/msg/BasicTypes".to_string()));
6459

6560
let node_2_publisher_names_and_types = graph
66-
.node2
67-
.get_publisher_names_and_types_by_node(&graph.node2.name(), &graph.node2.namespace())?;
61+
.node1
62+
.get_publisher_names_and_types_by_node(&graph.node2.name(), namespace)?;
6863

69-
let types = node_2_publisher_names_and_types
70-
.get(&(graph.node2.namespace() + "/graph_test_topic_3"))
71-
.unwrap();
64+
let types = node_2_publisher_names_and_types.get(&topic3).unwrap();
65+
assert!(types.contains(&"test_msgs/msg/Defaults".to_string()));
66+
67+
// Test get_publishers_info_by_topic()
68+
let expected_publishers_info = vec![TopicEndpointInfo {
69+
node_name: String::from("graph_test_node_1"),
70+
node_namespace: String::from(namespace),
71+
topic_type: String::from("test_msgs/msg/Empty"),
72+
}];
7273
assert_eq!(
73-
graph
74-
.node2
75-
.count_publishers(&(graph.node2.namespace() + "/graph_test_topic_3"))?,
76-
1
74+
graph.node1.get_publishers_info_by_topic(&topic1)?,
75+
expected_publishers_info
76+
);
77+
assert_eq!(
78+
graph.node2.get_publishers_info_by_topic(&topic1)?,
79+
expected_publishers_info
7780
);
78-
assert!(types.contains(&"test_msgs/msg/Defaults".to_string()));
7981

8082
Ok(())
8183
}
8284

8385
#[test]
8486
fn test_subscriptions() -> Result<(), RclrsError> {
85-
let mut graph = construct_test_graph("test_subscriptions_graph")?;
87+
let namespace = "/test_subscriptions_graph";
88+
let mut graph = construct_test_graph(namespace)?;
8689

87-
let _node_1_defaults_subscription = graph.node1.create_subscription::<msg::Defaults, _>(
88-
"graph_test_topic_3",
89-
QOS_PROFILE_SYSTEM_DEFAULT,
90-
|_msg: msg::Defaults| {},
91-
)?;
92-
let _node_2_empty_subscription = graph.node2.create_subscription::<msg::Empty, _>(
90+
let node_2_empty_subscription = graph.node2.create_subscription::<msg::Empty, _>(
9391
"graph_test_topic_1",
9492
QOS_PROFILE_SYSTEM_DEFAULT,
9593
|_msg: msg::Empty| {},
9694
)?;
97-
let _node_2_basic_types_subscription = graph.node2.create_subscription::<msg::BasicTypes, _>(
95+
let topic1 = node_2_empty_subscription.topic_name();
96+
let node_2_basic_types_subscription = graph.node2.create_subscription::<msg::BasicTypes, _>(
9897
"graph_test_topic_2",
9998
QOS_PROFILE_SYSTEM_DEFAULT,
10099
|_msg: msg::BasicTypes| {},
101100
)?;
101+
let topic2 = node_2_basic_types_subscription.topic_name();
102+
let node_1_defaults_subscription = graph.node1.create_subscription::<msg::Defaults, _>(
103+
"graph_test_topic_3",
104+
QOS_PROFILE_SYSTEM_DEFAULT,
105+
|_msg: msg::Defaults| {},
106+
)?;
107+
let topic3 = node_1_defaults_subscription.topic_name();
102108

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

111+
// Test count_subscriptions()
112+
assert_eq!(graph.node2.count_subscriptions(&topic1)?, 1);
113+
assert_eq!(graph.node2.count_subscriptions(&topic2)?, 1);
114+
115+
// Test get_subscription_names_and_types_by_node()
105116
let node_1_subscription_names_and_types = graph
106117
.node1
107-
.get_subscription_names_and_types_by_node(&graph.node1.name(), &graph.node1.namespace())?;
118+
.get_subscription_names_and_types_by_node(&graph.node1.name(), namespace)?;
108119

109-
let types = node_1_subscription_names_and_types
110-
.get(&(graph.node2.namespace() + "/graph_test_topic_3"))
111-
.unwrap();
120+
let types = node_1_subscription_names_and_types.get(&topic3).unwrap();
112121
assert!(types.contains(&"test_msgs/msg/Defaults".to_string()));
113122

114-
assert_eq!(
115-
graph
116-
.node2
117-
.count_subscriptions(&(graph.node2.namespace() + "/graph_test_topic_1"))?,
118-
1
119-
);
120-
assert_eq!(
121-
graph
122-
.node2
123-
.count_subscriptions(&(graph.node2.namespace() + "/graph_test_topic_2"))?,
124-
1
125-
);
126-
127123
let node_2_subscription_names_and_types = graph
128124
.node2
129-
.get_subscription_names_and_types_by_node(&graph.node2.name(), &graph.node2.namespace())?;
125+
.get_subscription_names_and_types_by_node(&graph.node2.name(), namespace)?;
130126

131-
let types = node_2_subscription_names_and_types
132-
.get(&(graph.node1.namespace() + "/graph_test_topic_1"))
133-
.unwrap();
127+
let types = node_2_subscription_names_and_types.get(&topic1).unwrap();
134128
assert!(types.contains(&"test_msgs/msg/Empty".to_string()));
135129

136-
let types = node_2_subscription_names_and_types
137-
.get(&(graph.node2.namespace() + "/graph_test_topic_2"))
138-
.unwrap();
130+
let types = node_2_subscription_names_and_types.get(&topic2).unwrap();
139131
assert!(types.contains(&"test_msgs/msg/BasicTypes".to_string()));
140132

133+
// Test get_subscriptions_info_by_topic()
134+
let expected_subscriptions_info = vec![TopicEndpointInfo {
135+
node_name: String::from("graph_test_node_2"),
136+
node_namespace: String::from(namespace),
137+
topic_type: String::from("test_msgs/msg/Empty"),
138+
}];
139+
assert_eq!(
140+
graph.node1.get_subscriptions_info_by_topic(&topic1)?,
141+
expected_subscriptions_info
142+
);
143+
assert_eq!(
144+
graph.node2.get_subscriptions_info_by_topic(&topic1)?,
145+
expected_subscriptions_info
146+
);
141147
Ok(())
142148
}
143149

@@ -184,7 +190,8 @@ fn test_topic_names_and_types() -> Result<(), RclrsError> {
184190

185191
#[test]
186192
fn test_services() -> Result<(), RclrsError> {
187-
let mut graph = construct_test_graph("test_services_graph")?;
193+
let namespace = "/test_services_graph";
194+
let mut graph = construct_test_graph(namespace)?;
188195
let check_names_and_types = |names_and_types: TopicNamesAndTypes| {
189196
let types = names_and_types
190197
.get("/test_services_graph/graph_test_topic_4")
@@ -209,15 +216,16 @@ fn test_services() -> Result<(), RclrsError> {
209216

210217
let service_names_and_types = graph
211218
.node1
212-
.get_service_names_and_types_by_node(&graph.node1.name(), &graph.node1.namespace())?;
219+
.get_service_names_and_types_by_node(&graph.node1.name(), namespace)?;
213220
check_names_and_types(service_names_and_types);
214221

215222
Ok(())
216223
}
217224

218225
#[test]
219226
fn test_clients() -> Result<(), RclrsError> {
220-
let mut graph = construct_test_graph("test_clients_graph")?;
227+
let namespace = "/test_clients_graph";
228+
let mut graph = construct_test_graph(namespace)?;
221229
let _node_2_empty_client = graph
222230
.node2
223231
.create_client::<srv::Empty>("graph_test_topic_4")?;

0 commit comments

Comments
 (0)