#[cfg(feature = "thread")]
use rustix::thread::nanosleep;
use rustix::time::{clock_gettime, ClockId, Timespec};
#[test]
fn test_monotonic_clock() {
let a = clock_gettime(ClockId::Monotonic);
let b = clock_gettime(ClockId::Monotonic);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}
#[cfg(feature = "thread")]
#[test]
fn test_monotonic_clock_with_sleep_1s() {
let a = clock_gettime(ClockId::Monotonic);
let _rem = nanosleep(&Timespec {
tv_sec: 1,
tv_nsec: 0,
});
let b = clock_gettime(ClockId::Monotonic);
assert!(b.tv_sec > a.tv_sec);
}
#[cfg(feature = "thread")]
#[test]
fn test_monotonic_clock_with_sleep_1ms() {
let a = clock_gettime(ClockId::Monotonic);
let _rem = nanosleep(&Timespec {
tv_sec: 0,
tv_nsec: 1_000_000,
});
let b = clock_gettime(ClockId::Monotonic);
assert!(b.tv_sec >= a.tv_sec);
assert!(b.tv_sec != a.tv_sec || b.tv_nsec > a.tv_nsec);
}