@@ -138,6 +138,7 @@ impl DynamicPublisherState {
138
138
#[ cfg( test) ]
139
139
mod tests {
140
140
use super :: * ;
141
+ use crate :: test_helpers:: * ;
141
142
142
143
fn assert_send < T : Send > ( ) { }
143
144
fn assert_sync < T : Sync > ( ) { }
@@ -147,4 +148,109 @@ mod tests {
147
148
assert_send :: < DynamicPublisher > ( ) ;
148
149
assert_sync :: < DynamicPublisher > ( ) ;
149
150
}
151
+
152
+ #[ test]
153
+ fn test_dynamic_publishers ( ) -> Result < ( ) , RclrsError > {
154
+ use crate :: TopicEndpointInfo ;
155
+ use test_msgs:: msg;
156
+
157
+ let namespace = "/test_dynamic_publishers_graph" ;
158
+ let graph = construct_test_graph ( namespace) ?;
159
+
160
+ let node_1_empty_publisher = graph. node1 . create_dynamic_publisher (
161
+ "test_msgs/msg/Empty" . try_into ( ) . unwrap ( ) ,
162
+ "graph_test_topic_1" ,
163
+ ) ?;
164
+ let topic1 = node_1_empty_publisher. topic_name ( ) ;
165
+ let node_1_basic_types_publisher = graph. node1 . create_dynamic_publisher (
166
+ "test_msgs/msg/BasicTypes" . try_into ( ) . unwrap ( ) ,
167
+ "graph_test_topic_2" ,
168
+ ) ?;
169
+ let topic2 = node_1_basic_types_publisher. topic_name ( ) ;
170
+ let node_2_default_publisher = graph. node2 . create_dynamic_publisher (
171
+ "test_msgs/msg/Defaults" . try_into ( ) . unwrap ( ) ,
172
+ "graph_test_topic_3" ,
173
+ ) ?;
174
+ let topic3 = node_2_default_publisher. topic_name ( ) ;
175
+
176
+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
177
+
178
+ // Test count_publishers()
179
+ assert_eq ! ( graph. node1. count_publishers( & topic1) ?, 1 ) ;
180
+ assert_eq ! ( graph. node1. count_publishers( & topic2) ?, 1 ) ;
181
+ assert_eq ! ( graph. node1. count_publishers( & topic3) ?, 1 ) ;
182
+
183
+ // Test get_publisher_names_and_types_by_node()
184
+ let node_1_publisher_names_and_types = graph
185
+ . node1
186
+ . get_publisher_names_and_types_by_node ( & graph. node1 . name ( ) , namespace) ?;
187
+
188
+ let types = node_1_publisher_names_and_types. get ( & topic1) . unwrap ( ) ;
189
+ assert ! ( types. contains( & "test_msgs/msg/Empty" . to_string( ) ) ) ;
190
+
191
+ let types = node_1_publisher_names_and_types. get ( & topic2) . unwrap ( ) ;
192
+ assert ! ( types. contains( & "test_msgs/msg/BasicTypes" . to_string( ) ) ) ;
193
+
194
+ let node_2_publisher_names_and_types = graph
195
+ . node1
196
+ . get_publisher_names_and_types_by_node ( & graph. node2 . name ( ) , namespace) ?;
197
+
198
+ let types = node_2_publisher_names_and_types. get ( & topic3) . unwrap ( ) ;
199
+ assert ! ( types. contains( & "test_msgs/msg/Defaults" . to_string( ) ) ) ;
200
+
201
+ // Test get_publishers_info_by_topic()
202
+ let expected_publishers_info = vec ! [ TopicEndpointInfo {
203
+ node_name: String :: from( "graph_test_node_1" ) ,
204
+ node_namespace: String :: from( namespace) ,
205
+ topic_type: String :: from( "test_msgs/msg/Empty" ) ,
206
+ } ] ;
207
+ assert_eq ! (
208
+ graph. node1. get_publishers_info_by_topic( & topic1) ?,
209
+ expected_publishers_info
210
+ ) ;
211
+ assert_eq ! (
212
+ graph. node2. get_publishers_info_by_topic( & topic1) ?,
213
+ expected_publishers_info
214
+ ) ;
215
+
216
+ // Test get_subscription_count()
217
+ assert_eq ! ( node_1_empty_publisher. get_subscription_count( ) , Ok ( 0 ) ) ;
218
+ assert_eq ! ( node_1_basic_types_publisher. get_subscription_count( ) , Ok ( 0 ) ) ;
219
+ assert_eq ! ( node_2_default_publisher. get_subscription_count( ) , Ok ( 0 ) ) ;
220
+ // Test subscription with static types
221
+ let _node_1_empty_subscriber = graph
222
+ . node1
223
+ . create_subscription ( "graph_test_topic_1" , |_msg : msg:: Empty | { } ) ;
224
+ let _node_1_basic_types_subscriber = graph
225
+ . node1
226
+ . create_subscription ( "graph_test_topic_2" , |_msg : msg:: BasicTypes | { } ) ;
227
+ let _node_2_default_subscriber = graph
228
+ . node2
229
+ . create_subscription ( "graph_test_topic_3" , |_msg : msg:: Defaults | { } ) ;
230
+ assert_eq ! ( node_1_empty_publisher. get_subscription_count( ) , Ok ( 1 ) ) ;
231
+ assert_eq ! ( node_1_basic_types_publisher. get_subscription_count( ) , Ok ( 1 ) ) ;
232
+ assert_eq ! ( node_2_default_publisher. get_subscription_count( ) , Ok ( 1 ) ) ;
233
+
234
+ // Test subscription with dynamic types
235
+ let _node_1_empty_subscriber = graph. node1 . create_dynamic_subscription (
236
+ "test_msgs/msg/Empty" . try_into ( ) . unwrap ( ) ,
237
+ "graph_test_topic_1" ,
238
+ |_, _| { } ,
239
+ ) ;
240
+ let _node_1_basic_types_subscriber = graph. node1 . create_dynamic_subscription (
241
+ "test_msgs/msg/BasicTypes" . try_into ( ) . unwrap ( ) ,
242
+ "graph_test_topic_2" ,
243
+ |_, _| { } ,
244
+ ) ;
245
+ let _node_2_default_subscriber = graph. node2 . create_dynamic_subscription (
246
+ "test_msgs/msg/Defaults" . try_into ( ) . unwrap ( ) ,
247
+ "graph_test_topic_3" ,
248
+ |_, _| { } ,
249
+ ) ;
250
+ assert_eq ! ( node_1_empty_publisher. get_subscription_count( ) , Ok ( 2 ) ) ;
251
+ assert_eq ! ( node_1_basic_types_publisher. get_subscription_count( ) , Ok ( 2 ) ) ;
252
+ assert_eq ! ( node_2_default_publisher. get_subscription_count( ) , Ok ( 2 ) ) ;
253
+
254
+ Ok ( ( ) )
255
+ }
150
256
}
0 commit comments