Skip to content

Commit d337c96

Browse files
committed
* Added unit test to confirm a timer will trigger the waitset.
1 parent 3d2f6b7 commit d337c96

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

rclrs/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl Node {
356356
where
357357
F: Fn(&mut Timer) + 'static + Send + Sync,
358358
{
359-
let timer = Arc::new(Mutex::new(Timer::new(
359+
let timer = Arc::new(Mutex::new(Timer::new_with_context_handle(
360360
Arc::clone(&self.handle.context_handle),
361361
self.get_clock(),
362362
period,

rclrs/src/timer.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
rcl_timer_is_canceled, rcl_timer_is_ready, rcl_timer_reset, rcl_timer_t,
88
rcutils_get_default_allocator,
99
},
10-
ContextHandle, RclReturnCode, RclrsError, ToResult, ENTITY_LIFECYCLE_MUTEX,
10+
Context, ContextHandle, RclReturnCode, RclrsError, ToResult, ENTITY_LIFECYCLE_MUTEX,
1111
};
1212
use std::{
1313
i64,
@@ -80,7 +80,20 @@ pub struct Timer {
8080
impl Timer {
8181
/// Creates a new `Timer` with the given period and callback.
8282
/// Periods greater than i64::MAX nanoseconds will saturate to i64::MAX.
83-
pub(crate) fn new<F>(
83+
pub fn new<F>(
84+
context: &Context,
85+
clock: Clock,
86+
period: Duration,
87+
callback: F,
88+
) -> Result<Self, RclrsError>
89+
where
90+
F: Fn(&mut Timer) + 'static + Send + Sync,
91+
{
92+
Timer::new_with_context_handle(Arc::clone(&context.handle), clock, period, callback)
93+
}
94+
95+
/// Version of [`Timer::new`] that takes a context handle directly.
96+
pub(crate) fn new_with_context_handle<F>(
8497
context_handle: Arc<ContextHandle>,
8598
clock: Clock,
8699
period: Duration,
@@ -310,14 +323,11 @@ mod tests {
310323

311324
fn new_timer() -> Timer {
312325
let context = Context::new([]).unwrap();
326+
327+
// This is technically a wall clock, but we have a period of 0 so it won't slow down unit testing.
313328
let clock = Clock::system();
314329

315-
let timer = Timer::new(
316-
context.handle.clone(),
317-
clock,
318-
Duration::from_secs(0),
319-
|_| {},
320-
);
330+
let timer = Timer::new(&context, clock, Duration::from_secs(0), |_| {});
321331

322332
timer.expect("Timer::new should not return an error")
323333
}

rclrs/src/wait.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ impl WaitSet {
475475

476476
#[cfg(test)]
477477
mod tests {
478+
use crate::{Clock, Timer};
479+
478480
use super::*;
479481

480482
#[test]
@@ -500,4 +502,28 @@ mod tests {
500502

501503
Ok(())
502504
}
505+
506+
#[test]
507+
fn timer_in_wait_set_readies() -> Result<(), RclrsError> {
508+
let context = Context::new([])?;
509+
// This is technically a wall clock, but we have a period of 0 so it won't slow down unit testing.
510+
let clock = Clock::system();
511+
512+
let timer: Arc<Mutex<dyn TimerBase>> = Arc::new(Mutex::new(Timer::new(
513+
&context,
514+
clock,
515+
Duration::from_secs(0),
516+
|_| {},
517+
)?));
518+
let mut wait_set = WaitSet::new(0, 0, 1, 0, 0, 0, &context)?;
519+
520+
assert!(wait_set.timers.is_empty());
521+
wait_set.add_timer(Arc::clone(&timer))?;
522+
523+
let readies = wait_set.wait(Some(std::time::Duration::from_millis(10)))?;
524+
525+
assert_eq!(readies.timers.len(), 1);
526+
527+
Ok(())
528+
}
503529
}

0 commit comments

Comments
 (0)