-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added a `From` implementation for `libc::timespec` -> `TimeSpec`. * Reworked the example with the new changes and changed the timer from 5 to 1 second. * Added a constructor for a 0-initialized `TimerSpec`. * Added a new method to get the timer configured expiration (based on timerfd_gettime). * Added an helper method to unset the expiration of the timer. * Added a `wait` method to actually read from the timer. * Renamed `settime` into just `set`. * Refactored the tests and added a new one that tests both the `unset` and the `get` method.
- Loading branch information
1 parent
4a3f5df
commit 05cd7f1
Showing
3 changed files
with
156 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,61 @@ | ||
use nix::sys::time::{TimeSpec, TimeValLike}; | ||
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags}; | ||
use nix::unistd::read; | ||
use std::os::unix::io::AsRawFd; | ||
use std::time::Instant; | ||
|
||
#[test] | ||
pub fn test_timerfd_oneshot() { | ||
let mut buffer = [0u8; 8]; | ||
let fd = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()) | ||
.unwrap(); | ||
let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap(); | ||
|
||
let before = Instant::now(); | ||
|
||
fd.settime( | ||
Expiration::OneShot(TimeSpec::seconds(1)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
timer | ||
.set( | ||
Expiration::OneShot(TimeSpec::seconds(1)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
timer.wait().unwrap(); | ||
|
||
let millis = before.elapsed().as_millis(); | ||
assert!(millis > 900); | ||
} | ||
|
||
#[test] | ||
pub fn test_timerfd_interval() { | ||
let mut buffer = [0u8; 8]; | ||
let fd = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()) | ||
.unwrap(); | ||
let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap(); | ||
|
||
let before = Instant::now(); | ||
fd.settime( | ||
Expiration::IntervalDelayed(TimeSpec::seconds(1), TimeSpec::seconds(2)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
timer | ||
.set( | ||
Expiration::IntervalDelayed(TimeSpec::seconds(1), TimeSpec::seconds(2)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
timer.wait().unwrap(); | ||
|
||
let start_delay = before.elapsed().as_millis(); | ||
assert!(start_delay > 900); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
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); | ||
} |