diff --git a/console-subscriber/examples/app.rs b/console-subscriber/examples/app.rs index f1247ed62..1bdf02227 100644 --- a/console-subscriber/examples/app.rs +++ b/console-subscriber/examples/app.rs @@ -11,6 +11,7 @@ OPTIONS: blocks Includes a (misbehaving) blocking task burn Includes a (misbehaving) task that spins CPU with self-wakes coma Includes a (misbehaving) task that forgets to register a waker + noyield Includes a (misbehaving) task that spawns tasks that never yield "#; #[tokio::main] @@ -38,6 +39,12 @@ async fn main() -> Result<(), Box> { .spawn(burn(1, 10)) .unwrap(); } + "noyield" => { + tokio::task::Builder::new() + .name("noyield") + .spawn(no_yield(20)) + .unwrap(); + } "help" | "-h" => { eprintln!("{}", HELP); return Ok(()); @@ -114,3 +121,17 @@ async fn burn(min: u64, max: u64) { } } } + +#[tracing::instrument] +async fn no_yield(seconds: u64) { + loop { + let handle = tokio::task::Builder::new() + .name("greedy") + .spawn(async move { + std::thread::sleep(Duration::from_secs(seconds)); + }) + .expect("Couldn't spawn greedy task"); + + _ = handle.await; + } +} diff --git a/console-subscriber/examples/busy_loop.rs b/console-subscriber/examples/busy_loop.rs deleted file mode 100644 index a72b037f5..000000000 --- a/console-subscriber/examples/busy_loop.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::sync::atomic::Ordering; -use std::sync::{atomic::AtomicBool, Arc}; -use std::time::Duration; -use tokio::task; -use tokio::time::sleep; - -#[tokio::main] -async fn main() -> Result<(), Box> { - console_subscriber::init(); - let stop = Arc::new(AtomicBool::new(false)); - - let t = task::Builder::default() - .name("busy-loop") - .spawn({ - let stop = Arc::clone(&stop); - async move { - loop { - if stop.load(Ordering::Acquire) { - break; - } - } - } - }) - .unwrap(); - - sleep(Duration::from_secs(300)).await; - stop.store(true, Ordering::Release); - t.await?; - - Ok(()) -}