1use nix::sys::time::{TimeSpec, TimeValLike};
2use nix::sys::timerfd::{
3 ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags,
4};
5use std::time::Instant;
6
7#[test]
8pub fn test_timerfd_oneshot() {
9 let timer =
10 TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
11
12 let before = Instant::now();
13
14 timer
15 .set(
16 Expiration::OneShot(TimeSpec::seconds(1)),
17 TimerSetTimeFlags::empty(),
18 )
19 .unwrap();
20
21 timer.wait().unwrap();
22
23 let millis = before.elapsed().as_millis();
24 assert!(millis > 900);
25}
26
27#[test]
28pub fn test_timerfd_interval() {
29 let timer =
30 TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
31
32 let before = Instant::now();
33 timer
34 .set(
35 Expiration::IntervalDelayed(
36 TimeSpec::seconds(1),
37 TimeSpec::seconds(2),
38 ),
39 TimerSetTimeFlags::empty(),
40 )
41 .unwrap();
42
43 timer.wait().unwrap();
44
45 let start_delay = before.elapsed().as_millis();
46 assert!(start_delay > 900);
47
48 timer.wait().unwrap();
49
50 let interval_delay = before.elapsed().as_millis();
51 assert!(interval_delay > 2900);
52}
53
54#[test]
55pub fn test_timerfd_unset() {
56 let timer =
57 TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
58
59 timer
60 .set(
61 Expiration::OneShot(TimeSpec::seconds(1)),
62 TimerSetTimeFlags::empty(),
63 )
64 .unwrap();
65
66 timer.unset().unwrap();
67
68 assert!(timer.get().unwrap().is_none());
69}
70