From f3bd6ed8116b89cbe6dfb3c942048dae09375685 Mon Sep 17 00:00:00 2001 From: Vincent Dagonneau Date: Tue, 3 Mar 2020 16:30:30 +0100 Subject: Adding an implementation and some basic tests for timerfd. Removed support for timerfd on Android as it seems to have been deprecated? See https://android.googlesource.com/platform/development/+/73a5a3b/ndk/platforms/android-20/include/sys/timerfd.h or https://github.com/rust-lang/libc/issues/1589 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. * Ran rustfmt. * 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. Modified CHANGELOG. --- test/sys/mod.rs | 2 ++ test/sys/test_timerfd.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/sys/test_timerfd.rs (limited to 'test') diff --git a/test/sys/mod.rs b/test/sys/mod.rs index 3dc430ee..c4391c72 100644 --- a/test/sys/mod.rs +++ b/test/sys/mod.rs @@ -41,3 +41,5 @@ mod test_pthread; target_os = "netbsd", target_os = "openbsd"))] mod test_ptrace; +#[cfg(any(target_os = "android", target_os = "linux"))] +mod test_timerfd; diff --git a/test/sys/test_timerfd.rs b/test/sys/test_timerfd.rs new file mode 100644 index 00000000..24fb2ac0 --- /dev/null +++ b/test/sys/test_timerfd.rs @@ -0,0 +1,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); +} -- cgit v1.2.3