diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3d0bb02022..d5f5fc1841 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1059,6 +1059,9 @@ if(BUILD_TESTING) add_test(NAME xgameruntime COMMAND xgameruntime_test) add_test(NAME xgameruntime_user COMMAND xgameruntime_user_test) add_test(NAME xgameruntime_gamesave COMMAND xgameruntime_gamesave_test) + # Two copies at once: each uses a temp folder of its own process (test/test_temp_dir.h). + add_test(NAME xgameruntime_gamesave_two_copies COMMAND ${CMAKE_COMMAND} -DTEST=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/test/run_two_copies.cmake) add_test(NAME xgameruntime_store COMMAND xgameruntime_store_test) add_test(NAME xgameruntime_offline COMMAND xgameruntime_offline_test) add_test(NAME xgameruntime_offline_hdr COMMAND xgameruntime_offline_test) @@ -1790,6 +1793,10 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_shader_cache_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_shader_cache COMMAND d3d12_shader_cache_test) set_tests_properties(d3d12_shader_cache PROPERTIES SKIP_RETURN_CODE 77) + # Two copies at once: each uses a temp folder of its own process (test/test_temp_dir.h). + add_test(NAME d3d12_shader_cache_two_copies COMMAND ${CMAKE_COMMAND} -DTEST=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/test/run_two_copies.cmake) + set_tests_properties(d3d12_shader_cache_two_copies PROPERTIES SKIP_REGULAR_EXPRESSION "SKIP: both copies skipped") set_tests_properties(d3d12_method_variants PROPERTIES SKIP_RETURN_CODE 77) # ID3D12Object private data (GetPrivateData, SetPrivateData, SetPrivateDataInterface, SetName) compared with # the host runtime's answers on the device and four child objects. @@ -2551,6 +2558,10 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_geometry_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_geometry COMMAND d3d12_geometry_test) set_tests_properties(d3d12_geometry PROPERTIES SKIP_RETURN_CODE 77) + # Two copies at once: each uses a temp folder of its own process (test/test_temp_dir.h). + add_test(NAME d3d12_geometry_two_copies COMMAND ${CMAKE_COMMAND} -DTEST=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/test/run_two_copies.cmake) + set_tests_properties(d3d12_geometry_two_copies PROPERTIES SKIP_REGULAR_EXPRESSION "SKIP: both copies skipped") # D3D-309: D24 depth bias and 24-bit depth in draws, with and without a pixel shader and with a geometry shader # (src/depth24_fragment.h). set(LEONIDA_TEST_VS_DEPTH ${CMAKE_CURRENT_BINARY_DIR}/generated/vs_depth.spv) diff --git a/runtime/test/d3d12_depth24_draw_test.cpp b/runtime/test/d3d12_depth24_draw_test.cpp index 436336e024..eb27c6a620 100644 --- a/runtime/test/d3d12_depth24_draw_test.cpp +++ b/runtime/test/d3d12_depth24_draw_test.cpp @@ -21,6 +21,7 @@ #include "../shims/d3d12_xs_device.h" #include "../src/depth24.h" #include "../src/shader_library.h" +#include "test_temp_dir.h" #include #include @@ -228,7 +229,8 @@ int main() { const auto ps_empty_program = fssm_program({0x50530309, 0xbf810000}); const auto ps_write_program = fssm_program({0x50530310, 0xbf810000}); const auto gs_program = fssm_program({0x47530309, 0xbf810000}); - const auto library = std::filesystem::temp_directory_path() / "leonida_depth24_draw_test"; + const testfiles::ScratchDir scratch("leonida_depth24_draw_test"); // this process's own, removed on return + const auto& library = scratch.path(); std::filesystem::create_directories(library); const auto copy = std::filesystem::copy_options::overwrite_existing; std::filesystem::copy_file(LEONIDA_TEST_VS_DEPTH_SPV, library / "vs_depth.spv", copy); @@ -458,7 +460,6 @@ int main() { std::to_string(r.depth[0]) + ")"); } } - std::filesystem::remove_all(library); std::printf("%d failure(s)\n", g_failures); return g_failures ? 1 : 0; } diff --git a/runtime/test/d3d12_depth_resolve_test.cpp b/runtime/test/d3d12_depth_resolve_test.cpp index 20b1ea5dba..cf1b1b08b6 100644 --- a/runtime/test/d3d12_depth_resolve_test.cpp +++ b/runtime/test/d3d12_depth_resolve_test.cpp @@ -23,6 +23,7 @@ #if defined(LEONIDA_DEPTH_EDGE_VS_SPV) #include "d3d12_test_shaders.h" #include "../src/shader_library.h" +#include "test_temp_dir.h" #include #endif @@ -361,11 +362,13 @@ int main() { std::vector root_blob, edge_container, edge_host_vs; rt::d3d12::VulkanDeviceInfo info{}; #if defined(LEONIDA_DEPTH_EDGE_VS_SPV) + // A folder of this process's own, removed when the test returns: the device reads the library through + // info.shader_dir after the block below, so the folder outlives it. + const testfiles::ScratchDir scratch("leonida_depth_resolve_test"); { const auto program = d3d12test::fssm_program({0x56530848, 0xbf810000}); edge_container = d3d12test::shader_container(program); - const auto library = std::filesystem::temp_directory_path() / "leonida_depth_resolve_test"; - std::filesystem::create_directories(library); + const auto& library = scratch.path(); std::filesystem::copy_file(LEONIDA_DEPTH_EDGE_VS_SPV, library / "vs.spv", std::filesystem::copy_options::overwrite_existing); std::ofstream index(library / "index.json", std::ios::binary); diff --git a/runtime/test/d3d12_f16_halves_test.cpp b/runtime/test/d3d12_f16_halves_test.cpp index c2f194e31b..a89c34464d 100644 --- a/runtime/test/d3d12_f16_halves_test.cpp +++ b/runtime/test/d3d12_f16_halves_test.cpp @@ -17,6 +17,7 @@ #include "../shims/d3d12_xs_device.h" #include "../src/shader_library.h" #include "../src/spirv_f16_halves.h" +#include "test_temp_dir.h" #include #include @@ -188,8 +189,8 @@ int main() { const auto ps1_c = shader_container(ps1_prog); const auto ps2_c = shader_container(ps2_prog); - const auto lib_dir = std::filesystem::temp_directory_path() / "leonida_f16_halves_gpu_test"; - std::filesystem::create_directories(lib_dir); + const testfiles::ScratchDir scratch("leonida_f16_halves_gpu_test"); // this process's own, removed on return + const auto& lib_dir = scratch.path(); write_spv(lib_dir / "vs_16p.spv", make_16param_vs()); write_spv(lib_dir / "ps_packed.spv", make_f16_ps(15, 20, 21)); write_spv(lib_dir / "ps_reloc.spv", make_f16_ps(14, 14, 15)); diff --git a/runtime/test/d3d12_geometry_test.cpp b/runtime/test/d3d12_geometry_test.cpp index 6d8bb59571..a71c9ac908 100644 --- a/runtime/test/d3d12_geometry_test.cpp +++ b/runtime/test/d3d12_geometry_test.cpp @@ -14,6 +14,7 @@ #include "../shims/d3d12_com.h" #include "../shims/d3d12_xs_device.h" #include "../src/shader_library.h" +#include "test_temp_dir.h" #include #include @@ -53,7 +54,8 @@ int main() { const auto ps_program = fssm_program({0x50530001, 0xbf810000}); const auto vs_container = shader_container(vs_program), gs_container = shader_container(gs_program), ps_container = shader_container(ps_program); - const auto library = std::filesystem::temp_directory_path() / "leonida_geometry_test"; + const testfiles::ScratchDir scratch("leonida_geometry_test"); // this process's own, removed on return + const auto& library = scratch.path(); std::filesystem::create_directories(library); std::filesystem::copy_file(LEONIDA_TEST_VS_SPV, library / "vs_draw.spv", std::filesystem::copy_options::overwrite_existing); std::filesystem::copy_file(LEONIDA_TEST_GS_SPV, library / "gs_double.spv", @@ -241,7 +243,6 @@ int main() { fence.reset(); EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); } - std::filesystem::remove_all(library); unsigned mismatches = 0; for (UINT row = 0; row < size; ++row) { diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index df430c11a6..18902583cd 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -19,6 +19,7 @@ #include "../shims/d3d12_com.h" #include "../shims/d3d12_xs_device.h" #include "../src/shader_library.h" +#include "test_temp_dir.h" #include #include @@ -110,7 +111,8 @@ int main() { const auto vs_program = fssm_program({0x56530002, 0xbf810000}); const auto ms_container = shader_container(ms_program), ps_container = shader_container(ps_program); const auto vs_container = shader_container(vs_program); - const auto library = std::filesystem::temp_directory_path() / "leonida_mesh_test"; + const testfiles::ScratchDir scratch("leonida_mesh_test"); // this process's own, removed on return + const auto& library = scratch.path(); std::filesystem::create_directories(library); std::filesystem::copy_file(LEONIDA_TEST_MS_SPV, library / "ms_draw.spv", std::filesystem::copy_options::overwrite_existing); std::filesystem::copy_file(LEONIDA_TEST_PS_SPV, library / "ps_draw.spv", std::filesystem::copy_options::overwrite_existing); @@ -184,7 +186,6 @@ int main() { EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); } if (!d.mesh_shader) { - std::filesystem::remove_all(library); std::printf("no VK_EXT_mesh_shader on this device: only the refusal without it was checked\n"); return 77; } @@ -492,7 +493,6 @@ int main() { fence.reset(); EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); } - std::filesystem::remove_all(library); unsigned mismatches = 0; for (UINT row = 0; row < size; ++row) { diff --git a/runtime/test/d3d12_shader_cache_test.cpp b/runtime/test/d3d12_shader_cache_test.cpp index bc4b2704da..4d95efb503 100644 --- a/runtime/test/d3d12_shader_cache_test.cpp +++ b/runtime/test/d3d12_shader_cache_test.cpp @@ -6,6 +6,7 @@ #include "vk_test_device.h" #include "d3d12_copy_compare.h" #include "../shims/d3d12_com.h" +#include "test_temp_dir.h" #include #include @@ -286,7 +287,7 @@ int main() { // D3D12_SHADER_CACHE_MODE_DISK persistence across session + device teardown and cleanup on SetDeleteOnDestroy. { const auto prev_cwd = std::filesystem::current_path(); - const auto temp_dir = std::filesystem::temp_directory_path() / L"leonida_shader_cache_disk_test"; + const auto temp_dir = testfiles::process_temp_dir("leonida_shader_cache_disk_test"); // this process's own std::error_code ec; std::filesystem::remove_all(temp_dir, ec); std::filesystem::create_directories(temp_dir); diff --git a/runtime/test/d3d12_state_sweep_test.cpp b/runtime/test/d3d12_state_sweep_test.cpp index 0ce033e99f..af4d883cc0 100644 --- a/runtime/test/d3d12_state_sweep_test.cpp +++ b/runtime/test/d3d12_state_sweep_test.cpp @@ -16,6 +16,7 @@ #include "d3d12_test_shaders.h" #include "../shims/d3d12_com.h" #include "../src/shader_library.h" +#include "test_temp_dir.h" #include #include @@ -444,8 +445,8 @@ int main() { const auto vs_program = d3d12test::fssm_program({0x56530501, 0xbf810000}); const auto ps_program = d3d12test::fssm_program({0x50530501, 0xbf810000}); - const auto library = std::filesystem::temp_directory_path() / "leonida_state_sweep_test"; - std::filesystem::create_directories(library); + const testfiles::ScratchDir scratch("leonida_state_sweep_test"); // this process's own, removed on return + const auto& library = scratch.path(); const auto overwrite = std::filesystem::copy_options::overwrite_existing; std::filesystem::copy_file(LEONIDA_SWEEP_VS_SPV, library / "vs.spv", overwrite); std::filesystem::copy_file(LEONIDA_SWEEP_PS_SPV, library / "ps.spv", overwrite); diff --git a/runtime/test/d3d12_stores_atomics_test.cpp b/runtime/test/d3d12_stores_atomics_test.cpp index afc00cce2f..40d2961f6f 100644 --- a/runtime/test/d3d12_stores_atomics_test.cpp +++ b/runtime/test/d3d12_stores_atomics_test.cpp @@ -17,6 +17,7 @@ #include "../src/shader_library.h" #include "../src/spirv_stores.h" #include "../src/spirv_words.h" +#include "test_temp_dir.h" #include #include @@ -121,8 +122,8 @@ int main() { const auto atomic_program = fssm_program({0x50530a70, 0xbf810000}); const auto vs_container = shader_container(vs_program), ps_container = shader_container(ps_program); const auto atomic_container = shader_container(atomic_program); - const auto library = std::filesystem::temp_directory_path() / "leonida_stores_atomics_test"; - std::filesystem::create_directories(library); + const testfiles::ScratchDir scratch("leonida_stores_atomics_test"); // this process's own, removed on return + const auto& library = scratch.path(); const struct { const char* compiled; const char* name; } files[] = { {LEONIDA_TEST_VS_FULLSCREEN_SPV, "vs_fullscreen.spv"}, {LEONIDA_TEST_PS_SPV, "ps_draw.spv"}, diff --git a/runtime/test/run_two_copies.cmake b/runtime/test/run_two_copies.cmake new file mode 100644 index 0000000000..82d50fd48d --- /dev/null +++ b/runtime/test/run_two_copies.cmake @@ -0,0 +1,29 @@ +# Runs two copies of one test program at the same time and fails unless both exit 0 (77 from both is a skip). +# +# cmake -DTEST= -P run_two_copies.cmake +# +# A test that wrote to one fixed folder under the temp directory raced with a second copy of itself (ctest -j from two +# checkouts, or two suites sharing one TMPDIR or Wine prefix): one copy's remove_all deleted the files the other was +# still using. The commands of one execute_process call run concurrently, so this starts both copies together. The +# first copy's output is piped into the second copy's standard input, which the tests do not read. +if(NOT TEST) + message(FATAL_ERROR "run_two_copies.cmake: pass -DTEST=") +endif() + +execute_process( + COMMAND "${TEST}" + COMMAND "${TEST}" + RESULTS_VARIABLE results + OUTPUT_VARIABLE output + ERROR_VARIABLE errors) + +message("${output}") +message("${errors}") +if(results STREQUAL "77;77") + message("SKIP: both copies skipped") + return() +endif() +if(NOT results STREQUAL "0;0") + message(FATAL_ERROR "two copies at once: exit codes ${results} (both must be 0)") +endif() +message("two copies at once: both passed") diff --git a/runtime/test/xgameruntime_gamesave_test.cpp b/runtime/test/xgameruntime_gamesave_test.cpp index d2d7f45eb3..767d3af35d 100644 --- a/runtime/test/xgameruntime_gamesave_test.cpp +++ b/runtime/test/xgameruntime_gamesave_test.cpp @@ -18,6 +18,7 @@ #include #include "shims.h" +#include "test_temp_dir.h" namespace rt::shims { long xgameruntime_gamesave_open_handles(); @@ -122,7 +123,7 @@ bool CALLBACK on_blob(const BlobInfo* info, void* context) { int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); // a crash still shows the checks before it std::printf("xgameruntime_gamesave_test: XGameSaveImpl through QueryApiImpl\n"); - const std::filesystem::path root = std::filesystem::temp_directory_path() / L"leonida_gamesave_test"; + const std::filesystem::path root = testfiles::process_temp_dir("leonida_gamesave_test"); // this process's own std::error_code ec; std::filesystem::remove_all(root, ec); SetEnvironmentVariableW(L"LEONIDA_SAVE_DIR", root.c_str());