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_stalledpolls 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.advancemoves the virtual clock forward, firing due sleeps in(deadline, registration order)order and running the woken tasks at each deadline.nowis the virtual clock. It starts at the wall-clock instant ofnewand moves only throughadvance.
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
impl TestExecutor
Sourcepub fn new() -> Self
pub fn new() -> Self
A new executor owned by the calling thread, with the virtual clock at the current instant.
Sourcepub fn run_until_stalled(&self) -> usize
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.
Sourcepub fn advance(&self, d: Duration)
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.
Sourcepub fn pending_timers(&self) -> usize
pub fn pending_timers(&self) -> usize
The number of sleeps registered and not yet fired (including ones whose future was dropped).
Sourcepub fn next_timer_deadline(&self) -> Option<Instant>
pub fn next_timer_deadline(&self) -> Option<Instant>
The earliest deadline among pending sleeps, if any.
Sourcepub fn queued_tasks(&self) -> usize
pub fn queued_tasks(&self) -> usize
The number of tasks queued to be polled.
Trait Implementations§
Source§impl Clone for TestExecutor
impl Clone for TestExecutor
Source§impl Debug for TestExecutor
impl Debug for TestExecutor
Source§impl Default for TestExecutor
impl Default for TestExecutor
Source§impl Executor for TestExecutor
impl Executor for TestExecutor
Source§fn spawn_local(&self, fut: LocalBoxFuture<'static, ()>) -> Result<(), NotLocal>
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>)
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, ()>
fn sleep(&self, d: Duration) -> BoxFuture<'static, ()>
Completes when advance reaches now() + d; at once if d is
zero.
Auto Trait Implementations§
impl Freeze for TestExecutor
impl RefUnwindSafe for TestExecutor
impl Send for TestExecutor
impl Sync for TestExecutor
impl Unpin for TestExecutor
impl UnsafeUnpin for TestExecutor
impl UnwindSafe for TestExecutor
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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