Skip to content

Commit 4515e9a

Browse files
committed
Adds tests, documentation and removes dead code in node.rs.
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent ab3d63a commit 4515e9a

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

rclrs/src/node.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ use crate::{
1919
SubscriptionCallback, TimeSource, Timer, TimerCallback, ToLogParams, ENTITY_LIFECYCLE_MUTEX,
2020
};
2121

22-
/// Constant conversion from seconds to nanoseconds
23-
const S_TO_NS: f64 = 1e9;
24-
2522
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
2623
// they are running in. Therefore, this type can be safely sent to another thread.
2724
unsafe impl Send for rcl_node_t {}
@@ -344,7 +341,10 @@ impl Node {
344341
Ok(subscription)
345342
}
346343

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.
348348
pub fn create_timer(
349349
&self,
350350
period_ns: i64,
@@ -583,6 +583,52 @@ mod tests {
583583
Ok(())
584584
}
585585

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+
586632
#[test]
587633
fn test_logger_name() -> Result<(), RclrsError> {
588634
// Use helper to create 2 nodes for us

0 commit comments

Comments
 (0)