| 1 |
macro_rules! define_mock_clock { |
| 2 |
($thread_local:expr; $(#[$outer:meta])*) => { |
| 3 |
/// A Mock clock |
| 4 |
/// |
| 5 |
$(#[$outer])* |
| 6 |
#[derive(Copy, Clone)] |
| 7 |
pub struct MockClock; |
| 8 |
|
| 9 |
impl std::fmt::Debug for MockClock { |
| 10 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 11 |
f.debug_struct("MockClock") |
| 12 |
.field("time", &Self::time()) |
| 13 |
.field("system_time", &Self::system_time()) |
| 14 |
.finish() |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
impl MockClock { |
| 19 |
/// Set the internal [`Instant`] clock to this [`Duration`] |
| 20 |
pub fn set_time(time: Duration) { |
| 21 |
self::with_time(|t| *t = time); |
| 22 |
} |
| 23 |
|
| 24 |
/// Advance the internal [`Instant`] clock by this [`Duration`] |
| 25 |
pub fn advance(time: Duration) { |
| 26 |
self::with_time(|t| *t += time); |
| 27 |
} |
| 28 |
|
| 29 |
/// Get the current [`Instant`] duration |
| 30 |
pub fn time() -> Duration { |
| 31 |
self::get_time() |
| 32 |
} |
| 33 |
|
| 34 |
/// Set the internal [`SystemTime`] clock to this [`Duration`] |
| 35 |
pub fn set_system_time(time: Duration) { |
| 36 |
self::with_system_time(|t| *t = time); |
| 37 |
} |
| 38 |
|
| 39 |
/// Advance the internal [`SystemTime`] clock by this [`Duration`] |
| 40 |
pub fn advance_system_time(time: Duration) { |
| 41 |
self::with_system_time(|t| *t += time); |
| 42 |
} |
| 43 |
|
| 44 |
/// Get the current [`SystemTime`] duration |
| 45 |
pub fn system_time() -> Duration { |
| 46 |
self::get_system_time() |
| 47 |
} |
| 48 |
|
| 49 |
/// Is this MockClock thread-local? |
| 50 |
pub const fn is_thread_local() -> bool { |
| 51 |
$thread_local |
| 52 |
} |
| 53 |
} |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
macro_rules! define_instant { |
| 58 |
($now:expr ; $thread_local:expr ; $(#[$outer:meta])* ) => { |
| 59 |
/// A simple deterministic [`std::time::Instant`] wrapped around a modifiable [`std::time::Duration`] |
| 60 |
/// |
| 61 |
$(#[$outer])* |
| 62 |
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 63 |
pub struct Instant(Duration); |
| 64 |
|
| 65 |
impl Instant { |
| 66 |
pub fn now() -> Self { |
| 67 |
Self($now()) |
| 68 |
} |
| 69 |
|
| 70 |
pub fn duration_since(&self, earlier: Self) -> Duration { |
| 71 |
self.checked_duration_since(earlier).unwrap_or_default() |
| 72 |
} |
| 73 |
|
| 74 |
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> { |
| 75 |
self.0.checked_sub(earlier.0) |
| 76 |
} |
| 77 |
|
| 78 |
pub fn saturating_duration_since(&self, earlier: Self) -> Duration { |
| 79 |
self.checked_duration_since(earlier).unwrap_or_default() |
| 80 |
} |
| 81 |
|
| 82 |
pub fn elapsed(&self) -> Duration { |
| 83 |
$now() - self.0 |
| 84 |
} |
| 85 |
|
| 86 |
pub fn checked_add(&self, duration: Duration) -> Option<Self> { |
| 87 |
self.0.checked_add(duration).map(Self) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn checked_sub(&self, duration: Duration) -> Option<Self> { |
| 91 |
self.0.checked_sub(duration).map(Self) |
| 92 |
} |
| 93 |
|
| 94 |
/// Is this Instant thread-local? |
| 95 |
pub const fn is_thread_local(&self) -> bool { |
| 96 |
$thread_local |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl std::ops::Add<Duration> for Instant { |
| 101 |
type Output = Self; |
| 102 |
fn add(self, rhs: Duration) -> Self::Output { |
| 103 |
Self(self.0 + rhs) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl std::ops::AddAssign<Duration> for Instant { |
| 108 |
fn add_assign(&mut self, rhs: Duration) { |
| 109 |
self.0 += rhs |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl std::ops::Sub for Instant { |
| 114 |
type Output = Duration; |
| 115 |
fn sub(self, rhs: Self) -> Self::Output { |
| 116 |
self.duration_since(rhs) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl std::ops::Sub<Duration> for Instant { |
| 121 |
type Output = Instant; |
| 122 |
fn sub(self, rhs: Duration) -> Self::Output { |
| 123 |
self.checked_sub(rhs) |
| 124 |
.expect("overflow when substracting duration from instant") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl std::ops::SubAssign<Duration> for Instant { |
| 129 |
fn sub_assign(&mut self, rhs: Duration) { |
| 130 |
self.0 -= rhs |
| 131 |
} |
| 132 |
} |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
macro_rules! define_system_time { |
| 137 |
($now:expr; $thread_local:expr; $(#[$outer:meta])*) => { |
| 138 |
/// A simple deterministic [`std::time::SystemTime`] wrapped around a modifiable [`std::time::Duration`] |
| 139 |
/// |
| 140 |
/// The source is the [`MockClock`] |
| 141 |
/// |
| 142 |
$(#[$outer])* |
| 143 |
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 144 |
pub struct SystemTime(Duration); |
| 145 |
|
| 146 |
/// A mocked UNIX_EPOCH. This starts at `0` rather than the traditional unix epoch date. |
| 147 |
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::ZERO); |
| 148 |
|
| 149 |
impl SystemTime { |
| 150 |
pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH; |
| 151 |
|
| 152 |
pub fn now() -> Self { |
| 153 |
Self($now()) |
| 154 |
} |
| 155 |
|
| 156 |
pub fn duration_since( |
| 157 |
&self, |
| 158 |
earlier: SystemTime, |
| 159 |
) -> Result<Duration, $crate::SystemTimeError> { |
| 160 |
self.0 |
| 161 |
.checked_sub(earlier.0) |
| 162 |
.ok_or_else(|| $crate::SystemTimeError(earlier.0 - self.0)) |
| 163 |
} |
| 164 |
|
| 165 |
pub fn elapsed(&self) -> Result<Duration, $crate::SystemTimeError> { |
| 166 |
Self::now().duration_since(*self) |
| 167 |
} |
| 168 |
|
| 169 |
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> { |
| 170 |
self.0.checked_add(duration).map(Self) |
| 171 |
} |
| 172 |
|
| 173 |
pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> { |
| 174 |
self.0.checked_sub(duration).map(Self) |
| 175 |
} |
| 176 |
|
| 177 |
/// Is this SystemTime thread-local? |
| 178 |
pub const fn is_thread_local(&self) -> bool { |
| 179 |
$thread_local |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl std::ops::Add<Duration> for SystemTime { |
| 184 |
type Output = SystemTime; |
| 185 |
|
| 186 |
fn add(self, rhs: Duration) -> Self::Output { |
| 187 |
self.checked_add(rhs) |
| 188 |
.expect("overflow when adding duration to instant") |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl std::ops::AddAssign<Duration> for SystemTime { |
| 193 |
fn add_assign(&mut self, rhs: Duration) { |
| 194 |
*self = *self + rhs |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
impl std::ops::Sub<Duration> for SystemTime { |
| 199 |
type Output = SystemTime; |
| 200 |
|
| 201 |
fn sub(self, rhs: Duration) -> Self::Output { |
| 202 |
self.checked_sub(rhs) |
| 203 |
.expect("overflow when subtracting duration from instant") |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
impl std::ops::SubAssign<Duration> for SystemTime { |
| 208 |
fn sub_assign(&mut self, rhs: Duration) { |
| 209 |
*self = *self - rhs |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
impl From<std::time::SystemTime> for SystemTime { |
| 214 |
fn from(value: std::time::SystemTime) -> Self { |
| 215 |
Self( |
| 216 |
value |
| 217 |
.duration_since(std::time::SystemTime::UNIX_EPOCH) |
| 218 |
.expect("std::time::SystemTime is before UNIX_EPOCH"), |
| 219 |
) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
impl From<SystemTime> for std::time::SystemTime { |
| 224 |
fn from(value: SystemTime) -> Self { |
| 225 |
Self::UNIX_EPOCH + value.0 |
| 226 |
} |
| 227 |
} |
| 228 |
}; |
| 229 |
} |
| 230 |
|
| 231 |
macro_rules! define_instant_tests { |
| •232 |
($takelock:stmt) => { |
| 233 |
#[cfg(test)] |
| 234 |
mod common { |
| 235 |
use super::*; |
| 236 |
|
| 237 |
fn reset_time() { |
| 238 |
MockClock::set_time(Duration::ZERO) |
| 239 |
} |
| 240 |
|
| 241 |
fn reset_system_time() { |
| 242 |
MockClock::set_system_time(Duration::ZERO) |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn set_system_time() { |
| •247 |
$takelock |
| 248 |
reset_system_time(); |
| 249 |
|
| 250 |
MockClock::set_system_time(Duration::from_secs(42)); |
| 251 |
assert_eq!(MockClock::system_time(), Duration::from_secs(42)); |
| 252 |
|
| 253 |
reset_system_time(); |
| 254 |
assert_eq!(MockClock::system_time(), Duration::ZERO); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn advance_system_time() { |
| •259 |
$takelock |
| 260 |
reset_system_time(); |
| 261 |
|
| 262 |
for i in 0..3 { |
| 263 |
MockClock::advance_system_time(Duration::from_millis(100)); |
| 264 |
let time = Duration::from_millis(100 * (i + 1)); |
| 265 |
assert_eq!(MockClock::system_time(), time); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn system_time() { |
| •271 |
$takelock |
| 272 |
reset_system_time(); |
| 273 |
|
| 274 |
let now = SystemTime::now(); |
| 275 |
for i in 0..3 { |
| 276 |
MockClock::advance_system_time(Duration::from_millis(100)); |
| 277 |
assert_eq!(now.elapsed().unwrap(), Duration::from_millis(100 * (i + 1))); |
| 278 |
} |
| 279 |
MockClock::advance_system_time(Duration::from_millis(100)); |
| 280 |
|
| 281 |
let next = SystemTime::now(); |
| 282 |
assert_eq!( |
| 283 |
next.duration_since(now).unwrap(), |
| 284 |
Duration::from_millis(400) |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn system_time_methods() { |
| •290 |
$takelock |
| 291 |
reset_system_time(); |
| 292 |
|
| 293 |
let system_time = SystemTime::now(); |
| 294 |
let interval = Duration::from_millis(42); |
| 295 |
MockClock::advance_system_time(interval); |
| 296 |
|
| 297 |
// zero + 1 = 1 |
| 298 |
assert_eq!( |
| 299 |
system_time.checked_add(Duration::from_millis(1)).unwrap(), |
| 300 |
SystemTime(Duration::from_millis(1)) |
| 301 |
); |
| 302 |
|
| 303 |
// now + 1 = diff + 1 |
| 304 |
assert_eq!( |
| 305 |
SystemTime::now() |
| 306 |
.checked_add(Duration::from_millis(1)) |
| 307 |
.unwrap(), |
| 308 |
SystemTime(Duration::from_millis(43)) |
| 309 |
); |
| 310 |
|
| 311 |
// zero - 1 = None |
| 312 |
assert!(system_time.checked_sub(Duration::from_millis(1)).is_none()); |
| 313 |
|
| 314 |
// now - 1 = diff -1 |
| 315 |
assert_eq!( |
| 316 |
SystemTime::now() |
| 317 |
.checked_sub(Duration::from_millis(1)) |
| 318 |
.unwrap(), |
| 319 |
SystemTime(Duration::from_millis(41)) |
| 320 |
); |
| 321 |
|
| 322 |
// now - 1 = diff - 1 |
| 323 |
assert_eq!( |
| 324 |
SystemTime::now() - Duration::from_millis(1), |
| 325 |
SystemTime(Duration::from_millis(41)) |
| 326 |
); |
| 327 |
|
| 328 |
// now - diff + 1 = none |
| 329 |
assert!(SystemTime::now() |
| 330 |
.checked_sub(Duration::from_millis(43)) |
| 331 |
.is_none()); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn system_time_sub_millisecond_precision() { |
| •336 |
$takelock |
| 337 |
reset_system_time(); |
| 338 |
|
| 339 |
let t1 = SystemTime::UNIX_EPOCH; |
| 340 |
let t2 = SystemTime::UNIX_EPOCH |
| 341 |
.checked_add(Duration::from_micros(100)) |
| 342 |
.unwrap(); |
| 343 |
assert!(t2 > t1, "{t2:?} > {t1:?}"); |
| 344 |
|
| 345 |
let t3 = t2.checked_sub(Duration::from_nanos(1)).unwrap(); |
| 346 |
assert!(t2 > t3, "{t2:?} > {t3:?}"); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn system_time_from_std_roundtrip() { |
| •351 |
$takelock |
| 352 |
let std_now = std::time::SystemTime::now(); |
| 353 |
let mock_now: SystemTime = std_now.into(); |
| 354 |
assert!(mock_now.0 > Duration::from_secs(1708041600)); // Friday 16 February 2024 00:00:00 GMT |
| 355 |
let roundtrip_now: std::time::SystemTime = mock_now.into(); |
| 356 |
assert_eq!(std_now, roundtrip_now) |
| 357 |
} |
| 358 |
|
| 359 |
#[test] |
| 360 |
fn set_time() { |
| •361 |
$takelock |
| 362 |
reset_time(); |
| 363 |
|
| 364 |
MockClock::set_time(Duration::from_secs(42)); |
| 365 |
assert_eq!(MockClock::time(), Duration::from_secs(42)); |
| 366 |
|
| 367 |
reset_time(); |
| 368 |
assert_eq!(MockClock::time(), Duration::ZERO); |
| 369 |
} |
| 370 |
|
| 371 |
#[test] |
| 372 |
fn advance() { |
| •373 |
$takelock |
| 374 |
reset_time(); |
| 375 |
|
| 376 |
for i in 0..3 { |
| 377 |
MockClock::advance(Duration::from_millis(100)); |
| 378 |
let time = Duration::from_millis(100 * (i + 1)); |
| 379 |
assert_eq!(MockClock::time(), time); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn instant() { |
| •385 |
$takelock |
| 386 |
reset_time(); |
| 387 |
|
| 388 |
let now = Instant::now(); |
| 389 |
for i in 0..3 { |
| 390 |
MockClock::advance(Duration::from_millis(100)); |
| 391 |
assert_eq!(now.elapsed(), Duration::from_millis(100 * (i + 1))); |
| 392 |
} |
| 393 |
MockClock::advance(Duration::from_millis(100)); |
| 394 |
|
| 395 |
let next = Instant::now(); |
| 396 |
assert_eq!(next.duration_since(now), Duration::from_millis(400)); |
| 397 |
} |
| 398 |
|
| 399 |
#[test] |
| 400 |
fn methods() { |
| •401 |
$takelock |
| 402 |
reset_time(); |
| 403 |
|
| 404 |
let instant = Instant::now(); |
| 405 |
let interval = Duration::from_millis(42); |
| 406 |
MockClock::advance(interval); |
| 407 |
|
| 408 |
// 0 - now = None |
| 409 |
assert!(instant.checked_duration_since(Instant::now()).is_none()); |
| 410 |
|
| 411 |
// now - 0 = diff |
| 412 |
assert_eq!( |
| 413 |
Instant::now().checked_duration_since(instant).unwrap(), |
| 414 |
interval |
| 415 |
); |
| 416 |
|
| 417 |
// 0 since now = 0 |
| 418 |
assert_eq!( |
| 419 |
instant.saturating_duration_since(Instant::now()), |
| 420 |
Duration::ZERO |
| 421 |
); |
| 422 |
|
| 423 |
// now since 0 = diff |
| 424 |
assert_eq!(Instant::now().saturating_duration_since(instant), interval); |
| 425 |
|
| 426 |
// 0 since now = 0 - same behavior as saturating_duration_since |
| 427 |
assert_eq!(instant.duration_since(Instant::now()), Duration::ZERO); |
| 428 |
|
| 429 |
// now since 0 = diff |
| 430 |
assert_eq!(Instant::now().duration_since(instant), interval); |
| 431 |
|
| 432 |
// zero + 1 = 1 |
| 433 |
assert_eq!( |
| 434 |
instant.checked_add(Duration::from_millis(1)).unwrap(), |
| 435 |
Instant(Duration::from_millis(1)) |
| 436 |
); |
| 437 |
|
| 438 |
// now + 1 = diff + 1 |
| 439 |
assert_eq!( |
| 440 |
Instant::now() |
| 441 |
.checked_add(Duration::from_millis(1)) |
| 442 |
.unwrap(), |
| 443 |
Instant(Duration::from_millis(43)) |
| 444 |
); |
| 445 |
|
| 446 |
// zero - 1 = None |
| 447 |
assert!(instant.checked_sub(Duration::from_millis(1)).is_none()); |
| 448 |
|
| 449 |
// now - 1 = diff -1 |
| 450 |
assert_eq!( |
| 451 |
Instant::now() |
| 452 |
.checked_sub(Duration::from_millis(1)) |
| 453 |
.unwrap(), |
| 454 |
Instant(Duration::from_millis(41)) |
| 455 |
); |
| 456 |
|
| 457 |
// now - 1 = diff - 1 |
| 458 |
assert_eq!( |
| 459 |
Instant::now() - Duration::from_millis(1), |
| 460 |
Instant(Duration::from_millis(41)) |
| 461 |
); |
| 462 |
|
| 463 |
// now - diff + 1 = none |
| 464 |
assert!(Instant::now() |
| 465 |
.checked_sub(Duration::from_millis(43)) |
| 466 |
.is_none()); |
| 467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn instant_sub_millisecond_precision() { |
| •471 |
$takelock |
| 472 |
reset_time(); |
| 473 |
|
| 474 |
let t1 = Instant::now(); |
| 475 |
let t2 = t1.checked_add(Duration::from_micros(100)).unwrap(); |
| 476 |
assert!(t2 > t1, "{t2:?} > {t1:?}"); |
| 477 |
|
| 478 |
let t3 = t2.checked_add(Duration::from_nanos(1)).unwrap(); |
| 479 |
assert!(t3 > t2, "{t3:?} > {t2:?}"); |
| 480 |
|
| 481 |
let t4 = t3.checked_sub(Duration::from_nanos(1)).unwrap(); |
| 482 |
assert_eq!(t4, t2); |
| 483 |
|
| 484 |
let t5 = t4.checked_sub(Duration::from_micros(100)).unwrap(); |
| 485 |
assert_eq!(t5, t1); |
| 486 |
} |
| 487 |
} |
| 488 |
}; |
| 489 |
} |
| 490 |
|
| 491 |
pub(super) use define_instant; |
| 492 |
pub(super) use define_instant_tests; |
| 493 |
pub(super) use define_mock_clock; |
| 494 |
pub(super) use define_system_time; |