1
1
use std:: time:: Duration ;
2
2
3
+ static HELP : & ' static str = r#"
4
+ Example console-instrumented app
5
+
6
+ USAGE:
7
+ app [OPTIONS]
8
+
9
+ OPTIONS:
10
+ -h, help prints this message
11
+ blocks Includes a (misbehaving) blocking task
12
+ burn Includes a (misbehaving) task that spins CPU with self-wakes
13
+ coma Includes a (misbehaving) task that forgets to register a waker
14
+ "# ;
15
+
3
16
#[ tokio:: main]
4
17
async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
5
18
console_subscriber:: init ( ) ;
19
+ // spawn optional extras from CLI args
20
+ // skip first which is command name
21
+ for opt in std:: env:: args ( ) . skip ( 1 ) {
22
+ match & * opt {
23
+ "blocks" => {
24
+ tokio:: spawn ( double_sleepy ( 1 , 10 ) ) ;
25
+ }
26
+ "coma" => {
27
+ tokio:: spawn ( std:: future:: pending :: < ( ) > ( ) ) ;
28
+ }
29
+ "burn" => {
30
+ tokio:: spawn ( burn ( 1 , 10 ) ) ;
31
+ }
32
+ "help" | "-h" => {
33
+ eprintln ! ( "{}" , HELP ) ;
34
+ return Ok ( ( ) ) ;
35
+ }
36
+ wat => {
37
+ return Err (
38
+ format ! ( "unknown option: {:?}, run with '-h' to see options" , wat) . into ( ) ,
39
+ )
40
+ }
41
+ }
42
+ }
6
43
7
44
let task1 = tokio:: spawn ( spawn_tasks ( 1 , 10 ) ) ;
8
- let task2 = tokio:: spawn ( spawn_tasks ( 10 , 100 ) ) ;
45
+ let task2 = tokio:: spawn ( spawn_tasks ( 10 , 30 ) ) ;
46
+
9
47
let result = tokio:: try_join! {
10
48
task1,
11
49
task2,
@@ -29,3 +67,26 @@ async fn spawn_tasks(min: u64, max: u64) {
29
67
async fn wait ( seconds : u64 ) {
30
68
tokio:: time:: sleep ( Duration :: from_secs ( seconds) ) . await ;
31
69
}
70
+
71
+ #[ tracing:: instrument]
72
+ async fn double_sleepy ( min : u64 , max : u64 ) {
73
+ loop {
74
+ for i in min..max {
75
+ // woops!
76
+ std:: thread:: sleep ( Duration :: from_secs ( i) ) ;
77
+ tokio:: time:: sleep ( Duration :: from_secs ( max - i) ) . await ;
78
+ }
79
+ }
80
+ }
81
+
82
+ #[ tracing:: instrument]
83
+ async fn burn ( min : u64 , max : u64 ) {
84
+ loop {
85
+ for i in min..max {
86
+ for _ in 0 ..i {
87
+ tokio:: task:: yield_now ( ) . await ;
88
+ }
89
+ tokio:: time:: sleep ( Duration :: from_secs ( i - min) ) . await ;
90
+ }
91
+ }
92
+ }
0 commit comments