xref: /aosp_15_r20/external/crosvm/cros_async/src/sys/windows/timer.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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 use base::TimerTrait;
6 
7 use crate::AsyncResult;
8 use crate::IntoAsync;
9 use crate::TimerAsync;
10 
11 impl<T: TimerTrait + IntoAsync> TimerAsync<T> {
wait_sys(&self) -> AsyncResult<()>12     pub async fn wait_sys(&self) -> AsyncResult<()> {
13         self.io_source.wait_for_handle().await
14     }
15 }
16 
17 #[cfg(test)]
18 mod test {
19     use std::time::Duration;
20     use std::time::Instant;
21 
22     use super::*;
23     use crate::Executor;
24 
25     #[test]
timer()26     fn timer() {
27         async fn this_test(ex: &Executor) {
28             // On Windows, SetWaitableTimer, the underlying timer API, is not
29             // guaranteed to sleep for *at least* the supplied duration, so here
30             // we permit early wakeups.
31             let dur = Duration::from_millis(200);
32             let min_duration = Duration::from_millis(150);
33 
34             let now = Instant::now();
35             TimerAsync::sleep(ex, dur).await.expect("unable to sleep");
36             let actual_sleep_duration = now.elapsed();
37             assert!(actual_sleep_duration >= min_duration);
38         }
39 
40         let ex = Executor::new().expect("creating an executor failed");
41         ex.run_until(this_test(&ex)).unwrap();
42     }
43 }
44