mock_instant
NOTE As of version 0.5. MockClock/Instant/SystemTime have been moved to specific modules
NOTE The modules, global
and thread_local
change the behavior across threads. If global
is used, the clock keeps its state across threads, otherwise if thread_local
is used, a new source is made for each thread
To ensure unsurprising behavior, reset the clock before each test (if that behavior is applicable.)
This crate allows you to test Instant
/Duration
/SystemTime
code, deterministically.
It provides a replacement std::time::Instant
that uses a deterministic 'clock'
You can swap out the std::time::Instant
with this one by doing something similar to:
use Instant;
use Instant;
or for a std::time::SystemTime
use ;
use ;
use ;
use Duration;
let now = now;
advance;
advance;
// its been '17' seconds
assert_eq!;
API:
// Overrides the current time to this `Duration`
set_time
// Advance the current time by this `Duration`
advance
// Get the current time
time // Overrides the current `SystemTime` to this duration
set_system_time
// Advance the current `SystemTime` by this duration
sdvance_system_time
// Get the current `SystemTime`
system_time // Determine if this MockClock was thread-local: (useful for assertions to ensure the right mode is being used)
is_thread_local .is_thread_local .is_thread_local
Usage:
NOTE The clock starts at Duration::ZERO
In your tests, you can use MockClock::set_time(Duration::ZERO)
to reset the clock back to 0. Or, you can set it to some sentinel time value.
Then, before you check your time-based logic, you can advance the clock by some Duration
(it'll freeze the time to that duration)
You can also get the current frozen time with MockClock::time
SystemTime
is also mockable with a similar API.
Thread-safety:
Two modes are provided via modules. The APIs are identical but the MockClock
source has different behavior in different threads.
-
mock_instant::global
MockClock
will have a new state per threadInstant
will have a new state per threadSystemTime
will have a new state per thread
-
mock_instant::thread_local
MockClock
will have a new state per threadInstant
will have a new state per threadSystemTime
will have a new state per thread
License: 0BSD