summaryrefslogtreecommitdiff
path: root/test/sys/test_timerfd.rs
blob: 24fb2ac002e1da959a377956d04aedf13c2df4ef (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use nix::sys::time::{TimeSpec, TimeValLike};
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags};
use std::time::Instant;

#[test]
pub fn test_timerfd_oneshot() {
    let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();

    let before = Instant::now();

    timer
        .set(
            Expiration::OneShot(TimeSpec::seconds(1)),
            TimerSetTimeFlags::empty(),
        )
        .unwrap();

    timer.wait().unwrap();

    let millis = before.elapsed().as_millis();
    assert!(millis > 900);
}

#[test]
pub fn test_timerfd_interval() {
    let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();

    let before = Instant::now();
    timer
        .set(
            Expiration::IntervalDelayed(TimeSpec::seconds(1), TimeSpec::seconds(2)),
            TimerSetTimeFlags::empty(),
        )
        .unwrap();

    timer.wait().unwrap();

    let start_delay = before.elapsed().as_millis();
    assert!(start_delay > 900);

    timer.wait().unwrap();

    let interval_delay = before.elapsed().as_millis();
    assert!(interval_delay > 2900);
}

#[test]
pub fn test_timerfd_unset() {
    let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();

    timer
        .set(
            Expiration::OneShot(TimeSpec::seconds(1)),
            TimerSetTimeFlags::empty(),
        )
        .unwrap();

    timer.unset().unwrap();

    assert!(timer.get().unwrap() == None);
}