tilo-clock 0.5.3

The sanctioned monotonic and wall clocks on every target, Unix-time helpers, and an injectable millisecond Clock with fixed and manual test clocks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Reading the time the same way on every target: [`now`] and [`wall`] are the
//! sanctioned monotonic and wall-clock reads, and [`time`] names the clock types
//! instead of `std::time`'s, which panic in a browser page. [`Clock`] is an
//! injectable time source, for deadline behaviour a test reaches without sleeping.

use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};

use time::{Duration, Instant, SystemTime, UNIX_EPOCH};

/// The clock under one module path: the types under `std::time`'s names, for signatures that
/// need one, and every reading, made through [`now`] and [`wall`]. The UI toolkit and the process
/// services name the clock through this module and never `std::time`, so a process whose clock
/// must be virtualised (a deterministic replay, a test) has one boundary to provide.
pub mod time {
    pub use crate::imp::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};
    /// A clock held at one wall time; tilo-clock's own tests hold it per thread instead.
    #[cfg(not(test))]
    pub use crate::pin_wall;
    pub use crate::{
        from_std, now, to_std, try_unix_now, try_unix_now_ms, try_unix_now_ns, unix_now,
        unix_now_i64, unix_now_ms, unix_now_ms_i64, unix_now_ns, wall, wall_frozen,
    };

    /// A file-system timestamp, as `std::fs::Metadata::modified` reports it, as a [`SystemTime`].
    pub use crate::from_std as from_file_time;

    /// Scoped clock readings used by production-path input and deadline tests.
    #[cfg(any(test, feature = "test-support"))]
    pub use crate::testing;
}

#[cfg(any(not(target_family = "wasm"), tilo_no_js))]
mod imp {
    pub use std::time::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};

    #[inline]
    pub const fn to_std(time: SystemTime) -> std::time::SystemTime {
        time
    }

    #[inline]
    pub const fn from_std(time: std::time::SystemTime) -> SystemTime {
        time
    }
}

#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
mod imp {
    pub use web_time::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};

    pub fn to_std(time: SystemTime) -> std::time::SystemTime {
        let converted = match time.duration_since(UNIX_EPOCH) {
            Ok(after) => std::time::UNIX_EPOCH.checked_add(after),
            Err(before) => std::time::UNIX_EPOCH.checked_sub(before.duration()),
        };
        converted.unwrap_or(std::time::UNIX_EPOCH)
    }

    pub fn from_std(time: std::time::SystemTime) -> SystemTime {
        let converted = match time.duration_since(std::time::UNIX_EPOCH) {
            Ok(after) => UNIX_EPOCH.checked_add(after),
            Err(before) => UNIX_EPOCH.checked_sub(before.duration()),
        };
        converted.unwrap_or(UNIX_EPOCH)
    }
}

// The page's lint pass bans std's clocks (tools/ci/wasm_clippy_conf.py), and this crate is the
// one place a build that is not the page reads them: in a page, `Instant` and `SystemTime` are
// `web_time`'s.
#[allow(
    clippy::disallowed_methods,
    reason = "off the page this crate is where std's monotonic clock is read"
)]
#[must_use]
#[inline]
pub fn now() -> Instant {
    #[cfg(any(test, feature = "test-support"))]
    if let Some(at) = testing::monotonic_override() {
        return at;
    }
    Instant::now()
}

#[cfg(any(test, feature = "test-support"))]
pub mod testing {
    use super::time::{Instant, SystemTime};
    use std::cell::Cell;

    thread_local! {
        static MONOTONIC: Cell<Option<Instant>> = const { Cell::new(None) };
    }

    pub(super) fn monotonic_override() -> Option<Instant> {
        MONOTONIC.with(Cell::get)
    }

    struct Restore(Option<Instant>);

    impl Drop for Restore {
        fn drop(&mut self) {
            MONOTONIC.with(|clock| clock.set(self.0));
        }
    }

    pub fn with_monotonic_time<T>(at: Instant, operation: impl FnOnce() -> T) -> T {
        let _restore = Restore(MONOTONIC.with(|clock| clock.replace(Some(at))));
        operation()
    }

    struct RestoreWall(Option<u64>);

    impl Drop for RestoreWall {
        fn drop(&mut self) {
            super::FROZEN_HERE_MS.with(|here| here.set(self.0));
        }
    }

    /// A test's right to pin its process's wall clock: only
    /// `tilo_test_env::in_own_process` gives one, and `policy/ui-clock-contract`
    /// refuses [`hold_wall_everywhere`] elsewhere.
    #[derive(Clone, Copy)]
    pub struct OwnProcess(());

    impl OwnProcess {
        #[doc(hidden)]
        #[must_use]
        pub const fn of_a_test_in_its_own_process() -> Self {
            Self(())
        }
    }

    /// A process-wide wall-clock pin, back to the real clock when it drops.
    pub struct WallHold {
        _process: OwnProcess,
        pinned: std::sync::Arc<super::AtomicU64>,
        replaced: Option<std::sync::Arc<super::AtomicU64>>,
    }

    impl Drop for WallHold {
        fn drop(&mut self) {
            let mut process = super::PROCESS
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if process
                .as_ref()
                .is_some_and(|pin| std::sync::Arc::ptr_eq(pin, &self.pinned))
            {
                *process = self.replaced.take();
            }
        }
    }

    /// Pin the wall clock of `process` to `at` until the test ends; every
    /// thread reads `at`.
    pub fn hold_wall_everywhere(process: OwnProcess, at: SystemTime) -> WallHold {
        let pinned = std::sync::Arc::new(super::AtomicU64::new(super::wall_ms(at)));
        // A second pin takes over from the first.
        let replaced = super::PROCESS
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .replace(pinned.clone());
        WallHold {
            _process: process,
            pinned,
            replaced,
        }
    }

    thread_local! {
        static FOLLOWED: std::cell::RefCell<Option<std::sync::Arc<std::sync::atomic::AtomicU64>>> =
            const { std::cell::RefCell::new(None) };
    }

    pub(super) fn followed_ms() -> u64 {
        FOLLOWED.with(|followed| {
            followed
                .borrow()
                .as_ref()
                .map_or(0, |wall| wall.load(std::sync::atomic::Ordering::Relaxed))
        })
    }

    /// A wall clock a test pins for the threads it owns; threads that follow
    /// none keep their own.
    #[derive(Clone, Debug, Default)]
    pub struct SharedWall(std::sync::Arc<std::sync::atomic::AtomicU64>);

    impl SharedWall {
        /// This thread reads the pinned time from now on, until
        /// [`Self::release`].
        pub fn follow(&self) {
            FOLLOWED.with(|followed| *followed.borrow_mut() = Some(self.0.clone()));
        }

        pub fn pin(&self, at: SystemTime) {
            self.0
                .store(super::wall_ms(at), std::sync::atomic::Ordering::Relaxed);
        }

        pub fn release(&self) {
            self.0.store(0, std::sync::atomic::Ordering::Relaxed);
        }
    }

    /// Pin the wall clock of this thread alone to `at`: the other tests of the
    /// process and a runtime's workers keep their own clock.
    pub fn with_wall<T>(at: SystemTime, operation: impl FnOnce() -> T) -> T {
        let ms = super::wall_ms(at);
        let _restore = RestoreWall(super::FROZEN_HERE_MS.with(|here| here.replace(Some(ms))));
        operation()
    }
}

#[cfg(test)]
mod scoped_monotonic_tests {
    use super::*;

    tilo_test_env::scenario! {
        fn a_held_clock_is_the_holding_threads_alone_and_ends_with_its_scope;

        phase a_shared_wall_pins_the_threads_following_it_until_released;
        {
            const PINNED: u64 = 123_456;
            let wall = testing::SharedWall::default();
            let follower = {
                let wall = wall.clone();
                move || {
                    wall.follow();
                    unix_now()
                }
            };
            assert_ne!(std::thread::spawn(follower.clone()).join().unwrap(), PINNED);
            wall.pin(UNIX_EPOCH + Duration::from_secs(PINNED));
            assert_eq!(std::thread::spawn(follower.clone()).join().unwrap(), PINNED);
            assert_ne!(
                unix_now(),
                PINNED,
                "a thread following none keeps its clock"
            );
            wall.release();
            assert_ne!(std::thread::spawn(follower).join().unwrap(), PINNED);
        }

        phase scoped_time_is_thread_local_nested_and_restored_after_unwind;
        {
            let at = now().checked_add(Duration::from_secs(60)).unwrap();
            testing::with_monotonic_time(at, || {
                assert_eq!(now(), at);
                let later = at.checked_add(Duration::from_secs(1)).unwrap();
                testing::with_monotonic_time(later, || assert_eq!(now(), later));
                assert_eq!(now(), at);
                assert!(std::thread::spawn(now).join().unwrap() < at);
                let panicked = std::panic::catch_unwind(|| {
                    testing::with_monotonic_time(later, || panic!("unwind the clock scope"));
                });
                assert!(panicked.is_err());
                assert_eq!(now(), at);
            });
            assert!(now() < at);
        }

        phase scoped_wall_time_is_thread_local_nested_and_restored_after_unwind;
        {
            let at = UNIX_EPOCH + Duration::from_secs(4_000_000_000);
            testing::with_wall(at, || {
                assert_eq!(wall(), at);
                let later = at + Duration::from_secs(3);
                testing::with_wall(later, || assert_eq!(wall(), later));
                assert_eq!(wall(), at);
                assert!(std::thread::spawn(wall).join().unwrap() < at);
                let panicked = std::panic::catch_unwind(|| {
                    testing::with_wall(later, || panic!("unwind the clock scope"));
                });
                assert!(panicked.is_err());
                assert_eq!(wall(), at);
            });
            assert!(wall() < at);
        }

        phase a_clock_lent_at_the_epoch_is_a_clock_the_code_reads;
        {
            // `0` is the epoch, not the absence of a hold: a bare wasm32 has no
            // `std` clock, so falling back to the machine's would trap a module lent
            // this clock.
            let (ms, held, frozen) = with_wall_ms(0, || (unix_now_ms(), wall(), wall_frozen()));
            assert_eq!(ms, 0, "the lent time is the time the code reads");
            assert_eq!(held, UNIX_EPOCH);
            assert!(frozen, "a hold at the epoch is a hold");
            assert!(
                unix_now_ms() > 0,
                "the thread's own clock runs again after the hold"
            );
        }

        phase two_threads_holding_different_wall_times_at_once_each_read_their_own;
        {
            let first = UNIX_EPOCH + Duration::from_secs(4_000_000_000);
            let second = UNIX_EPOCH + Duration::from_secs(4_100_000_000);
            let together = std::sync::Arc::new(std::sync::Barrier::new(2));
            let hold = |at: SystemTime, other: SystemTime| {
                let together = std::sync::Arc::clone(&together);
                std::thread::spawn(move || {
                    testing::with_wall(at, || {
                        together.wait();
                        assert_eq!(wall(), at);
                        assert_ne!(wall(), other);
                        together.wait();
                    });
                    together.wait();
                    assert!(wall() < first, "the hold ended with its scope");
                })
            };
            let threads = [hold(first, second), hold(second, first)];
            for thread in threads {
                thread.join().unwrap();
            }
        }
    }
}

thread_local! {
    /// The wall time this thread holds; `None` while it reads its own clock.
    static FROZEN_HERE_MS: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
}

fn frozen_ms() -> Option<u64> {
    match FROZEN_HERE_MS.with(std::cell::Cell::get) {
        Some(here) => Some(here),
        None => match followed_ms() {
            0 => match pinned_ms() {
                0 => None,
                pinned => Some(pinned),
            },
            followed => Some(followed),
        },
    }
}

/// Hold this thread's wall clock at `now_ms`; nested holds restore the one
/// before them. This is how a host lends a clock to code that has none.
pub fn with_wall_ms<T>(now_ms: u64, f: impl FnOnce() -> T) -> T {
    struct Restore(Option<u64>);
    impl Drop for Restore {
        fn drop(&mut self) {
            FROZEN_HERE_MS.with(|here| here.set(self.0));
        }
    }
    let _restore = Restore(FROZEN_HERE_MS.with(|here| here.replace(Some(now_ms))));
    f()
}

static PROCESS: std::sync::Mutex<Option<std::sync::Arc<AtomicU64>>> = std::sync::Mutex::new(None);

fn pinned_ms() -> u64 {
    PROCESS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .as_ref()
        .map_or(0, |ms| ms.load(Ordering::Relaxed))
}

#[cfg(any(test, feature = "test-support"))]
fn followed_ms() -> u64 {
    testing::followed_ms()
}

#[cfg(not(any(test, feature = "test-support")))]
const fn followed_ms() -> u64 {
    0
}

#[allow(
    clippy::disallowed_methods,
    reason = "off the page this crate is where std's wall clock is read"
)]
#[must_use]
#[inline]
pub fn wall() -> SystemTime {
    match frozen_ms() {
        Some(ms) => UNIX_EPOCH
            .checked_add(Duration::from_millis(ms))
            .unwrap_or(UNIX_EPOCH),
        None => SystemTime::now(),
    }
}

/// Pin the wall clock of the whole process to `at` for a screenshot capture,
/// so every frame shows the same date. The monotonic clock keeps running.
#[cfg(not(test))]
pub fn pin_wall(at: SystemTime) {
    let pinned = std::sync::Arc::new(AtomicU64::new(wall_ms(at)));
    *PROCESS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(pinned);
}

/// `at` as milliseconds since the epoch, never `0` (which means the clock runs).
fn wall_ms(at: SystemTime) -> u64 {
    at.duration_since(UNIX_EPOCH)
        .map_or(1, |since| saturating_u64(since.as_millis()).max(1))
}

/// Whether a clock is held here, so a wall read answers the hold.
#[must_use]
pub fn wall_frozen() -> bool {
    frozen_ms().is_some()
}

#[must_use]
#[inline]
pub fn to_std(time: SystemTime) -> std::time::SystemTime {
    imp::to_std(time)
}

#[must_use]
#[inline]
pub fn from_std(time: std::time::SystemTime) -> SystemTime {
    imp::from_std(time)
}

fn since_epoch() -> Option<Duration> {
    wall().duration_since(UNIX_EPOCH).ok()
}

fn saturating_u64(value: u128) -> u64 {
    u64::try_from(value).unwrap_or(u64::MAX)
}

pub fn try_unix_now() -> Option<u64> {
    since_epoch().map(|duration| duration.as_secs())
}

pub fn try_unix_now_ms() -> Option<u64> {
    since_epoch().map(|duration| saturating_u64(duration.as_millis()))
}

pub fn try_unix_now_ns() -> Option<u64> {
    since_epoch().map(|duration| saturating_u64(duration.as_nanos()))
}

pub fn unix_now() -> u64 {
    try_unix_now().unwrap_or(0)
}

pub fn unix_now_ms() -> u64 {
    try_unix_now_ms().unwrap_or(0)
}

pub fn unix_now_ns() -> u64 {
    try_unix_now_ns().unwrap_or(0)
}

pub fn unix_now_i64() -> i64 {
    i64::try_from(unix_now()).unwrap_or(i64::MAX)
}

pub fn unix_now_ms_i64() -> i64 {
    i64::try_from(unix_now_ms()).unwrap_or(i64::MAX)
}

pub trait Clock: Send + Sync {
    fn now_ms(&self) -> u64;
}

#[derive(Clone, Copy, Debug, Default)]
pub struct SystemClock;

impl Clock for SystemClock {
    fn now_ms(&self) -> u64 {
        unix_now_ms()
    }
}

/// Milliseconds since this process first read a monotonic clock; never moves
/// backwards.
#[derive(Clone, Copy, Debug, Default)]
pub struct MonotonicClock;

impl Clock for MonotonicClock {
    #[allow(
        clippy::disallowed_methods,
        reason = "off the page this crate is where std's monotonic clock is read"
    )]
    fn now_ms(&self) -> u64 {
        static START: OnceLock<Instant> = OnceLock::new();
        saturating_u64(START.get_or_init(now).elapsed().as_millis())
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FixedClock(pub u64);

impl Clock for FixedClock {
    fn now_ms(&self) -> u64 {
        self.0
    }
}

#[derive(Debug, Default)]
pub struct ManualClock(AtomicU64);

impl ManualClock {
    pub const fn new(now_ms: u64) -> Self {
        Self(AtomicU64::new(now_ms))
    }

    pub fn set(&self, now_ms: u64) {
        self.0.store(now_ms, Ordering::SeqCst);
    }

    pub fn advance(&self, delta_ms: u64) {
        let _ = self
            .0
            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |now| {
                Some(now.saturating_add(delta_ms))
            });
    }
}

impl Clock for ManualClock {
    fn now_ms(&self) -> u64 {
        self.0.load(Ordering::SeqCst)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_threads_own_wall_clock_shadows_the_process_pin_only_on_that_thread() {
        const OTHER: u64 = 4_000_000_000;
        let at = UNIX_EPOCH + Duration::from_secs(OTHER);
        tilo_test_env::in_own_process(
            module_path!(),
            "a_threads_own_wall_clock_shadows_the_process_pin_only_on_that_thread",
            || {
                let process = testing::OwnProcess::of_a_test_in_its_own_process();
                let _held = testing::hold_wall_everywhere(process, at);
                assert_eq!(unix_now(), OTHER);
                assert!(wall_frozen());
                testing::with_wall(at + Duration::from_secs(1), || {
                    assert_eq!(wall(), at + Duration::from_secs(1), "the thread's own");
                    assert_eq!(
                        std::thread::spawn(wall).join().unwrap(),
                        at,
                        "another thread keeps the process's"
                    );
                });
                assert_eq!(wall(), at, "the process's, after the thread's scope");
            },
        );
    }

    tilo_test_env::scenario! {
        fn clocks_read_units_that_agree_and_a_hold_ends_with_its_scope;

        phase a_frozen_wall_clock_holds_while_the_monotonic_clock_runs;
        {
            let at = UNIX_EPOCH + Duration::from_secs(1_782_816_120);
            let before = now();
            testing::with_wall(at, || {
                assert!(wall_frozen());
                assert_eq!(wall(), at);
                assert_eq!(unix_now(), 1_782_816_120);
                assert_eq!(wall(), at, "it stays put");
                assert!(now() >= before, "the monotonic clock is not frozen");
            });
            assert!(!wall_frozen(), "the scope ended");
        }

        phase clocks_agree_on_units_move_only_as_told_and_never_backwards;
        {
            assert!(!wall_frozen(), "no other test's pinned time is read here");
            {
                let secs = unix_now();
                let ms = unix_now_ms();
                let ns = unix_now_ns();
                assert!(secs > 1_600_000_000);
                assert!(ms / 1_000 >= secs);
                assert!(ns / 1_000_000 >= ms);
                assert_eq!(unix_now_i64(), i64::try_from(unix_now()).unwrap());
            }
            {
                assert_eq!(FixedClock(7).now_ms(), 7);
                let clock = ManualClock::new(10);
                assert_eq!(clock.now_ms(), 10);
                clock.advance(5);
                assert_eq!(clock.now_ms(), 15);
                clock.set(3);
                assert_eq!(clock.now_ms(), 3);
                clock.set(u64::MAX - 1);
                clock.advance(10);
                assert_eq!(clock.now_ms(), u64::MAX);
            }
            {
                let first = MonotonicClock.now_ms();
                assert!(MonotonicClock.now_ms() >= first);
            }
        }

        phase now_is_monotonic;
        {
            let earlier = now();
            assert!(now() >= earlier);
        }

        phase wall_is_after_the_epoch;
        {
            assert!(wall().duration_since(UNIX_EPOCH).is_ok());
        }

        phase std_conversions_round_trip_on_both_sides_of_the_epoch;
        {
            let offset = Duration::from_millis(1_700_000_000_123);
            for time in [UNIX_EPOCH + offset, UNIX_EPOCH - offset, UNIX_EPOCH] {
                assert_eq!(from_std(to_std(time)), time);
            }
            assert_eq!(to_std(UNIX_EPOCH + offset), std::time::UNIX_EPOCH + offset);
        }
    }
}