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
pub mod awaker;
pub mod error_handle;
pub mod hash;
pub mod hashmap;
pub mod init_array;
pub mod init_vec;
pub mod option_handle;
pub mod random;
pub mod vec_dequeue;

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

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

#[derive(Clone)]
pub struct SystemTimer();

impl Timer for SystemTimer {
    fn now_ms(&self) -> u64 {
        let start = SystemTime::now();
        start.duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis() as u64
    }
}

#[derive(Clone, Default)]
pub struct MockTimer {
    current_value: Arc<AtomicU64>,
}

impl Timer for MockTimer {
    fn now_ms(&self) -> u64 {
        self.current_value.load(Ordering::Relaxed)
    }
}

impl MockTimer {
    pub fn fake(&self, value: u64) {
        self.current_value.store(value, Ordering::Relaxed);
    }
}