Skip to main content

syncular_testkit/
deterministic.rs

1/// Deterministic id generator for tests that need stable row ids, commit ids,
2/// or command ids.
3#[derive(Debug, Clone)]
4pub struct DeterministicIds {
5    prefix: String,
6    next: u64,
7}
8
9impl DeterministicIds {
10    pub fn new(prefix: impl Into<String>) -> Self {
11        Self {
12            prefix: prefix.into(),
13            next: 0,
14        }
15    }
16
17    pub fn next_id(&mut self) -> String {
18        self.next = self.next.saturating_add(1);
19        format!("{}-{}", self.prefix, self.next)
20    }
21}
22
23/// Fake millisecond clock with explicit advancement.
24#[derive(Debug, Clone)]
25pub struct FakeClock {
26    now_ms: i64,
27}
28
29impl FakeClock {
30    pub fn new(now_ms: i64) -> Self {
31        Self { now_ms }
32    }
33
34    pub fn now_ms(&self) -> i64 {
35        self.now_ms
36    }
37
38    pub fn advance_ms(&mut self, delta_ms: i64) -> i64 {
39        self.now_ms = self.now_ms.saturating_add(delta_ms);
40        self.now_ms
41    }
42}
43
44impl Default for FakeClock {
45    fn default() -> Self {
46        Self::new(1_700_000_000_000)
47    }
48}