pub struct SimpleMockTimeProvider { /* private fields */ }
Expand description
Simple provider of simulated time
Maintains a mocked view of the current Instant
and SystemTime
.
The simulated time advances only when explicitly instructed,
by calling .advance()
.
The wallclock time can be warped with
.jump_wallclock()
,
allowing simulation of wall clock non-monotonicity.
§Panics and aborts
Panics on time under/overflow.
May cause an abort if the SimpleMockTimeProvider
implementation contains bugs.
Implementations§
Source§impl SimpleMockTimeProvider
impl SimpleMockTimeProvider
Sourcepub fn new(now: Instant, wallclock: SystemTime) -> Self
pub fn new(now: Instant, wallclock: SystemTime) -> Self
Return a new mock time provider starting at a specified point in time
Sourcepub fn from_real() -> Self
pub fn from_real() -> Self
Return a new mock time provider starting at the current actual (non-mock) time
Like any SimpleMockTimeProvider
, the time is frozen and only changes
due to calls to advance
.
Sourcepub fn from_wallclock(wallclock: SystemTime) -> Self
pub fn from_wallclock(wallclock: SystemTime) -> Self
Return a new mock time provider starting at a specified wallclock time
The monotonic time (Instant
) starts at the current actual (non-mock) time.
(Absolute values of the real monotonic time are not readily
observable or distinguishable from Rust,
nor can a fixed Instant
be constructed,
so this is usually sufficient for a reproducible test.)
Sourcepub fn advance(&self, d: Duration)
pub fn advance(&self, d: Duration)
Advance the simulated time by d
This advances both the Instant
(monotonic time)
and SystemTime
(wallclock time)
by the same amount.
Will wake sleeping SleepFuture
s, as appropriate.
Note that the tasks which were waiting on those now-expired SleepFuture
s
will only actually execute when they are next polled.
advance
does not yield to the executor or poll any futures.
The executor will (presumably) poll those woken tasks, when it regains control.
But the order in which the tasks run will depend on its scheduling policy,
and might be different to the order implied by the futures’ timeout values.
To simulate normal time advancement, wakeups, and task activations,
use MockExecutor::advance_*()
.
Sourcepub fn jump_wallclock(&self, new_wallclock: SystemTime)
pub fn jump_wallclock(&self, new_wallclock: SystemTime)
Warp the wallclock time
This has no effect on any sleeping futures.
It only affects the return value from .wallclock()
.
Sourcepub fn time_until_next_timeout(&self) -> Option<Duration>
pub fn time_until_next_timeout(&self) -> Option<Duration>
When will the next timeout occur?
Returns the duration until the next SleepFuture
should wake up.
Advancing time by at least this amount will wake up that future, and any others with the same wakeup time.
Will never return Some(ZERO)
:
any future that is supposed to wake up now (or earlier) has indeed already been woken,
so it is no longer sleeping and isn’t included in the calculation.
Trait Implementations§
Source§impl Clone for SimpleMockTimeProvider
impl Clone for SimpleMockTimeProvider
Source§fn clone(&self) -> SimpleMockTimeProvider
fn clone(&self) -> SimpleMockTimeProvider
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl CoarseTimeProvider for SimpleMockTimeProvider
impl CoarseTimeProvider for SimpleMockTimeProvider
Source§fn now_coarse(&self) -> CoarseInstant
fn now_coarse(&self) -> CoarseInstant
CoarseTimeProvider
’s view of the current instant. Read moreSource§impl Debug for SimpleMockTimeProvider
impl Debug for SimpleMockTimeProvider
Source§impl Default for SimpleMockTimeProvider
Default
makes a Provider
which starts at whatever the current real time is
impl Default for SimpleMockTimeProvider
Default
makes a Provider
which starts at whatever the current real time is
Source§impl SleepProvider for SimpleMockTimeProvider
impl SleepProvider for SimpleMockTimeProvider
Source§type SleepFuture = SleepFuture
type SleepFuture = SleepFuture
SleepProvider::sleep()
Source§fn sleep(&self, d: Duration) -> SleepFuture ⓘ
fn sleep(&self, d: Duration) -> SleepFuture ⓘ
duration
has
elapsed.Source§fn wallclock(&self) -> SystemTime
fn wallclock(&self) -> SystemTime
Source§fn block_advance<T>(&self, _reason: T)
fn block_advance<T>(&self, _reason: T)
Source§fn release_advance<T>(&self, _reason: T)
fn release_advance<T>(&self, _reason: T)
block_advance
no
longer exists, and it’s fine to move time forward if nothing else is blocking advances. Read moreSource§fn allow_one_advance(&self, _dur: Duration)
fn allow_one_advance(&self, _dur: Duration)
block_advance
API has been used. Read moreAuto Trait Implementations§
impl Freeze for SimpleMockTimeProvider
impl RefUnwindSafe for SimpleMockTimeProvider
impl Send for SimpleMockTimeProvider
impl Sync for SimpleMockTimeProvider
impl Unpin for SimpleMockTimeProvider
impl UnwindSafe for SimpleMockTimeProvider
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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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<T> SleepProviderExt for Twhere
T: SleepProvider,
impl<T> SleepProviderExt for Twhere
T: SleepProvider,
Source§fn timeout<F>(
&self,
duration: Duration,
future: F,
) -> Timeout<F, Self::SleepFuture>where
F: Future,
fn timeout<F>(
&self,
duration: Duration,
future: F,
) -> Timeout<F, Self::SleepFuture>where
F: Future,
Source§fn sleep_until_wallclock(
&self,
when: SystemTime,
) -> SleepUntilWallclock<'_, Self>
fn sleep_until_wallclock( &self, when: SystemTime, ) -> SleepUntilWallclock<'_, Self>
when
or later, trying to
recover from clock jumps. Read more