| 1 | # mock_instant |
| 2 | |
| 3 | ## Modules |
| 4 | **_NOTE_** As of verison 0.6, the timer resolution is nanosecond-based, which may be much more accurate than the real thing |
| 5 | |
| 6 | **_NOTE_** As of version 0.5. MockClock/Instant/SystemTime have been moved to specific modules |
| 7 | |
| 8 | **_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. In most cases, especially when unit testing, `thread_local` will deliver the most expected behavior. |
| 9 | |
| 10 | To ensure unsurprising behavior, **reset** the clock _before_ each test (if that behavior is applicable.) |
| 11 | |
| 12 | ## Basics |
| 13 | |
| 14 | This crate allows you to test `Instant`/`Duration`/`SystemTime` code, deterministically. |
| 15 | |
| 16 | It provides a replacement `std::time::Instant` that uses a deterministic 'clock' |
| 17 | |
| 18 | You can swap out the `std::time::Instant` with this one by doing something similar to: |
| 19 | |
| 20 | ```rust |
| 21 | #[cfg(test)] |
| 22 | use mock_instant::thread_local::Instant; |
| 23 | |
| 24 | #[cfg(not(test))] |
| 25 | use std::time::Instant; |
| 26 | ``` |
| 27 | |
| 28 | or for a `std::time::SystemTime` |
| 29 | |
| 30 | ```rust |
| 31 | #[cfg(test)] |
| 32 | use mock_instant::thread_local::{SystemTime, SystemTimeError}; |
| 33 | |
| 34 | #[cfg(not(test))] |
| 35 | use std::time::{SystemTime, SystemTimeError}; |
| 36 | ``` |
| 37 | |
| 38 | ```rust |
| 39 | use mock_instant::thread_local::{MockClock, Instant}; |
| 40 | use std::time::Duration; |
| 41 | |
| 42 | let now = Instant::now(); |
| 43 | MockClock::advance(Duration::from_secs(15)); |
| 44 | MockClock::advance(Duration::from_secs(2)); |
| 45 | |
| 46 | // its been '17' seconds |
| 47 | assert_eq!(now.elapsed(), Duration::from_secs(17)); |
| 48 | ``` |
| 49 | |
| 50 | ## API |
| 51 | |
| 52 | ```rust,compile_fail |
| 53 | // Overrides the current time to this `Duration` |
| 54 | MockClock::set_time(time: Duration) |
| 55 | |
| 56 | // Advance the current time by this `Duration` |
| 57 | MockClock::advance(time: Duration) |
| 58 | |
| 59 | // Get the current time |
| 60 | MockClock::time() -> Duration |
| 61 | |
| 62 | // Overrides the current `SystemTime` to this duration |
| 63 | MockClock::set_system_time(time: Duration) |
| 64 | |
| 65 | // Advance the current `SystemTime` by this duration |
| 66 | MockClock::advance_system_time(time: Duration) |
| 67 | |
| 68 | // Get the current `SystemTime` |
| 69 | MockClock::system_time() -> Duration |
| 70 | |
| 71 | // Determine if this MockClock was thread-local: (useful for assertions to ensure the right mode is being used) |
| 72 | MockClock::is_thread_local() -> bool |
| 73 | Instant::now().is_thread_local() -> bool |
| 74 | SystemTime::now().is_thread_local() -> bool |
| 75 | ``` |
| 76 | |
| 77 | ## Usage |
| 78 | |
| 79 | **_NOTE_** The clock starts at `Duration::ZERO` |
| 80 | |
| 81 | 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. |
| 82 | |
| 83 | Then, before you check your time-based logic, you can advance the clock by some `Duration` (it'll freeze the time to that duration) |
| 84 | |
| 85 | You can also get the current frozen time with `MockClock::time` |
| 86 | |
| 87 | `SystemTime` is also mockable with a similar API. |
| 88 | |
| 89 | ## Thread-safety |
| 90 | |
| 91 | Two modes are provided via modules. The APIs are identical but the `MockClock` source has different behavior in different threads. |
| 92 | |
| 93 | ### `mock_instant::thread_local` |
| 94 | |
| 95 | - `MockClock` will have an independent state per thread |
| 96 | - `Instant`will have an independent state per thread |
| 97 | - `SystemTime` will have an independent state per thread |
| 98 | |
| 99 | Using `thread_local` creates a shared clock in each thread of operation within the test application. It is recommended for unit tests and integration tests that test single-threaded behavior. |
| 100 | |
| 101 | `thread_local` will be the module used in most cases and will create the least surprise. |
| 102 | |
| 103 | ### `mock_instant::global` |
| 104 | |
| 105 | - `MockClock` will have shared state per thread |
| 106 | - `Instant`will have shared state per thread |
| 107 | - `SystemTime` will have shared state per thread |
| 108 | |
| 109 | Using `global` creates a shared clock across all thread of operation within the test application. It is recommended for integration tests that are specifically testing multi-threaded behavior. Using `global` when not testing multi-threaded behavior is likely to cause inconsistent and confusing errors. |
| 110 | |
| 111 | Rust's unit test test-runner executes unit tests in multiple threads (unless directed to run single-threaded). If multiple unit tests are running simultaneouly within the test execution, changes to the `global` clock (resetting or advancing the clock) in one test can created unexpected results in other tests that are executing simultaneously, by changing the clock when the tests assume the clock is not being changed. |
| 112 | |
| 113 | ## License |
| 114 | |
| 115 | License: 0BSD |