use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use time::OffsetDateTime;
use crate::id::Id;
pub trait Environment: Send + Sync {
fn now(&self) -> OffsetDateTime;
fn new_id(&self) -> Id;
}
pub struct SystemEnvironment;
impl Environment for SystemEnvironment {
fn now(&self) -> OffsetDateTime {
OffsetDateTime::now_utc()
}
fn new_id(&self) -> Id {
Id::new()
}
}
pub struct FixedEnvironment {
start: OffsetDateTime,
step: time::Duration,
ticks: AtomicU64,
ids: AtomicU64,
}
impl FixedEnvironment {
pub fn new(start: OffsetDateTime, step: time::Duration) -> Self {
Self {
start,
step,
ticks: AtomicU64::new(0),
ids: AtomicU64::new(0),
}
}
pub fn epoch() -> Self {
Self::new(OffsetDateTime::UNIX_EPOCH, time::Duration::seconds(1))
}
}
impl Environment for FixedEnvironment {
fn now(&self) -> OffsetDateTime {
let n = self.ticks.fetch_add(1, Ordering::SeqCst);
self.start + self.step * (n as i32)
}
fn new_id(&self) -> Id {
let n = self.ids.fetch_add(1, Ordering::SeqCst);
let mut bytes = [0u8; 16];
bytes[8..].copy_from_slice(&n.to_be_bytes());
Id::from_bytes(bytes)
}
}
pub struct ShiftedEnvironment {
start: OffsetDateTime,
origin: Instant,
}
impl ShiftedEnvironment {
pub fn starting_at(start: OffsetDateTime) -> Self {
Self {
start,
origin: Instant::now(),
}
}
pub fn starting_at_ms(ms: u64) -> Option<Self> {
let ns = i128::from(ms).checked_mul(1_000_000)?;
OffsetDateTime::from_unix_timestamp_nanos(ns)
.ok()
.map(Self::starting_at)
}
}
impl Environment for ShiftedEnvironment {
fn now(&self) -> OffsetDateTime {
self.start + self.origin.elapsed()
}
fn new_id(&self) -> Id {
Id::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_fixed_clock_advances_one_step_per_reading() {
let env = FixedEnvironment::epoch();
let a = env.now();
let b = env.now();
assert_eq!(a, OffsetDateTime::UNIX_EPOCH);
assert_eq!(
b - a,
time::Duration::seconds(1),
"entries must still order"
);
}
#[test]
fn a_shifted_clock_starts_where_it_is_told_and_keeps_running() {
let start = OffsetDateTime::UNIX_EPOCH + time::Duration::days(20_000);
let env = ShiftedEnvironment::starting_at(start);
let a = env.now();
std::thread::sleep(std::time::Duration::from_millis(5));
let b = env.now();
assert!(a >= start && a - start < time::Duration::seconds(1));
assert!(b > a, "it runs");
assert_ne!(env.new_id(), env.new_id());
let from_ms = ShiftedEnvironment::starting_at_ms(1_761_955_200_000)
.unwrap()
.now();
assert_eq!(from_ms.date().to_string(), "2025-11-01");
}
#[test]
fn two_runs_of_the_same_length_agree_on_every_answer() {
let one = FixedEnvironment::epoch();
let two = FixedEnvironment::epoch();
for _ in 0..8 {
assert_eq!(one.now(), two.now());
assert_eq!(one.new_id(), two.new_id());
}
}
#[test]
fn fixed_identifiers_are_still_distinct() {
let env = FixedEnvironment::epoch();
let ids: Vec<Id> = (0..64).map(|_| env.new_id()).collect();
let mut sorted: Vec<String> = ids.iter().map(|i| i.to_string()).collect();
sorted.sort();
sorted.dedup();
assert_eq!(
sorted.len(),
64,
"a counter that repeats is not an id source"
);
}
#[test]
fn the_system_environment_does_not_repeat_itself() {
let env = SystemEnvironment;
assert_ne!(env.new_id(), env.new_id());
}
}