wgpu_test/
run.rs

1use std::panic::AssertUnwindSafe;
2
3use futures_lite::FutureExt;
4use wgpu::{Adapter, Device, Instance, Queue};
5
6use crate::{
7    expectations::{expectations_match_failures, ExpectationMatchResult, FailureResult},
8    init::{init_logger, initialize_adapter, initialize_device},
9    isolation,
10    params::TestInfo,
11    report::AdapterReport,
12    GpuTestConfiguration,
13};
14
15#[derive(Hash)]
16/// Parameters and resources handed to the test function.
17pub struct TestingContext {
18    pub instance: Instance,
19    pub adapter: Adapter,
20    pub adapter_info: wgpu::AdapterInfo,
21    pub adapter_downlevel_capabilities: wgpu::DownlevelCapabilities,
22    pub device: Device,
23    pub device_features: wgpu::Features,
24    pub device_limits: wgpu::Limits,
25    pub queue: Queue,
26}
27
28/// Execute the given test configuration with the given adapter report.
29///
30/// If test_info is specified, will use the information whether to skip the test.
31/// If it is not, we'll create the test info from the adapter itself.
32pub async fn execute_test(
33    adapter_report: Option<&AdapterReport>,
34    config: GpuTestConfiguration,
35    test_info: Option<TestInfo>,
36) {
37    // If we get information externally, skip based on that information before we do anything.
38    if let Some(TestInfo { skip: true, .. }) = test_info {
39        return;
40    }
41
42    init_logger();
43
44    let _test_guard = isolation::OneTestPerProcessGuard::new();
45
46    let (instance, adapter, _surface_guard) =
47        initialize_adapter(adapter_report, &config.params).await;
48
49    let adapter_info = adapter.get_info();
50    let adapter_downlevel_capabilities = adapter.get_downlevel_capabilities();
51
52    let test_info = test_info.unwrap_or_else(|| {
53        let adapter_report = AdapterReport::from_adapter(&adapter);
54        TestInfo::from_configuration(&config, &adapter_report)
55    });
56
57    // We are now guaranteed to have information about this test, so skip if we need to.
58    if test_info.skip {
59        log::info!("TEST RESULT: SKIPPED");
60        return;
61    }
62
63    // Print the name of the test.
64    log::info!("TEST: {}", config.name);
65
66    let (device, queue) = pollster::block_on(initialize_device(
67        &adapter,
68        config.params.required_features,
69        config.params.required_limits.clone(),
70    ));
71
72    let context = TestingContext {
73        instance,
74        adapter,
75        adapter_info,
76        adapter_downlevel_capabilities,
77        device,
78        device_features: config.params.required_features,
79        device_limits: config.params.required_limits.clone(),
80        queue,
81    };
82
83    let mut failures = Vec::new();
84
85    // Run the test, and catch panics (possibly due to failed assertions).
86    let panic_res = AssertUnwindSafe((config.test.as_ref().unwrap())(context))
87        .catch_unwind()
88        .await;
89
90    if let Err(panic) = panic_res {
91        let message = panic
92            .downcast_ref::<&str>()
93            .copied()
94            .or_else(|| panic.downcast_ref::<String>().map(String::as_str));
95
96        let result = FailureResult::panic();
97
98        let result = if let Some(panic_str) = message {
99            result.with_message(panic_str)
100        } else {
101            result
102        };
103
104        failures.push(result)
105    }
106
107    // Check whether any validation errors were reported during the test run.
108    cfg_if::cfg_if!(
109        if #[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))] {
110            failures.extend(wgpu::hal::VALIDATION_CANARY.get_and_reset().into_iter().map(|msg| FailureResult::validation_error().with_message(msg)));
111        } else if #[cfg(all(target_arch = "wasm32", feature = "webgl"))] {
112            if _surface_guard.unwrap().check_for_unreported_errors() {
113                failures.push(FailureResult::validation_error());
114            }
115        } else {
116        }
117    );
118
119    // The call to matches_failure will log.
120    if expectations_match_failures(&test_info.failures, failures) == ExpectationMatchResult::Panic {
121        panic!(
122            "{}: test {:?} did not behave as expected",
123            config.location, config.name
124        );
125    }
126    // Print the name of the test.
127    log::info!("TEST FINISHED: {}", config.name);
128}