Skip to main content

reifydb_value/
clock.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use crate::value::datetime::DateTime;
5
6pub trait ClockNow {
7	fn now(&self) -> DateTime;
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 crate::{
19		clock::{ClockNow, RandomBytes},
20		value::datetime::DateTime,
21	};
22
23	#[derive(Clone)]
24	pub struct TestClock {
25		nanos: Rc<Cell<u64>>,
26	}
27
28	impl TestClock {
29		pub fn from_millis(millis: u64) -> Self {
30			Self {
31				nanos: Rc::new(Cell::new(millis * 1_000_000)),
32			}
33		}
34
35		pub fn advance_millis(&self, millis: u64) {
36			self.nanos.set(self.nanos.get() + millis * 1_000_000);
37		}
38	}
39
40	impl ClockNow for TestClock {
41		fn now(&self) -> DateTime {
42			DateTime::from_nanos(self.nanos.get())
43		}
44	}
45
46	pub struct TestRng;
47
48	impl RandomBytes for TestRng {
49		fn bytes_10(&self) -> [u8; 10] {
50			[0; 10]
51		}
52	}
53}