Skip to main content

reifydb_value/
clock.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4pub trait ClockNow {
5	fn now_nanos(&self) -> u64;
6
7	fn now_millis(&self) -> u64;
8}
9
10pub trait RandomBytes {
11	fn bytes_10(&self) -> [u8; 10];
12}
13
14#[cfg(test)]
15pub(crate) mod testing {
16	use std::{cell::Cell, rc::Rc};
17
18	use super::{ClockNow, RandomBytes};
19
20	#[derive(Clone)]
21	pub struct TestClock {
22		nanos: Rc<Cell<u64>>,
23	}
24
25	impl TestClock {
26		pub fn from_millis(millis: u64) -> Self {
27			Self {
28				nanos: Rc::new(Cell::new(millis * 1_000_000)),
29			}
30		}
31
32		pub fn advance_millis(&self, millis: u64) {
33			self.nanos.set(self.nanos.get() + millis * 1_000_000);
34		}
35	}
36
37	impl ClockNow for TestClock {
38		fn now_nanos(&self) -> u64 {
39			self.nanos.get()
40		}
41
42		fn now_millis(&self) -> u64 {
43			self.nanos.get() / 1_000_000
44		}
45	}
46
47	pub struct TestRng;
48
49	impl RandomBytes for TestRng {
50		fn bytes_10(&self) -> [u8; 10] {
51			[0; 10]
52		}
53	}
54}