blob: 89470f2b4b89c0091fd4735552db40ff2f5c987f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#[cfg(feature = "thread")]
use rustix::thread::nanosleep;
use rustix::time::{clock_gettime, ClockId, Timespec};
/// Attempt to test that the monotonic clock is monotonic. Time may or may not
/// advance, but it shouldn't regress.
#[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);
}
}
/// With the "thread" feature, we can sleep so that we're guaranteed that time
/// has advanced.
#[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);
}
/// With the "thread" feature, we can sleep so that we're guaranteed that time
/// has advanced.
#[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);
}
|