pub struct Clock<Tz: TimeZone> { /* private fields */ }Expand description
A testable source of time
By using a Clock as the source of time in your code, you can
choose in testing to replace it with a fake. Your test harness can
then control how your production code sees time passing.
Implementations§
Source§impl<Tz: TimeZone> Clock<Tz>
impl<Tz: TimeZone> Clock<Tz>
Sourcepub fn new_with_timezone(timezone: Tz) -> Self
pub fn new_with_timezone(timezone: Tz) -> Self
Create a new clock in this TimeZone
Sourcepub fn new_fake(start: DateTime<Tz>) -> Self
pub fn new_fake(start: DateTime<Tz>) -> Self
Create a new fake clock in this [‘TimeZone’].
Note that the time reported by this clock will not change from start until
the clock is advanced using Clock::advance().
Sourcepub fn is_fake(&self) -> bool
pub fn is_fake(&self) -> bool
Check whether the clock is fake
Using this in production code defeats the point, but you may wish to use it in utilities that could be called from either production or test code.
Sourcepub fn split(&self) -> Clock<Tz>
pub fn split(&self) -> Clock<Tz>
Divide this clock
This is equivalent to Clone when using the default, wall
clock. The following discussion pertains only to the fake
clock case which is intended to be used in testing; in that
sense, getting it wrong is likely to be caught by the tests
themselves.
When using the fake clock, things become more complicated
to enable testing. Clones of the same clock are divided into
groups. When you call clone, you get a new clock in the same
group as the clock you clone. If you want a Clock in a new group
of its own, you must call Clock::split.
For the clock to advance, all groups must either be sleeping or advancing - that means someone must have called either sleep or advance on exactly one instance in each group. So you can think of a group as being the clocks in one execution context (task/thread).
Sourcepub async fn advance(&mut self, duration: Duration) -> (DateTime<Tz>, Duration)
pub async fn advance(&mut self, duration: Duration) -> (DateTime<Tz>, Duration)
Move a fake clock forward by some duration
This will panic if called on a wall clock.
Sourcepub fn advance_blocking(
&mut self,
duration: Duration,
) -> (DateTime<Tz>, Duration)
pub fn advance_blocking( &mut self, duration: Duration, ) -> (DateTime<Tz>, Duration)
Move a fake clock forward by some duration
This will panic if called on a wall clock.
Sourcepub fn sleep_blocking(&mut self, duration: Duration)
pub fn sleep_blocking(&mut self, duration: Duration)
Sleep for some duration
This should not be called from an async context.
Trait Implementations§
Auto Trait Implementations§
impl<Tz> Freeze for Clock<Tz>where
Tz: Freeze,
impl<Tz> RefUnwindSafe for Clock<Tz>where
Tz: RefUnwindSafe,
impl<Tz> Send for Clock<Tz>
impl<Tz> Sync for Clock<Tz>
impl<Tz> Unpin for Clock<Tz>where
Tz: Unpin,
impl<Tz> UnwindSafe for Clock<Tz>where
Tz: UnwindSafe,
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,
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 more