wgpu_test/
isolation.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2
3/// True if a test is in progress somewhere in the process, false otherwise.
4static TEST_ACTIVE_IN_PROCESS: AtomicBool = AtomicBool::new(false);
5
6const OTHER_TEST_IN_PROGRESS_ERROR: &str = "TEST ISOLATION ERROR:
7
8wgpu's test harness requires that no more than one test is running per process.
9
10The best way to facilitate this is by using cargo-nextest which runs each test in its own process
11and has a very good testing UI:
12
13cargo install cargo-nextest
14cargo nextest run
15
16Alternatively, you can run tests in single threaded mode (much slower).
17
18cargo test -- --test-threads=1
19
20Calling std::process::abort()...
21";
22
23/// When this guard is active, enforces that there is only a single test running in the process
24/// at any one time. If there are multiple processes, creating the guard hard terminates the process.
25pub struct OneTestPerProcessGuard(());
26
27impl OneTestPerProcessGuard {
28    pub fn new() -> Self {
29        let other_tests_in_flight = TEST_ACTIVE_IN_PROCESS.swap(true, Ordering::SeqCst);
30
31        // We never abort if we're on wasm. Wasm tests are inherently single threaded, and panics cannot
32        // unwind the stack and trigger all the guards, so we don't actually need to check.
33        if other_tests_in_flight && !cfg!(target_arch = "wasm32") {
34            log::error!("{OTHER_TEST_IN_PROGRESS_ERROR}");
35            // Hard exit to call attention to the error
36            std::process::abort();
37        }
38        OneTestPerProcessGuard(())
39    }
40}
41
42impl Drop for OneTestPerProcessGuard {
43    fn drop(&mut self) {
44        TEST_ACTIVE_IN_PROCESS.store(false, Ordering::SeqCst);
45    }
46}