async-std/tests/task_local.rs

44 lines
1 KiB
Rust
Raw Normal View History

2019-08-08 12:44:48 +00:00
use std::sync::atomic::{AtomicBool, Ordering};
2019-08-14 13:57:51 +00:00
use async_std::task;
use async_std::task_local;
2019-08-08 12:44:48 +00:00
2020-04-26 16:00:00 +00:00
#[cfg(not(target_os = "unknown"))]
use async_std::task::spawn;
#[cfg(target_os = "unknown")]
use async_std::task::spawn_local as spawn;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
2019-08-08 12:44:48 +00:00
#[test]
2020-04-26 16:00:00 +00:00
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
2019-08-08 12:44:48 +00:00
fn drop_local() {
static DROP_LOCAL: AtomicBool = AtomicBool::new(false);
struct Local;
impl Drop for Local {
fn drop(&mut self) {
DROP_LOCAL.store(true, Ordering::SeqCst);
}
}
task_local! {
static LOCAL: Local = Local;
}
// Spawn a task that just touches its task-local.
2020-04-26 16:00:00 +00:00
let handle = spawn(async {
2019-08-08 12:44:48 +00:00
LOCAL.with(|_| ());
});
let task = handle.task().clone();
// Wait for the task to finish and make sure its task-local has been dropped.
task::block_on(async {
handle.await;
assert!(DROP_LOCAL.load(Ordering::SeqCst));
drop(task);
});
}