From f1306a462169f73daa9c9d54bf32d0c4012c9767 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jul 2022 15:20:55 +0200 Subject: [PATCH] Add unit test for multiple device removals --- .../DeviceLostTests.cs | 9 ++++ .../Helpers/GraphicsDeviceHelper.cs | 50 ++++++++++--------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index 927cd1c6..17139d71 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -30,6 +30,15 @@ public class DeviceLostTests Assert.IsNotNull(args[0].Reason); Assert.AreSame(args[0].Sender, graphicsDevice); Assert.AreEqual(args[0].Reason, DeviceLostReason.DeviceRemoved); + + // Trying to remove the device again does nothing + GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + + // The event can only be raised once + Assert.AreEqual(1, args.Count); } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs index ecbf11ef..39a13cd0 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs @@ -45,6 +45,33 @@ internal static class GraphicsDeviceHelper return d3D12Device.Get()->Release(); } + /// + /// Removes the underlying device for a given instance. + /// + /// The target instance. + public static unsafe void RemoveDevice(GraphicsDevice graphicsDevice) + { + ID3D12Device5* d3D12Device = default; + Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID; + + if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0) + { + Assert.Inconclusive(); + } + + try + { + d3D12Device->RemoveDevice(); + } + finally + { + if (d3D12Device is not null) + { + d3D12Device->Release(); + } + } + } + /// /// Removes the underlying device for a given instance and waits for it to be reported. /// @@ -62,29 +89,6 @@ internal static class GraphicsDeviceHelper graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null); - static unsafe void RemoveDevice(GraphicsDevice graphicsDevice) - { - ID3D12Device5* d3D12Device = default; - Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID; - - if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0) - { - Assert.Inconclusive(); - } - - try - { - d3D12Device->RemoveDevice(); - } - finally - { - if (d3D12Device is not null) - { - d3D12Device->Release(); - } - } - } - RemoveDevice(graphicsDevice); await tcs.Task;