summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-24 20:08:46 +0000
committerGitHub <noreply@github.com>2020-07-24 20:08:46 +0000
commitb1ba07447315df4cd7b414410e793b17e6c722a7 (patch)
treeca502b7e7b760e241f2daab16ccb28c9a5d61d7d /test
parent4b6b14a0c2fabb0cdedf415aa0eece2752adc47e (diff)
parentf3bd6ed8116b89cbe6dfb3c942048dae09375685 (diff)
downloadnix-b1ba07447315df4cd7b414410e793b17e6c722a7.zip
Merge #1261
1261: Adding an implementation for timerfd. r=asomers a=vdagonneau-anssi Hello, I have encountered a situation where I needed to use timerfd in addition to other fd with nix implementation of epoll. Here is a basic implementation of this feature, I also added some documentation and tests. Note: currently testing works by pausing for 1-3 seconds. Obviously it is pretty bad if we want tests to run quickly but that was my best idea at the time. Feel free to suggest any other way. Thank you! Co-authored-by: Vincent Dagonneau <vincent.dagonneau@ssi.gouv.fr>
Diffstat (limited to 'test')
-rw-r--r--test/sys/mod.rs2
-rw-r--r--test/sys/test_timerfd.rs61
2 files changed, 63 insertions, 0 deletions
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);
+}