diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index e3077d35..927cd1c6 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.DeviceLost.Helpers; @@ -17,24 +18,18 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - TaskCompletionSource<(object? Sender, DeviceLostReason Reason)> tcs = new(); + List<(object? Sender, DeviceLostReason Reason)> args = new(); // Register the device lost callback - graphicsDevice.DeviceLost += (s, e) => tcs.SetResult((s, e)); + graphicsDevice.DeviceLost += (s, e) => args.Add((s, e)); - GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); - // Wait up to a second for the event to be raised (it's raised asynchronously on a thread pool thread) - await Task.WhenAny(tcs.Task, Task.Delay(1000)); - - // Ensure the event has been raised, and get the results - Assert.IsTrue(tcs.Task.IsCompleted); - - (object? sender, DeviceLostReason reason) = await tcs.Task; - - Assert.IsNotNull(sender); - Assert.AreSame(sender, graphicsDevice); - Assert.AreEqual(reason, DeviceLostReason.DeviceRemoved); + Assert.AreEqual(1, args.Count); + Assert.IsNotNull(args[0].Sender); + Assert.IsNotNull(args[0].Reason); + Assert.AreSame(args[0].Sender, graphicsDevice); + Assert.AreEqual(args[0].Reason, DeviceLostReason.DeviceRemoved); } [CombinatorialTestMethod] diff --git a/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs b/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs index c1071b4c..bbfe9ddd 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using ComputeSharp.Tests.DeviceLost.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; @@ -11,7 +12,7 @@ namespace ComputeSharp.Tests.DeviceLost; public class GetDefaultDeviceTests { [TestMethod] - public unsafe void GetDefault_VerifyContract() + public async Task GetDefault_VerifyContract() { GraphicsDevice device1 = GraphicsDevice.GetDefault(); @@ -28,7 +29,7 @@ public class GetDefaultDeviceTests // GraphicsDevice.GetDefault() again will always return the same cached instance. Assert.AreSame(device1, device2); - GraphicsDeviceHelper.RemoveDevice(device1); + await GraphicsDeviceHelper.RemoveDeviceAsync(device1); // The device is lost but not disposed, so the same instance should be returned yet again GraphicsDevice device3 = GraphicsDevice.GetDefault(); @@ -55,7 +56,7 @@ public class GetDefaultDeviceTests // The LUID should match though, as the same adapter should be returned Assert.AreEqual(device1.Luid, device4.Luid); - GraphicsDeviceHelper.RemoveDevice(device4); + await GraphicsDeviceHelper.RemoveDeviceAsync(device4); using (ComPtr d3D12Device = default) { @@ -69,10 +70,13 @@ public class GetDefaultDeviceTests // Sanity check that this is in fact the only reference keeping the device alive Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); - HRESULT removalReason = d3D12Device.Get()->GetDeviceRemovedReason(); + unsafe + { + HRESULT removalReason = d3D12Device.Get()->GetDeviceRemovedReason(); - // Sanity check that the device is in fact removed - Assert.AreEqual(DXGI.DXGI_ERROR_DEVICE_REMOVED, (int)removalReason); + // Sanity check that the device is in fact removed + Assert.AreEqual(DXGI.DXGI_ERROR_DEVICE_REMOVED, (int)removalReason); + } // Calling GraphicsDevice.GetDefault() will now throw, because the native device has not been disposed properly Assert.ThrowsException(() => GraphicsDevice.GetDefault()); diff --git a/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs index 017d1118..ecbf11ef 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs @@ -45,50 +45,48 @@ 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. /// /// The target instance. + /// + /// Removing a device will cause the device removed callback to be invoked on a separate thread, to get a + /// reference tracking lease for the device and then raise the event. If this is not awaited, it means + /// that that callback might race against a thread just calling Dispose(), so that that thread would not + /// actually cause the object to release resources until the device removed callback has also released its + /// own lease. To avoid issues related to this, the event should always be awaited after removing a device. + /// public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice) { TaskCompletionSource tcs = new(); 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 Task.WhenAny(tcs.Task, Task.Delay(1000)); - - if (!tcs.Task.IsCompleted) - { - Assert.Fail(); - } + await tcs.Task; } }