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
use std::{
    sync::{Arc, RwLock},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

/// Milliseconds since epoch at 2015-05-15T00:00:00 UTC.
///
/// This is the date of the [Rust 1.0 announcement][rust-announcement].
///
/// [rust-announcement]: https://blog.rust-lang.org/2015/05/15/Rust-1.0.html
const DEFAULT_MILLIS_SINCE_EPOCH: u64 = 1431648000000;

/// Fake clock.
#[derive(Debug, Clone)]
pub struct FakeClock {
    clock: Arc<RwLock<SystemTime>>,
}

/// Create a new [`FakeClock`] with the time `2015-05-15T00:00:00 UTC`.
impl Default for FakeClock {
    fn default() -> Self {
        Self::from(Duration::from_millis(DEFAULT_MILLIS_SINCE_EPOCH))
    }
}

impl FakeClock {
    pub fn now(&self) -> SystemTime {
        *self.clock.read().unwrap()
    }

    /// Advance the clock by `duration`.
    pub fn advance(&self, duration: Duration) {
        let mut clock = self.clock.write().unwrap();
        *clock += duration;
    }

    /// Unwind the clock by `duration`.
    pub fn unwind(&self, duration: Duration) {
        let mut clock = self.clock.write().unwrap();
        *clock -= duration;
    }

    /// Set the clock to time `t`.
    pub fn set(&self, t: SystemTime) {
        let mut clock = self.clock.write().unwrap();
        *clock = t;
    }
}

/// Create a new [`FakeClock`] at time since [`UNIX_EPOCH`].
impl From<Duration> for FakeClock {
    fn from(value: Duration) -> Self {
        Self::from(UNIX_EPOCH + value)
    }
}

/// Create a new [`FakeClock`] at [`SystemTime`].
impl From<SystemTime> for FakeClock {
    fn from(t: SystemTime) -> Self {
        Self { clock: Arc::new(RwLock::new(t)) }
    }
}

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

    #[test]
    fn from_system_time() {
        let now = SystemTime::now();
        let clock = FakeClock::from(now);
        assert_eq!(clock.now(), now);
    }

    #[test]
    fn from_duration() {
        let now = SystemTime::now();
        let since_epoch = now.duration_since(UNIX_EPOCH).unwrap();
        let clock = FakeClock::from(since_epoch);
        assert_eq!(clock.now(), now);
    }

    #[test]
    fn advance_secs() {
        let now = SystemTime::now();
        let clock = FakeClock::from(now);
        clock.advance(Duration::from_secs(1));
        assert_eq!(clock.now(), now + Duration::from_secs(1));
    }

    #[test]
    fn advance_nanos() {
        let now = SystemTime::now();
        let clock = FakeClock::from(now);
        clock.advance(Duration::from_nanos(1));
        assert_eq!(clock.now(), now + Duration::from_nanos(1));
    }

    #[test]
    fn unwind_secs() {
        let now = SystemTime::now();
        let clock = FakeClock::from(now);
        clock.unwind(Duration::from_secs(1));
        assert_eq!(clock.now(), now - Duration::from_secs(1));
    }

    #[test]
    fn unwind_nanos() {
        let now = SystemTime::now();
        let clock = FakeClock::from(now);
        clock.unwind(Duration::from_nanos(1));
        assert_eq!(clock.now(), now - Duration::from_nanos(1));
    }

    #[test]
    fn set_time() {
        let clock = FakeClock::default();
        let t = SystemTime::now();
        assert_ne!(clock.now(), t);

        clock.set(t);
        assert_eq!(clock.now(), t);
    }
}