Skip to main content

TestTimerDriver

Struct TestTimerDriver 

Source
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>
where S::Target: Handler<Instant>,

Source

pub fn new(poller: TimerPoller<S>) -> Self

Wrap an installed TimerPoller. Captures Instant::now() as the starting time.

Source

pub fn now(&self) -> Instant

Current virtual time.

Source

pub fn advance(&mut self, duration: Duration)

Advance virtual time by a duration.

Source

pub fn set_now(&mut self, now: Instant)

Set virtual time to a specific instant.

Source

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.

Source

pub fn next_deadline(&self, world: &World) -> Option<Instant>

Earliest deadline in the wheel.

Source

pub fn len(&self, world: &World) -> usize

Number of active timers.

Source

pub fn is_empty(&self, world: &World) -> bool

Whether the wheel is empty.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.