| 1 |
use crate::sync::AtomicWaker; |
| 2 |
use tokio_test::task; |
| 3 |
|
| 4 |
use std::task::Waker; |
| 5 |
|
| 6 |
#[allow(unused)] |
| 7 |
trait AssertSend: Send {} |
| 8 |
|
| 9 |
#[allow(unused)] |
| 10 |
trait AssertSync: Sync {} |
| 11 |
|
| 12 |
impl AssertSend for AtomicWaker {} |
| 13 |
impl AssertSync for AtomicWaker {} |
| 14 |
|
| 15 |
impl AssertSend for Waker {} |
| 16 |
impl AssertSync for Waker {} |
| 17 |
|
| 18 |
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] |
| 19 |
use wasm_bindgen_test::wasm_bindgen_test as test; |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn basic_usage() { |
| 23 |
let mut waker = task::spawn(AtomicWaker::new()); |
| 24 |
|
| 25 |
waker.enter(|cx, waker| waker.register_by_ref(cx.waker())); |
| 26 |
waker.wake(); |
| 27 |
|
| 28 |
assert!(waker.is_woken()); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn wake_without_register() { |
| 33 |
let mut waker = task::spawn(AtomicWaker::new()); |
| 34 |
waker.wake(); |
| 35 |
|
| 36 |
// Registering should not result in a notification |
| 37 |
waker.enter(|cx, waker| waker.register_by_ref(cx.waker())); |
| 38 |
|
| 39 |
assert!(!waker.is_woken()); |
| 40 |
} |
| 41 |
|
| 42 |
#[cfg(panic = "unwind")] |
| 43 |
#[test] |
| 44 |
#[cfg(not(target_family = "wasm"))] // wasm currently doesn't support unwinding |
| 45 |
fn atomic_waker_panic_safe() { |
| 46 |
use std::panic; |
| 47 |
use std::ptr; |
| 48 |
use std::task::{RawWaker, RawWakerVTable, Waker}; |
| 49 |
|
| 50 |
static PANICKING_VTABLE: RawWakerVTable = RawWakerVTable::new( |
| 51 |
|_| panic!("clone"), |
| 52 |
|_| unimplemented!("wake"), |
| 53 |
|_| unimplemented!("wake_by_ref"), |
| 54 |
|_| (), |
| 55 |
); |
| 56 |
|
| 57 |
static NONPANICKING_VTABLE: RawWakerVTable = RawWakerVTable::new( |
| 58 |
|_| RawWaker::new(ptr::null(), &NONPANICKING_VTABLE), |
| 59 |
|_| unimplemented!("wake"), |
| 60 |
|_| unimplemented!("wake_by_ref"), |
| 61 |
|_| (), |
| 62 |
); |
| 63 |
|
| 64 |
let panicking = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &PANICKING_VTABLE)) }; |
| 65 |
let nonpanicking = unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &NONPANICKING_VTABLE)) }; |
| 66 |
|
| 67 |
let atomic_waker = AtomicWaker::new(); |
| 68 |
|
| 69 |
let panicking = panic::AssertUnwindSafe(&panicking); |
| 70 |
|
| 71 |
let result = panic::catch_unwind(|| { |
| 72 |
let panic::AssertUnwindSafe(panicking) = panicking; |
| 73 |
atomic_waker.register_by_ref(panicking); |
| 74 |
}); |
| 75 |
|
| 76 |
assert!(result.is_err()); |
| 77 |
assert!(atomic_waker.take_waker().is_none()); |
| 78 |
|
| 79 |
atomic_waker.register_by_ref(&nonpanicking); |
| 80 |
assert!(atomic_waker.take_waker().is_some()); |
| 81 |
} |