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
impl MockExecutor
sourcepub fn with_scheduling(scheduling: SchedulingPolicy) -> Self
pub fn with_scheduling(scheduling: SchedulingPolicy) -> Self
Make a MockExecutor
with a specific SchedulingPolicy
source§impl MockExecutor
impl MockExecutor
sourcepub fn spawn_identified(
&self,
desc: impl Display,
fut: impl Future<Output = ()> + Send + 'static
) -> impl Debug + Clone + Send + 'static
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.)
sourcepub fn spawn_join<T: Debug + Send + 'static>(
&self,
desc: impl Display,
fut: impl Future<Output = T> + Send + 'static
) -> impl Future<Output = T>
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
impl MockExecutor
sourcepub fn progress_until_stalled(&self) -> impl Future<Output = ()>
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
impl MockExecutor
sourcepub fn n_tasks(&self) -> usize
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
impl MockExecutor
sourcepub fn debug_dump(&self)
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.
sourcepub fn as_debug_dump(&self) -> DebugDump<'_>
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
impl BlockOn for MockExecutor
source§fn block_on<F>(&self, fut: F) -> F::Outputwhere
F: Future,
fn block_on<F>(&self, fut: F) -> F::Outputwhere
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
impl Clone for MockExecutor
source§fn clone(&self) -> MockExecutor
fn clone(&self) -> MockExecutor
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for MockExecutor
impl Debug for MockExecutor
source§impl Default for MockExecutor
impl Default for MockExecutor
source§fn default() -> MockExecutor
fn default() -> MockExecutor
Auto Trait Implementations§
impl Freeze for MockExecutor
impl RefUnwindSafe for MockExecutor
impl Send for MockExecutor
impl Sync for MockExecutor
impl Unpin for MockExecutor
impl UnwindSafe for MockExecutor
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> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moresource§impl<Sp> SpawnExt for Sp
impl<Sp> SpawnExt for Sp
source§fn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError>
fn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError>
alloc
only.()
to
completion. Read moresource§fn spawn_with_handle<Fut>(
&self,
future: Fut
) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError>
fn spawn_with_handle<Fut>( &self, future: Fut ) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError>
channel
and std
only.