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

use async_trait::async_trait;

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

#[async_trait]
pub trait Sleeper: Send + Sync {
    async fn sleep(&self, duration: Duration);
}

pub struct SystemClock(u64);

impl SystemClock {
    pub const fn default() -> Self {
        Self(1)
    }

    pub const fn accelerated(scale: u64) -> Self {
        Self(scale)
    }
}

impl Clock for SystemClock {
    fn current_timestamp(&self) -> u64 {
        SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            * self.0
    }
}

pub struct TokioSleeper(pub u32);

impl TokioSleeper {
    pub const fn default() -> Self {
        Self(1)
    }

    pub const fn accelerated(scale: u32) -> Self {
        Self(scale)
    }
}

#[async_trait]
impl Sleeper for TokioSleeper {
    async fn sleep(&self, duration: Duration) {
        tokio::time::sleep(duration / self.0).await
    }
}

pub struct Insomniac;

#[async_trait]
impl Sleeper for Insomniac {
    async fn sleep(&self, _duration: Duration) {}
}