This crate allows you to test Instant/Duration code, deterministically per thread.
If cross-thread determinism is required, enable the sync
feature:
toml
mock_instant = { version = "0.3", features = ["sync"] }
It provides a replacement std::time::Instant
and std::time::SystemTime
that uses a deterministic thread-local 'clock'
You can swap out the std::time::Instant
with this one by doing something similar to:
```rust
use mock_instant::Instant;
use std::time::Instant; ```
or for a std::time::SystemTime
```
use mock_instant::{SystemTime, SystemTimeError};
use std::time::{SystemTime, SystemTimeError}; ```
```rust use std::time::Duration;
let now = Instant::now(); MockClock::advance(Duration::fromsecs(15)); MockClock::advance(Duration::fromsecs(2));
// its been '17' seconds asserteq!(now.elapsed(), Duration::fromsecs(17)); ```
```
use std::time::Duration;
let now = SystemTime::now(); MockClock::advancesystemtime(Duration::fromsecs(15)); MockClock::advancesystemtime(Duration::fromsecs(2));
// its been '17' seconds asserteq!(now.elapsed().unwrap(), Duration::fromsecs(17)); ```
License: 0BSD