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

A dummy SleepProvider instance for testing.

The MockSleepProvider ignores the current time, and instead keeps its own view of the current Instant and SystemTime. You can advance them in-step by calling advance(), and you can simulate jumps in the system clock by calling jump().

This is not for production use.

Implementations§

source§

impl MockSleepProvider

source

pub fn new(wallclock: SystemTime) -> Self

Create a new MockSleepProvider, starting at a given wall-clock time.

source

pub async fn advance(&self, dur: Duration)

Advance the simulated timeline forward by dur.

Calling this function will wake any pending futures as appropriate, and yield to the scheduler so they get a chance to run.

Limitations

This function advances time in one big step. We might instead want to advance in small steps and make sure that each step’s futures can get run before the ones scheduled to run after it.

source

pub fn jump_to(&self, new_wallclock: SystemTime)

Simulate a discontinuity in the system clock, by jumping to new_wallclock.

Panics

Panics if we have already panicked while holding the lock on the internal timer state, and the lock is poisoned.

Trait Implementations§

source§

impl Clone for MockSleepProvider

source§

fn clone(&self) -> MockSleepProvider

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 MockSleepProvider

source§

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

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

impl Default for MockSleepProvider

source§

fn default() -> Self

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

impl SleepProvider for MockSleepProvider

§

type SleepFuture = Sleeping

A future returned by SleepProvider::sleep()
source§

fn sleep(&self, duration: Duration) -> Self::SleepFuture

Return a future that will be ready after duration has elapsed.
source§

fn block_advance<T: Into<String>>(&self, reason: T)

Signify that a test running under mock time shouldn’t advance time yet, with a given unique reason string. This is useful for making sure (mock) time doesn’t advance while things that might require some (real-world) time to complete do so, such as spawning a task on another thread. Read more
source§

fn release_advance<T: Into<String>>(&self, reason: T)

Signify that the reason to withhold time advancing provided in a call to block_advance no longer exists, and it’s fine to move time forward if nothing else is blocking advances. Read more
source§

fn allow_one_advance(&self, dur: Duration)

Allow a test running under mock time to advance time by the provided duration, even if the above block_advance API has been used. Read more
source§

fn now(&self) -> Instant

Return the SleepProvider’s view of the current instant. Read more
source§

fn wallclock(&self) -> SystemTime

Return the SleepProvider’s view of the current wall-clock time. 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> SleepProviderExt for T
where T: SleepProvider,

source§

fn timeout<F>( &self, duration: Duration, future: F ) -> Timeout<F, Self::SleepFuture>
where F: Future,

Wrap a Future with a timeout. Read more
source§

fn sleep_until_wallclock( &self, when: SystemTime ) -> SleepUntilWallclock<'_, Self>

Pause until the wall-clock is at when or later, trying to recover from clock jumps. 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