pub struct TestTimerDriver<S: 'static = Box<dyn Handler<Instant>>> { /* private fields */ }Expand description
Virtual-time wrapper around TimerPoller for deterministic timer
testing.
Captures Instant::now() at construction as the starting time.
advance and set_now control
the virtual clock. poll delegates to the inner
poller using the virtual time.
§Examples
ⓘ
use std::time::{Duration, Instant};
use nexus_rt::{WorldBuilder, ResMut, IntoHandler};
use nexus_rt::timer::{TimerInstaller, TimerWheel, Wheel};
use nexus_rt::testing::TestTimerDriver;
let mut builder = WorldBuilder::new();
builder.register::<bool>(false);
let wheel = Wheel::unbounded(64, Instant::now());
let poller = builder.install_driver(TimerInstaller::new(wheel));
let mut timer = TestTimerDriver::new(poller);
let mut world = builder.build();
fn on_fire(mut flag: ResMut<bool>, _now: std::time::Instant) {
*flag = true;
}
let handler = on_fire.into_handler(world.registry());
let deadline = timer.now() + Duration::from_millis(100);
world.resource_mut::<TimerWheel>()
.schedule_forget(deadline, Box::new(handler));
timer.advance(Duration::from_millis(150));
let fired = timer.poll(&mut world);
assert_eq!(fired, 1);
assert!(*world.resource::<bool>());Implementations§
Source§impl<S: DerefMut + Send + 'static> TestTimerDriver<S>
impl<S: DerefMut + Send + 'static> TestTimerDriver<S>
Sourcepub fn new(poller: TimerPoller<S>) -> Self
pub fn new(poller: TimerPoller<S>) -> Self
Wrap an installed TimerPoller. Captures Instant::now() as
the starting time.
Sourcepub fn poll(&mut self, world: &mut World) -> usize
pub fn poll(&mut self, world: &mut World) -> usize
Poll expired timers at the current virtual time.
Delegates to TimerPoller::poll with the virtual time.
Returns the number of timers fired.
Sourcepub fn next_deadline(&self, world: &World) -> Option<Instant>
pub fn next_deadline(&self, world: &World) -> Option<Instant>
Earliest deadline in the wheel.
Auto Trait Implementations§
impl<S> Freeze for TestTimerDriver<S>
impl<S> RefUnwindSafe for TestTimerDriver<S>where
S: RefUnwindSafe,
impl<S> Send for TestTimerDriver<S>where
S: Send,
impl<S = Box<dyn Handler<Instant>>> !Sync for TestTimerDriver<S>
impl<S> Unpin for TestTimerDriver<S>where
S: Unpin,
impl<S> UnsafeUnpin for TestTimerDriver<S>
impl<S> UnwindSafe for TestTimerDriver<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more