#mock #test #time #instant

mock_instant

a simple way to mock an std::time::Instant

4 releases

0.2.1 Oct 14, 2020
0.2.0 Sep 25, 2020
0.1.1 Jul 12, 2020
0.1.0 Jul 1, 2020

#109 in Testing

Download history 4205/week @ 2021-07-02 5056/week @ 2021-07-09 6183/week @ 2021-07-16 2212/week @ 2021-07-23 2352/week @ 2021-07-30 2310/week @ 2021-08-06 2686/week @ 2021-08-13 2544/week @ 2021-08-20 2442/week @ 2021-08-27 2698/week @ 2021-09-03 3128/week @ 2021-09-10 3538/week @ 2021-09-17 4719/week @ 2021-09-24 4125/week @ 2021-10-01 3540/week @ 2021-10-08 3622/week @ 2021-10-15

6,741 downloads per month
Used in jupiter

0BSD license

10KB
184 lines

mock_instant

mock_instant

This crate allows you to test Instant/Duration code, deterministically per thread.

If cross-thread determinism is required, enable the sync feature:

mock_instant = { version = "0.2", features = ["sync"] }

It provides a replacement std::time::Instant that uses a deterministic thread-local 'clock'

You can swap out the std::time::Instant with this one by doing something similar to:

#[cfg(test)]
use mock_instant::Instant;

#[cfg(not(test))]
use std::time::Instant;

Example

use std::time::Duration;

let now = Instant::now();
MockClock::advance(Duration::from_secs(15));
MockClock::advance(Duration::from_secs(2));

// its been '17' seconds
assert_eq!(now.elapsed(), Duration::from_secs(17));

License: 0BSD


lib.rs:

mock_instant

This crate allows you to test Instant/Duration code, deterministically per thread.

If cross-thread determinism is required, enable the sync feature:

mock_instant = { version = "0.2", features = ["sync"] }

It provides a replacement std::time::Instant that uses a deterministic thread-local 'clock'

You can swap out the std::time::Instant with this one by doing something similar to:

#[cfg(test)]
use mock_instant::Instant;

#[cfg(not(test))]
use std::time::Instant;

Example

# use mock_instant::{MockClock, Instant};
use std::time::Duration;

let now = Instant::now();
MockClock::advance(Duration::from_secs(15));
MockClock::advance(Duration::from_secs(2));

// its been '17' seconds
assert_eq!(now.elapsed(), Duration::from_secs(17));

Dependencies

~14KB