#[cfg(test)]
mod tests {
use crate::current_time_millis;
use std::thread;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tracing::info;
#[test]
fn test_current_time_millis_increases() {
let time1 = current_time_millis();
thread::sleep(Duration::from_millis(5));
let time2 = current_time_millis();
assert!(time2 > time1, "Time should increase between calls");
}
#[test]
fn test_current_time_millis_is_reasonably_current() {
let time_from_function = current_time_millis();
let time_direct = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_millis() as u64;
let difference = time_direct.abs_diff(time_from_function);
assert!(
difference <= 10,
"Time difference should be small, but got {difference}ms"
);
}
#[test]
fn test_current_time_millis_precision() {
let time1 = current_time_millis();
let time2 = current_time_millis();
if time1 == time2 {
info!("Note: consecutive calls returned same time, which might happen occasionally");
}
thread::sleep(Duration::from_millis(5));
let time3 = current_time_millis();
assert!(time3 > time1, "Time should increase after sleep");
}
}