File: task_local.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (43 lines) | stat: -rw-r--r-- 1,072 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::sync::atomic::{AtomicBool, Ordering};

use async_std::task;
use async_std::task_local;

#[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);

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
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.
    let handle = spawn(async {
        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);
    });
}