1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! A test for sleep latency in ylong_runtime.
15 
16 use std::time::{Duration, Instant};
17 
18 use ylong_runtime::builder::RuntimeBuilder;
19 #[cfg(feature = "time")]
20 use ylong_runtime::time::sleep;
21 use ylong_runtime::{block_on, spawn};
22 
main()23 fn main() {
24     let _runtime = RuntimeBuilder::new_multi_thread().build().unwrap();
25 
26     let handle_one = spawn(async {
27         let start = Instant::now();
28         sleep(Duration::new(1, 0)).await;
29         println!("{:?}", Instant::now() - start);
30     });
31     let handle_two = spawn(async {
32         let start = Instant::now();
33         sleep(Duration::new(2, 0)).await;
34         println!("{:?}", Instant::now() - start);
35     });
36     let handle_three = spawn(async {
37         let start = Instant::now();
38         sleep(Duration::new(3, 0)).await;
39         println!("{:?}", Instant::now() - start);
40     });
41     block_on(handle_one).unwrap();
42     block_on(handle_two).unwrap();
43     block_on(handle_three).unwrap();
44 
45     println!("-------------------");
46 
47     block_on(async move {
48         let start = Instant::now();
49         let mut times = 10;
50         while times > 0 {
51             sleep(Duration::new(0, 200_000_000)).await;
52             println!("{:?}", Instant::now() - start);
53             times -= 1;
54         }
55     });
56 }
57