Debian Codegraph @

third_party/rust/tokio/tests/test_clock.rs

raw
1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3
4 use tokio::time::{self, Duration, Instant};
5
6 #[tokio::test]
7 async fn resume_lets_time_move_forward_instead_of_resetting_it() {
8 let start = Instant::now();
9 time::pause();
10 time::advance(Duration::from_secs(10)).await;
11 let advanced_by_ten_secs = Instant::now();
12 assert!(advanced_by_ten_secs - start > Duration::from_secs(10));
13 assert!(advanced_by_ten_secs - start < Duration::from_secs(11));
14 time::resume();
15 assert!(advanced_by_ten_secs < Instant::now());
16 assert!(Instant::now() - advanced_by_ten_secs < Duration::from_secs(1));
17 }
18
19 #[tokio::test]
20 async fn can_pause_after_resume() {
21 let start = Instant::now();
22 time::pause();
23 time::advance(Duration::from_secs(10)).await;
24 time::resume();
25 time::pause();
26 time::advance(Duration::from_secs(10)).await;
27 assert!(Instant::now() - start > Duration::from_secs(20));
28 assert!(Instant::now() - start < Duration::from_secs(21));
29 }
30
31 #[tokio::test]
32 #[should_panic]
33 async fn freezing_time_while_frozen_panics() {
34 time::pause();
35 time::pause();
36 }
37
38 #[tokio::test]
39 #[should_panic]
40 async fn advancing_time_when_time_is_not_frozen_panics() {
41 time::advance(Duration::from_secs(1)).await;
42 }
43
44 #[tokio::test]
45 #[should_panic]
46 async fn resuming_time_when_not_frozen_panics() {
47 time::pause();
48 time::resume();
49 time::resume();
50 }