@@ -19,9 +19,6 @@ use crate::{
19
19
SubscriptionCallback , TimeSource , Timer , TimerCallback , ToLogParams , ENTITY_LIFECYCLE_MUTEX ,
20
20
} ;
21
21
22
- /// Constant conversion from seconds to nanoseconds
23
- const S_TO_NS : f64 = 1e9 ;
24
-
25
22
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
26
23
// they are running in. Therefore, this type can be safely sent to another thread.
27
24
unsafe impl Send for rcl_node_t { }
@@ -344,7 +341,10 @@ impl Node {
344
341
Ok ( subscription)
345
342
}
346
343
347
- /// Creates a [`Timer`]
344
+ /// Creates a [`Timer`][1].
345
+ ///
346
+ /// [1]: crate::Timer
347
+ /// TODO: make timer's lifetime depend on node's lifetime.
348
348
pub fn create_timer (
349
349
& self ,
350
350
period_ns : i64 ,
@@ -583,6 +583,52 @@ mod tests {
583
583
Ok ( ( ) )
584
584
}
585
585
586
+ #[ test]
587
+ fn test_create_timer_without_clock_source ( ) -> Result < ( ) , RclrsError > {
588
+ let timer_period_ns: i64 = 1e6 as i64 ; // 1 millisecond.
589
+ let flag: Arc < Mutex < bool > > = Arc :: new ( Mutex :: new ( false ) ) ;
590
+ let callback_flag: Arc < Mutex < bool > > = Arc :: new ( Mutex :: new ( false ) ) ;
591
+ let context = Context :: new ( [ ] ) ?;
592
+ let dut = NodeBuilder :: new ( & context, "node_with_timer" )
593
+ . namespace ( "test_create_timer" )
594
+ . build ( ) ?;
595
+
596
+ let timer = dut. create_timer (
597
+ timer_period_ns,
598
+ & context,
599
+ Some ( Box :: new ( move |_| {
600
+ * callback_flag. lock ( ) . unwrap ( ) = true ;
601
+ } ) ) ,
602
+ None ,
603
+ ) ?;
604
+ assert_eq ! ( dut. live_timers( ) . len( ) , 1 ) ;
605
+
606
+ Ok ( ( ) )
607
+ }
608
+
609
+ #[ test]
610
+ fn test_create_timer_with_clock_source ( ) -> Result < ( ) , RclrsError > {
611
+ let timer_period_ns: i64 = 1e6 as i64 ; // 1 millisecond.
612
+ let flag: Arc < Mutex < bool > > = Arc :: new ( Mutex :: new ( false ) ) ;
613
+ let callback_flag: Arc < Mutex < bool > > = Arc :: new ( Mutex :: new ( false ) ) ;
614
+ let context = Context :: new ( [ ] ) ?;
615
+ let dut = NodeBuilder :: new ( & context, "node_with_timer" )
616
+ . namespace ( "test_create_timer" )
617
+ . build ( ) ?;
618
+
619
+ let timer = dut. create_timer (
620
+ timer_period_ns,
621
+ & context,
622
+ Some ( Box :: new ( move |_| {
623
+ * callback_flag. lock ( ) . unwrap ( ) = true ;
624
+ } ) ) ,
625
+ None ,
626
+ ) ?;
627
+ assert_eq ! ( dut. live_timers( ) . len( ) , 1 ) ;
628
+
629
+ Ok ( ( ) )
630
+ }
631
+
586
632
#[ test]
587
633
fn test_logger_name ( ) -> Result < ( ) , RclrsError > {
588
634
// Use helper to create 2 nodes for us
0 commit comments