Skip to content

Commit 5568bf6

Browse files
authored
example: add some misbehaving options to example app (#54)
1 parent 5142a60 commit 5568bf6

File tree

1 file changed

+62
-1
lines changed
  • console-subscriber/examples

1 file changed

+62
-1
lines changed

Diff for: console-subscriber/examples/app.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
use std::time::Duration;
22

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+
316
#[tokio::main]
417
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
518
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+
}
643

744
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+
947
let result = tokio::try_join! {
1048
task1,
1149
task2,
@@ -29,3 +67,26 @@ async fn spawn_tasks(min: u64, max: u64) {
2967
async fn wait(seconds: u64) {
3068
tokio::time::sleep(Duration::from_secs(seconds)).await;
3169
}
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

Comments
 (0)