Skip to main content

TestExecutor

Struct TestExecutor 

Source
pub struct TestExecutor { /* private fields */ }
Expand description

A single-threaded, deterministic Executor with a virtual clock, for tests.

Nothing runs until the test drives it:

  • run_until_stalled polls queued tasks in FIFO order until none is queued. A task is queued when it is spawned and each time its waker is woken (from any thread); a task that is not woken is not polled again, so nothing busy-polls.
  • advance moves the virtual clock forward, firing due sleeps in (deadline, registration order) order and running the woken tasks at each deadline.
  • now is the virtual clock. It starts at the wall-clock instant of new and moves only through advance.

spawn accepts futures from any thread. spawn_local succeeds only on the thread that created the executor (its owner thread), which is also where local tasks are polled. spawn_blocking queues the closure as an ordinary task, so it runs inside run_until_stalled on the driving thread.

Cloning gives another handle to the same executor.

Implementations§

Source§

impl TestExecutor

Source

pub fn new() -> Self

A new executor owned by the calling thread, with the virtual clock at the current instant.

Source

pub fn run_until_stalled(&self) -> usize

Polls queued tasks in FIFO order until no task is queued, and returns how many polls ran.

Tasks woken while this runs are queued at the back and polled in the same call. Returns when every remaining task is waiting on a waker (a sleep, or a wake from elsewhere).

§Panics

If a task panics (the panic propagates), or if a spawn_local task is reached on a thread other than the owner thread.

Source

pub fn advance(&self, d: Duration)

Moves the virtual clock forward by d, firing sleeps as their deadlines are reached.

First runs queued tasks. Then, for each distinct deadline ≤ now + d in order: sets the clock to that deadline, fires every sleep due then (in registration order) and runs the woken tasks until stalled. Sleeps registered on the way are included if they fall due within d. Finally the clock is set to now + d.

Source

pub fn pending_timers(&self) -> usize

The number of sleeps registered and not yet fired (including ones whose future was dropped).

Source

pub fn next_timer_deadline(&self) -> Option<Instant>

The earliest deadline among pending sleeps, if any.

Source

pub fn queued_tasks(&self) -> usize

The number of tasks queued to be polled.

Trait Implementations§

Source§

impl Clone for TestExecutor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TestExecutor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TestExecutor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Executor for TestExecutor

Source§

fn spawn_local(&self, fut: LocalBoxFuture<'static, ()>) -> Result<(), NotLocal>

Ok on the owner thread; Err(NotLocal) elsewhere.

Source§

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>)

Queued as an ordinary task: the closure runs during run_until_stalled.

Source§

fn sleep(&self, d: Duration) -> BoxFuture<'static, ()>

Completes when advance reaches now() + d; at once if d is zero.

Source§

fn spawn(&self, fut: BoxFuture<'static, ()>)

Spawns a Send future to run to completion in the background.
Source§

fn now(&self) -> Instant

The current instant on this executor’s clock (virtual for test executors).

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<S> Embed<HNil, HNil> for S

Source§

fn project(&self) -> HNil

The sub-environment.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T

Source§

fn pipe<F, R>(self, f: F) -> R
where F: FnOnce(Self) -> R,

Applies f to self (F#-style forward pipe).
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.