Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed the public status of
TimerSpec
, as it should not be exposed…
… to the user. Implemented `FromRawFd` for `TimerFd` as it already implements `AsRawFd`. Addressed comments from the latest code review: - Removed upper bound assertions on timer expirations in tests. - Made the main example runnable and added code to show how to wait for the timer. - Refactored `ClockId` to use `libc_enum`. - Added comments for all public parts of the module. - Wrapped to 80 cols. - Changed the size of the buffer in the tests to the minimum required.
- Loading branch information
1 parent
cd70cf3
commit 4a3f5df
Showing
2 changed files
with
105 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,49 @@ | ||
use nix::errno::Errno; | ||
use nix::sys::time::{TimeSpec, TimeValLike}; | ||
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags}; | ||
use nix::unistd::read; | ||
use nix::Error; | ||
use std::os::unix::io::AsRawFd; | ||
use std::time::Instant; | ||
|
||
#[test] | ||
pub fn test_timerfd_oneshot() { | ||
let mut buffer = [0u8; 64]; | ||
let fd = TimerFd::new(ClockId::Monotonic, TimerFlags::empty()).unwrap(); | ||
let mut buffer = [0u8; 8]; | ||
let fd = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()) | ||
.unwrap(); | ||
|
||
let before = Instant::now(); | ||
|
||
fd.settime( | ||
TimerSetTimeFlags::empty(), | ||
Expiration::OneShot(TimeSpec::seconds(1)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
|
||
let millis = before.elapsed().as_millis(); | ||
assert!(millis < 1100 && millis > 900); | ||
assert!(millis > 900); | ||
} | ||
|
||
#[test] | ||
pub fn test_timerfd_interval() { | ||
let mut buffer = [0u8; 64]; | ||
let fd = TimerFd::new(ClockId::Monotonic, TimerFlags::empty()).unwrap(); | ||
let mut buffer = [0u8; 8]; | ||
let fd = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()) | ||
.unwrap(); | ||
|
||
let before = Instant::now(); | ||
fd.settime( | ||
TimerSetTimeFlags::empty(), | ||
Expiration::IntervalDelayed(TimeSpec::seconds(1), TimeSpec::seconds(2)), | ||
TimerSetTimeFlags::empty(), | ||
) | ||
.unwrap(); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
|
||
let start_delay = before.elapsed().as_millis(); | ||
assert!(start_delay < 1100 && start_delay > 900); | ||
assert!(start_delay > 900); | ||
|
||
read(fd.as_raw_fd(), &mut buffer).unwrap(); | ||
|
||
let interval_delay = before.elapsed().as_millis(); | ||
assert!(interval_delay < 3100 && interval_delay > 2900); | ||
assert!(interval_delay > 2900); | ||
} |