@@ -77,3 +77,44 @@ pub fn spin(node: &Node) -> Result<(), RclrsError> {
7777
7878 Ok ( ( ) )
7979}
80+
81+ #[ cfg( test) ]
82+ mod tests {
83+ use super :: * ;
84+ use std:: sync:: { Arc , Mutex } ;
85+ use std:: time:: Duration ;
86+
87+ #[ test]
88+ fn test_spin_once ( ) -> Result < ( ) , RclrsError > {
89+ let context = Context :: new ( vec ! [ ] ) . unwrap ( ) ;
90+ let mut subscriber_node = context. create_node ( "minimal_subscriber" ) ?;
91+ let num_messages = Arc :: new ( Mutex :: new ( 0 ) ) ;
92+ let received_msg = Arc :: new ( Mutex :: new ( String :: new ( ) ) ) ;
93+ let n = num_messages. clone ( ) ;
94+ let m = received_msg. clone ( ) ;
95+ let publisher = subscriber_node
96+ . create_publisher :: < std_msgs:: msg:: String > ( "topic" , QOS_PROFILE_DEFAULT ) ?;
97+ let _subscription = subscriber_node. create_subscription :: < std_msgs:: msg:: String , _ > (
98+ "topic" ,
99+ QOS_PROFILE_DEFAULT ,
100+ move |msg : std_msgs:: msg:: String | {
101+ let mut num_messages = n. lock ( ) . unwrap ( ) ;
102+ let mut received_msg = m. lock ( ) . unwrap ( ) ;
103+
104+ * num_messages += 1 ;
105+ * received_msg = msg. data ;
106+ } ,
107+ ) ?;
108+
109+ let message = std_msgs:: msg:: String {
110+ data : String :: from ( "Hello World" ) ,
111+ } ;
112+ publisher. publish ( message) ?;
113+ spin_once ( & subscriber_node, Some ( Duration :: from_millis ( 500 ) ) ) ?;
114+
115+ assert_eq ! ( * num_messages. lock( ) . unwrap( ) , 1 ) ;
116+ assert_eq ! ( & * received_msg. lock( ) . unwrap( ) . as_str( ) , "Hello World" ) ;
117+
118+ Ok ( ( ) )
119+ }
120+ }
0 commit comments