1
1
use rclrs:: {
2
- Context , Node , NodeBuilder , RclrsError , TopicNamesAndTypes , QOS_PROFILE_SYSTEM_DEFAULT ,
2
+ Context , Node , NodeBuilder , RclrsError , TopicEndpointInfo , TopicNamesAndTypes ,
3
+ QOS_PROFILE_SYSTEM_DEFAULT ,
3
4
} ;
4
5
use test_msgs:: { msg, srv} ;
5
6
@@ -22,122 +23,127 @@ fn construct_test_graph(namespace: &str) -> Result<TestGraph, RclrsError> {
22
23
23
24
#[ test]
24
25
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) ?;
26
28
27
- let _node_1_empty_publisher = graph
29
+ let node_1_empty_publisher = graph
28
30
. node1
29
31
. 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
31
34
. node1
32
35
. 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
34
38
. node2
35
39
. create_publisher :: < msg:: Defaults > ( "graph_test_topic_3" , QOS_PROFILE_SYSTEM_DEFAULT ) ?;
40
+ let topic3 = node_2_default_publisher. topic_name ( ) ;
36
41
37
42
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
38
43
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()
51
50
let node_1_publisher_names_and_types = graph
52
51
. 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) ?;
54
53
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 ( ) ;
58
55
assert ! ( types. contains( & "test_msgs/msg/Empty" . to_string( ) ) ) ;
59
56
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 ( ) ;
63
58
assert ! ( types. contains( & "test_msgs/msg/BasicTypes" . to_string( ) ) ) ;
64
59
65
60
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) ?;
68
63
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
+ } ] ;
72
73
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
77
80
) ;
78
- assert ! ( types. contains( & "test_msgs/msg/Defaults" . to_string( ) ) ) ;
79
81
80
82
Ok ( ( ) )
81
83
}
82
84
83
85
#[ test]
84
86
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) ?;
86
89
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 , _ > (
93
91
"graph_test_topic_1" ,
94
92
QOS_PROFILE_SYSTEM_DEFAULT ,
95
93
|_msg : msg:: Empty | { } ,
96
94
) ?;
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 , _ > (
98
97
"graph_test_topic_2" ,
99
98
QOS_PROFILE_SYSTEM_DEFAULT ,
100
99
|_msg : msg:: BasicTypes | { } ,
101
100
) ?;
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 ( ) ;
102
108
103
109
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
104
110
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()
105
116
let node_1_subscription_names_and_types = graph
106
117
. 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) ?;
108
119
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 ( ) ;
112
121
assert ! ( types. contains( & "test_msgs/msg/Defaults" . to_string( ) ) ) ;
113
122
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
-
127
123
let node_2_subscription_names_and_types = graph
128
124
. 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) ?;
130
126
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 ( ) ;
134
128
assert ! ( types. contains( & "test_msgs/msg/Empty" . to_string( ) ) ) ;
135
129
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 ( ) ;
139
131
assert ! ( types. contains( & "test_msgs/msg/BasicTypes" . to_string( ) ) ) ;
140
132
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
+ ) ;
141
147
Ok ( ( ) )
142
148
}
143
149
@@ -184,7 +190,8 @@ fn test_topic_names_and_types() -> Result<(), RclrsError> {
184
190
185
191
#[ test]
186
192
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) ?;
188
195
let check_names_and_types = |names_and_types : TopicNamesAndTypes | {
189
196
let types = names_and_types
190
197
. get ( "/test_services_graph/graph_test_topic_4" )
@@ -209,15 +216,16 @@ fn test_services() -> Result<(), RclrsError> {
209
216
210
217
let service_names_and_types = graph
211
218
. 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) ?;
213
220
check_names_and_types ( service_names_and_types) ;
214
221
215
222
Ok ( ( ) )
216
223
}
217
224
218
225
#[ test]
219
226
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) ?;
221
229
let _node_2_empty_client = graph
222
230
. node2
223
231
. create_client :: < srv:: Empty > ( "graph_test_topic_4" ) ?;
0 commit comments