pub struct MockSleeper { /* private fields */ }Expand description
A manually controlled elapsed-time sleeper for deterministic tests.
This type implements Sleeper by waiting until manually controlled mock
elapsed time reaches the requested target. When the tokio feature is
enabled, it also implements AsyncSleeper with the same mock elapsed-time
semantics.
§Testing guidance
Use this type when the code under test sleeps for retry, backoff, polling,
or timeout intervals. MockSleeper controls elapsed sleep time only; it
does not change the current time returned by crate::MockClock. If a
component depends on both current-time reads and sleep completion, inject a
MockClock and a MockSleeper separately and advance each one explicitly.
Implementations§
Source§impl MockSleeper
impl MockSleeper
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a mock sleeper whose elapsed time starts at zero.
§Returns
A new mock sleeper with zero elapsed time.
Sourcepub fn wait_for_blocking_sleepers(
&self,
count: usize,
timeout: Duration,
) -> bool
pub fn wait_for_blocking_sleepers( &self, count: usize, timeout: Duration, ) -> bool
Waits until at least count synchronous sleeps are blocked.
This method is intended for deterministic tests that need to advance
mock time only after another thread has entered Sleeper::sleep_for.
§Arguments
count- The minimum number of blocked synchronous sleepers.timeout- The maximum real duration to wait.
§Returns
true if the requested number of sleepers is observed before the
timeout expires; otherwise false.
Sourcepub fn set_elapsed(&self, elapsed: Duration)
pub fn set_elapsed(&self, elapsed: Duration)
Sets the current mock elapsed time.
This wakes all blocking and asynchronous sleepers so they can recheck their target elapsed time.
§Arguments
elapsed- The new elapsed time.
Trait Implementations§
Source§impl Clone for MockSleeper
impl Clone for MockSleeper
Source§fn clone(&self) -> MockSleeper
fn clone(&self) -> MockSleeper
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more