#[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 = if time_direct >= time_from_function {
time_direct - time_from_function
} else {
time_from_function - time_direct
};
assert!(
difference <= 10,
"Time difference should be small, but got {}ms",
difference
);
}
#[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");
}
#[test]
fn test_current_time_millis_increases_bis() {
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_bis() {
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 = if time_direct >= time_from_function {
time_direct - time_from_function
} else {
time_from_function - time_direct
};
assert!(
difference <= 10,
"Time difference should be small, but got {}ms",
difference
);
}
#[test]
fn test_current_time_millis_precision_bis() {
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");
}
}