| 1 | /*! # mock_instant |
| 2 | **_NOTE_** As of verison 0.6, the timer resolution is nanosecond-based, which may be much more accurate than the real thing |
| 3 | |
| 4 | **_NOTE_** As of version 0.5. MockClock/Instant/SystemTime have been moved to specific modules |
| 5 | |
| 6 | **_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 |
| 7 | |
| 8 | To ensure unsurprising behavior, **reset** the clock _before_ each test (if that behavior is applicable.) |
| 9 | |
| 10 | --- |
| 11 | |
| 12 | This crate allows you to test `Instant`/`Duration`/`SystemTime` code, deterministically. |
| 13 | |
| 14 | It provides a replacement `std::time::Instant` that uses a deterministic 'clock' |
| 15 | |
| 16 | You can swap out the `std::time::Instant` with this one by doing something similar to: |
| 17 | |
| 18 | ```rust |
| 19 | #[cfg(test)] |
| 20 | use mock_instant::global::Instant; |
| 21 | |
| 22 | #[cfg(not(test))] |
| 23 | use std::time::Instant; |
| 24 | ``` |
| 25 | |
| 26 | or for a `std::time::SystemTime` |
| 27 | ```rust |
| 28 | #[cfg(test)] |
| 29 | use mock_instant::global::{SystemTime, SystemTimeError}; |
| 30 | |
| 31 | #[cfg(not(test))] |
| 32 | use std::time::{SystemTime, SystemTimeError}; |
| 33 | ``` |
| 34 | |
| 35 | ```rust |
| 36 | use mock_instant::global::{MockClock, Instant}; |
| 37 | use std::time::Duration; |
| 38 | |
| 39 | let now = Instant::now(); |
| 40 | MockClock::advance(Duration::from_secs(15)); |
| 41 | MockClock::advance(Duration::from_secs(2)); |
| 42 | |
| 43 | // its been '17' seconds |
| 44 | assert_eq!(now.elapsed(), Duration::from_secs(17)); |
| 45 | ``` |
| 46 | |
| 47 | ## API: |
| 48 | |
| 49 | ```rust,compile_fail |
| 50 | // Overrides the current time to this `Duration` |
| 51 | MockClock::set_time(time: Duration) |
| 52 | |
| 53 | // Advance the current time by this `Duration` |
| 54 | MockClock::advance(time: Duration) |
| 55 | |
| 56 | // Get the current time |
| 57 | MockClock::time() -> Duration |
| 58 | |
| 59 | // Overrides the current `SystemTime` to this duration |
| 60 | MockClock::set_system_time(time: Duration) |
| 61 | |
| 62 | // Advance the current `SystemTime` by this duration |
| 63 | MockClock::advance_system_time(time: Duration) |
| 64 | |
| 65 | // Get the current `SystemTime` |
| 66 | MockClock::system_time() -> Duration |
| 67 | |
| 68 | // Determine if this MockClock was thread-local: (useful for assertions to ensure the right mode is being used) |
| 69 | MockClock::is_thread_local() -> bool |
| 70 | Instant::now().is_thread_local() -> bool |
| 71 | SystemTime::now().is_thread_local() -> bool |
| 72 | ``` |
| 73 | |
| 74 | ## Usage: |
| 75 | |
| 76 | **_NOTE_** The clock starts at `Duration::ZERO` |
| 77 | |
| 78 | 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. |
| 79 | |
| 80 | Then, before you check your time-based logic, you can advance the clock by some `Duration` (it'll freeze the time to that duration) |
| 81 | |
| 82 | You can also get the current frozen time with `MockClock::time` |
| 83 | |
| 84 | `SystemTime` is also mockable with a similar API. |
| 85 | |
| 86 | |
| 87 | ## Thread-safety: |
| 88 | |
| 89 | Two modes are provided via modules. The APIs are identical but the `MockClock` source has different behavior in different threads. |
| 90 | |
| 91 | - `mock_instant::global` |
| 92 | - `MockClock` will have a new state per thread |
| 93 | - `Instant`will have a new state per thread |
| 94 | - `SystemTime` will have a new state per thread |
| 95 | |
| 96 | - `mock_instant::thread_local` |
| 97 | - `MockClock` will have a new state per thread |
| 98 | - `Instant`will have a new state per thread |
| 99 | - `SystemTime` will have a new state per thread |
| 100 | |
| 101 | */ |
| 102 | |
| 103 | use std::time::Duration; |
| 104 | |
| 105 | mod macros; |
| 106 | |
| 107 | /// An error returned from the duration_since and elapsed methods on SystemTime, used to learn how far in the opposite direction a system time lies. |
| 108 | #[derive(Clone, Debug)] |
| 109 | pub struct SystemTimeError(Duration); |
| 110 | |
| 111 | impl SystemTimeError { |
| 112 | pub fn duration(&self) -> Duration { |
| 113 | self.0 |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl std::fmt::Display for SystemTimeError { |
| 118 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 119 | write!(f, "second time provided was later than self") |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl std::error::Error for SystemTimeError { |
| 124 | #[allow(deprecated)] |
| 125 | fn description(&self) -> &str { |
| 126 | "other time was not earlier than self" |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Thread-local state. |
| 131 | /// |
| 132 | /// This creates a new state when accessed from a new thread |
| 133 | pub mod thread_local; |
| 134 | |
| 135 | /// Global state. |
| 136 | /// |
| 137 | /// This shares its 'clock' across threads |
| 138 | pub mod global; |