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

A single-threaded executor for testing. Exposes additional APIs for manipulating executor state and validating behavior of executed tasks.

Implementations§

source§

impl TestExecutor

source

pub fn new() -> Self

Create a new executor for testing.

source

pub fn new_with_fake_time() -> Self

Create a new single-threaded executor running with fake time.

source

pub fn now(&self) -> Time

Return the current time according to the executor.

source

pub fn set_fake_time(&self, t: Time)

Set the fake time to a given value.

§Panics

If the executor was not created with fake time

source

pub fn run_singlethreaded<F>(&mut self, main_future: F) -> F::Output
where F: Future,

Run a single future to completion on a single thread, also polling other active tasks.

source

pub fn run_until_stalled<F>(&mut self, main_future: &mut F) -> Poll<F::Output>
where F: Future + Unpin,

PollResult the future. If it is not ready, dispatch available packets and possibly try again. Timers will only fire if this executor uses fake time. Never blocks.

This function is for testing. DO NOT use this function in tests or applications that involve any interaction with other threads or processes, as those interactions may become stalled waiting for signals from “the outside world” which is beyond the knowledge of the executor.

Unpin: this function requires all futures to be Unpinable, so any !Unpin futures must first be pinned using the pin! macro.

source

pub fn wake_expired_timers(&mut self) -> bool

Wake all tasks waiting for expired timers, and return true if any task was woken.

This is intended for use in test code in conjunction with fake time.

source

pub fn wake_next_timer(&mut self) -> Option<Time>

Wake up the next task waiting for a timer, if any, and return the time for which the timer was scheduled.

This is intended for use in test code in conjunction with run_until_stalled. For example, here is how one could test that the Timer future fires after the given timeout:

let deadline = 5.seconds().after_now();
let mut future = Timer::<Never>::new(deadline);
assert_eq!(Poll::Pending, exec.run_until_stalled(&mut future));
assert_eq!(Some(deadline), exec.wake_next_timer());
assert_eq!(Poll::Ready(()), exec.run_until_stalled(&mut future));
source

pub fn next_timer() -> Option<Time>

Returns the deadline for the next timer due to expire.

source

pub async fn advance_to(time: Time)

Advances fake time to the specified time. This will only work if the executor is being run via TestExecutor::run_until_stalled and can only be called by one task at a time. This will make sure that repeating timers fire as expected.

§Panics

Panics if the executor was not created with fake time, and for the same reasons poll_until_stalled can below.

source

pub async fn poll_until_stalled<T>( fut: impl Future<Output = T> + Unpin ) -> Poll<T>

Runs the future until it is ready or the executor is stalled. Returns the state of the future.

This will only work if the executor is being run via TestExecutor::run_until_stalled and can only be called by one task at a time.

This can be used in tests to assert that a future should be pending:

assert!(
    TestExecutor::poll_until_stalled(my_fut).await.is_pending(),
    "my_fut should not be ready!"
);

If you just want to know when the executor is stalled, you can do:

let _: Poll<()> = TestExecutor::poll_until_stalled(future::pending::<()>()).await;
§Panics

Panics if another task is currently trying to use run_until_stalled, or the executor is not using TestExecutor::run_until_stalled.

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

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>,

§

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more