• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // For the moment, the only platform specific code is related to tests.
6 #![cfg(test)]
7 
8 use base::Timer;
9 
10 use super::FdExecutor;
11 use super::URingExecutor;
12 use crate::sys::unix::executor;
13 use crate::AsyncResult;
14 use crate::TimerAsync;
15 
16 impl TimerAsync {
new_poll(timer: Timer, ex: &FdExecutor) -> AsyncResult<TimerAsync>17     pub(crate) fn new_poll(timer: Timer, ex: &FdExecutor) -> AsyncResult<TimerAsync> {
18         executor::async_poll_from(timer, ex).map(|io_source| TimerAsync { io_source })
19     }
20 
new_uring(timer: Timer, ex: &URingExecutor) -> AsyncResult<TimerAsync>21     pub(crate) fn new_uring(timer: Timer, ex: &URingExecutor) -> AsyncResult<TimerAsync> {
22         executor::async_uring_from(timer, ex).map(|io_source| TimerAsync { io_source })
23     }
24 }
25 
26 mod tests {
27     use std::time::Duration;
28     use std::time::Instant;
29 
30     use super::*;
31     use crate::sys::unix::uring_executor::is_uring_stable;
32     use crate::Executor;
33 
34     #[test]
timer()35     fn timer() {
36         async fn this_test(ex: &Executor) {
37             let dur = Duration::from_millis(200);
38             let now = Instant::now();
39             TimerAsync::sleep(ex, dur).await.expect("unable to sleep");
40             assert!(now.elapsed() >= dur);
41         }
42 
43         let ex = Executor::new().expect("creating an executor failed");
44         ex.run_until(this_test(&ex)).unwrap();
45     }
46 
47     #[test]
one_shot()48     fn one_shot() {
49         if !is_uring_stable() {
50             return;
51         }
52 
53         async fn this_test(ex: &URingExecutor) {
54             let mut tfd = Timer::new().expect("failed to create timerfd");
55 
56             let dur = Duration::from_millis(200);
57             let now = Instant::now();
58             tfd.reset(dur, None).expect("failed to arm timer");
59 
60             let t = TimerAsync::new_uring(tfd, ex).unwrap();
61             let count = t.next_val().await.expect("unable to wait for timer");
62 
63             assert_eq!(count, 1);
64             assert!(now.elapsed() >= dur);
65         }
66 
67         let ex = URingExecutor::new().unwrap();
68         ex.run_until(this_test(&ex)).unwrap();
69     }
70 
71     #[test]
one_shot_fd()72     fn one_shot_fd() {
73         async fn this_test(ex: &FdExecutor) {
74             let mut tfd = Timer::new().expect("failed to create timerfd");
75 
76             let dur = Duration::from_millis(200);
77             let now = Instant::now();
78             tfd.reset(dur, None).expect("failed to arm timer");
79 
80             let t = TimerAsync::new_poll(tfd, ex).unwrap();
81             let count = t.next_val().await.expect("unable to wait for timer");
82 
83             assert_eq!(count, 1);
84             assert!(now.elapsed() >= dur);
85         }
86 
87         let ex = FdExecutor::new().unwrap();
88         ex.run_until(this_test(&ex)).unwrap();
89     }
90 }
91