@@ -230,3 +230,81 @@ impl<'a, T: Message> MessageCow<'a, T> for &'a T {
230230 Cow :: Borrowed ( self )
231231 }
232232}
233+
234+ #[ cfg( test) ]
235+ mod tests {
236+ use super :: * ;
237+ use crate :: test_helpers:: * ;
238+
239+ #[ test]
240+ fn traits ( ) {
241+ assert_send :: < Publisher < test_msgs:: msg:: BoundedSequences > > ( ) ;
242+ assert_sync :: < Publisher < test_msgs:: msg:: BoundedSequences > > ( ) ;
243+ }
244+
245+ #[ test]
246+ fn test_publishers ( ) -> Result < ( ) , RclrsError > {
247+ use crate :: TopicEndpointInfo ;
248+ use crate :: QOS_PROFILE_SYSTEM_DEFAULT ;
249+ use test_msgs:: msg;
250+
251+ let namespace = "/test_publishers_graph" ;
252+ let graph = construct_test_graph ( namespace) ?;
253+
254+ let node_1_empty_publisher = graph
255+ . node1
256+ . create_publisher :: < msg:: Empty > ( "graph_test_topic_1" , QOS_PROFILE_SYSTEM_DEFAULT ) ?;
257+ let topic1 = node_1_empty_publisher. topic_name ( ) ;
258+ let node_1_basic_types_publisher = graph. node1 . create_publisher :: < msg:: BasicTypes > (
259+ "graph_test_topic_2" ,
260+ QOS_PROFILE_SYSTEM_DEFAULT ,
261+ ) ?;
262+ let topic2 = node_1_basic_types_publisher. topic_name ( ) ;
263+ let node_2_default_publisher = graph
264+ . node2
265+ . create_publisher :: < msg:: Defaults > ( "graph_test_topic_3" , QOS_PROFILE_SYSTEM_DEFAULT ) ?;
266+ let topic3 = node_2_default_publisher. topic_name ( ) ;
267+
268+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
269+
270+ // Test count_publishers()
271+ assert_eq ! ( graph. node1. count_publishers( & topic1) ?, 1 ) ;
272+ assert_eq ! ( graph. node1. count_publishers( & topic2) ?, 1 ) ;
273+ assert_eq ! ( graph. node1. count_publishers( & topic3) ?, 1 ) ;
274+
275+ // Test get_publisher_names_and_types_by_node()
276+ let node_1_publisher_names_and_types = graph
277+ . node1
278+ . get_publisher_names_and_types_by_node ( & graph. node1 . name ( ) , namespace) ?;
279+
280+ let types = node_1_publisher_names_and_types. get ( & topic1) . unwrap ( ) ;
281+ assert ! ( types. contains( & "test_msgs/msg/Empty" . to_string( ) ) ) ;
282+
283+ let types = node_1_publisher_names_and_types. get ( & topic2) . unwrap ( ) ;
284+ assert ! ( types. contains( & "test_msgs/msg/BasicTypes" . to_string( ) ) ) ;
285+
286+ let node_2_publisher_names_and_types = graph
287+ . node1
288+ . get_publisher_names_and_types_by_node ( & graph. node2 . name ( ) , namespace) ?;
289+
290+ let types = node_2_publisher_names_and_types. get ( & topic3) . unwrap ( ) ;
291+ assert ! ( types. contains( & "test_msgs/msg/Defaults" . to_string( ) ) ) ;
292+
293+ // Test get_publishers_info_by_topic()
294+ let expected_publishers_info = vec ! [ TopicEndpointInfo {
295+ node_name: String :: from( "graph_test_node_1" ) ,
296+ node_namespace: String :: from( namespace) ,
297+ topic_type: String :: from( "test_msgs/msg/Empty" ) ,
298+ } ] ;
299+ assert_eq ! (
300+ graph. node1. get_publishers_info_by_topic( & topic1) ?,
301+ expected_publishers_info
302+ ) ;
303+ assert_eq ! (
304+ graph. node2. get_publishers_info_by_topic( & topic1) ?,
305+ expected_publishers_info
306+ ) ;
307+
308+ Ok ( ( ) )
309+ }
310+ }
0 commit comments