Debian Codegraph @

third_party/rust/tokio/tests/rt_metrics.rs

raw
1 #![allow(unknown_lints, unexpected_cfgs)]
2 #![warn(rust_2018_idioms)]
3 #![cfg(all(feature = "full", not(target_os = "wasi"), target_has_atomic = "64"))]
4
5 use tokio::runtime::Runtime;
6
7 #[test]
8 fn num_workers() {
9 let rt = current_thread();
10 assert_eq!(1, rt.metrics().num_workers());
11
12 let rt = threaded();
13 assert_eq!(2, rt.metrics().num_workers());
14 }
15
16 #[test]
17 fn num_alive_tasks() {
18 let rt = current_thread();
19 let metrics = rt.metrics();
20 assert_eq!(0, metrics.num_alive_tasks());
21 rt.block_on(rt.spawn(async move {
22 assert_eq!(1, metrics.num_alive_tasks());
23 }))
24 .unwrap();
25
26 assert_eq!(0, rt.metrics().num_alive_tasks());
27
28 let rt = threaded();
29 let metrics = rt.metrics();
30 assert_eq!(0, metrics.num_alive_tasks());
31 rt.block_on(rt.spawn(async move {
32 assert_eq!(1, metrics.num_alive_tasks());
33 }))
34 .unwrap();
35
36 // try for 10 seconds to see if this eventually succeeds.
37 // wake_join() is called before the task is released, so in multithreaded
38 // code, this means we sometimes exit the block_on before the counter decrements.
39 for _ in 0..100 {
40 if rt.metrics().num_alive_tasks() == 0 {
41 break;
42 }
43 std::thread::sleep(std::time::Duration::from_millis(100));
44 }
45 assert_eq!(0, rt.metrics().num_alive_tasks());
46 }
47
48 fn current_thread() -> Runtime {
49 tokio::runtime::Builder::new_current_thread()
50 .enable_all()
51 .build()
52 .unwrap()
53 }
54
55 fn threaded() -> Runtime {
56 tokio::runtime::Builder::new_multi_thread()
57 .worker_threads(2)
58 .enable_all()
59 .build()
60 .unwrap()
61 }