Struct tor_rtmock::task::MockExecutor

source ·
pub struct MockExecutor { /* private fields */ }
Expand description

Executor for running tests with mocked environment

For test cases which don’t actually wait for anything in the real world.

This is the executor. It implements Spawn and BlockOn

It will usually be used as part of a MockRuntime.

§Restricted environment

Tests run with this executor must not attempt to block on anything “outside”: every future that anything awaits must (eventually) be woken directly by some other task in the same test case.

(By directly we mean that the Waker::wake call is made by that waking future, before that future itself awaits anything.)

§Panics

This executor will malfunction or panic if reentered.

Implementations§

source§

impl MockExecutor

source

pub fn new() -> Self

Make a MockExecutor with default parameters

source

pub fn with_scheduling(scheduling: SchedulingPolicy) -> Self

Make a MockExecutor with a specific SchedulingPolicy

source§

impl MockExecutor

source

pub fn spawn_identified( &self, desc: impl Display, fut: impl Future<Output = ()> + Send + 'static ) -> impl Debug + Clone + Send + 'static

Spawn a task and return something to identify it

desc should Display as some kind of short string (ideally without spaces) and will be used in the Debug impl and trace log messages from MockExecutor.

The returned value is an opaque task identifier which is very cheap to clone and which can be used by the caller in debug logging, if it’s desired to correlate with the debug output from MockExecutor. Most callers will want to ignore it.

This method is infalliable. (The MockExecutor cannot be shut down.)

source

pub fn spawn_join<T: Debug + Send + 'static>( &self, desc: impl Display, fut: impl Future<Output = T> + Send + 'static ) -> impl Future<Output = T>

Spawn a task and return its output for further usage

desc should Display as some kind of short string (ideally without spaces) and will be used in the Debug impl and trace log messages from MockExecutor.

source§

impl MockExecutor

source

pub fn progress_until_stalled(&self) -> impl Future<Output = ()>

Run tasks in the current executor until every other task is waiting

§Panics

Might malfunction or panic if more than one such call is running at once.

(Ie, you must .await or drop the returned Future before calling this method again.)

Must be called and awaited within a future being run by self.

source§

impl MockExecutor

source

pub fn n_tasks(&self) -> usize

Return the number of tasks running in this executor

One possible use is for a test case to check that task(s) that ought to have exited, have indeed done so.

In the usual case, the answer will be at least 1, because it counts the future passed to block_on (perhaps via MockRuntime::test_with_various).

source§

impl MockExecutor

source

pub fn debug_dump(&self)

Dump the executor’s state including backtraces of waiting tasks, to stderr

This is considerably more extensive than simply MockExecutor as Debug.

(This is a convenience method, which wraps MockExecutor::as_debug_dump().

§Backtrace salience (possible spurious traces)

Summary

The technique used to capture backtraces when futures sleep is not 100% exact. It will usually show all the actual sleeping sites, but it might also show other backtraces which were part of the implementation of some complex relevant future.

Details

When a future’s implementation wants to sleep, it needs to record the Waker (from the Context) so that the “other end” can call .wake() on it later, when the future should be woken.

Since Context.waker() gives &Waker, borrowed from the Context, the future must clone the Waker, and it must do so in within the poll() call.

A future which is waiting in a select! will typically show multiple traces, one for each branch. But, if a future sleeps on one thing, and then when polled again later, sleeps on something different, without waking up in between, both backtrace locations will be shown. And, a complicated future contraption might clone the Waker more times. So not every backtrace will necessarily be informative.

§Panics

Panics on write errors.

source

pub fn as_debug_dump(&self) -> DebugDump<'_>

Dump the executor’s state including backtraces of waiting tasks

This is considerably more extensive than simply MockExecutor as Debug.

Returns an object for formatting with Debug. To simply print the dump to stderr (eg in a test), use .debug_dump().

Backtrace salience (possible spurious traces) - see .debug_dump().

Trait Implementations§

source§

impl BlockOn for MockExecutor

source§

fn block_on<F>(&self, fut: F) -> F::Output
where F: Future,

Run fut to completion, synchronously

§Panics

Might malfunction or panic if:

  • The provided future doesn’t complete (without externally blocking), but instead waits for something.

  • The MockExecutor is reentered. (Eg, block_on is reentered.)

source§

impl Clone for MockExecutor

source§

fn clone(&self) -> MockExecutor

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for MockExecutor

source§

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

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

impl Default for MockExecutor

source§

fn default() -> MockExecutor

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

impl Spawn for MockExecutor

source§

fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError>

Spawns a future that will be run to completion. Read more
source§

fn status(&self) -> Result<(), SpawnError>

Determines whether the executor is able to spawn new tasks. Read more

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.

source§

impl<T> Instrument for T

source§

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

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

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.

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<Sp> SpawnExt for Sp
where Sp: Spawn + ?Sized,

source§

fn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError>
where Fut: Future<Output = ()> + Send + 'static,

Available on crate feature alloc only.
Spawns a task that polls the given future with output () to completion. Read more
source§

fn spawn_with_handle<Fut>( &self, future: Fut ) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError>
where Fut: Future + Send + 'static, <Fut as Future>::Output: Send,

Available on crate features channel and std only.
Spawns a task that polls the given future to completion and returns a future that resolves to the spawned future’s output. Read more
source§

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

§

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

§

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

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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