|
| 1 | +#![warn(rust_2018_idioms)] |
| 2 | +#![cfg(feature = "full")] |
| 3 | +#![cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 4 | + |
| 5 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 6 | +use std::sync::Arc; |
| 7 | +use tokio::runtime::Builder; |
| 8 | +use tokio::sync::Notify; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn before_park_wakes_block_on_task() { |
| 12 | + let notify = Arc::new(Notify::new()); |
| 13 | + let notify2 = notify.clone(); |
| 14 | + let woken = Arc::new(AtomicBool::new(false)); |
| 15 | + let woken2 = woken.clone(); |
| 16 | + |
| 17 | + let rt = Builder::new_current_thread() |
| 18 | + .enable_all() |
| 19 | + .on_thread_park(move || { |
| 20 | + // Only wake once to avoid busy loop if something goes wrong, |
| 21 | + // though in this test we expect it to unpark immediately. |
| 22 | + if !woken2.swap(true, Ordering::SeqCst) { |
| 23 | + notify2.notify_one(); |
| 24 | + } |
| 25 | + }) |
| 26 | + .build() |
| 27 | + .unwrap(); |
| 28 | + |
| 29 | + rt.block_on(async { |
| 30 | + // This will block until `notify` is notified. |
| 31 | + // `before_park` should run when the runtime is about to park. |
| 32 | + // It will notify `notify`, which should wake this task. |
| 33 | + // The runtime should then see the task is woken and NOT park. |
| 34 | + notify.notified().await; |
| 35 | + }); |
| 36 | + |
| 37 | + assert!(woken.load(Ordering::SeqCst)); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn before_park_spawns_task() { |
| 42 | + let notify = Arc::new(Notify::new()); |
| 43 | + let notify2 = notify.clone(); |
| 44 | + let woken = Arc::new(AtomicBool::new(false)); |
| 45 | + let woken2 = woken.clone(); |
| 46 | + |
| 47 | + let rt = Builder::new_current_thread() |
| 48 | + .enable_all() |
| 49 | + .on_thread_park(move || { |
| 50 | + if !woken2.swap(true, Ordering::SeqCst) { |
| 51 | + let notify = notify2.clone(); |
| 52 | + tokio::spawn(async move { |
| 53 | + notify.notify_one(); |
| 54 | + }); |
| 55 | + } |
| 56 | + }) |
| 57 | + .build() |
| 58 | + .unwrap(); |
| 59 | + |
| 60 | + rt.block_on(async { |
| 61 | + // This will block until `notify` is notified. |
| 62 | + // `before_park` should run when the runtime is about to park. |
| 63 | + // It will spawn a task that notifies `notify`. |
| 64 | + // The runtime should see the new task and NOT park. |
| 65 | + // If it parks, it will deadlock. |
| 66 | + notify.notified().await; |
| 67 | + }); |
| 68 | + |
| 69 | + assert!(woken.load(Ordering::SeqCst)); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn wake_from_other_thread_block_on() { |
| 74 | + let rt = Builder::new_current_thread().enable_all().build().unwrap(); |
| 75 | + let handle = rt.handle().clone(); |
| 76 | + let notify = Arc::new(Notify::new()); |
| 77 | + let notify2 = notify.clone(); |
| 78 | + |
| 79 | + let th = std::thread::spawn(move || { |
| 80 | + // Give the main thread time to park |
| 81 | + std::thread::sleep(std::time::Duration::from_millis(5)); |
| 82 | + handle.block_on(async move { |
| 83 | + notify2.notify_one(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + rt.block_on(async { |
| 88 | + notify.notified().await; |
| 89 | + }); |
| 90 | + |
| 91 | + th.join().unwrap(); |
| 92 | +} |
0 commit comments