From d86056fd6e627d98f9a6906566e6bf4462368361 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 9 Aug 2022 11:52:36 +0200 Subject: [PATCH] Add ref count check to DeviceLost_RaiseEvent test --- .../DeviceLostTests.cs | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index 94fadf55..5a59f3f7 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -5,6 +5,8 @@ using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.DeviceLost.Helpers; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; namespace ComputeSharp.Tests.DeviceLost; @@ -16,29 +18,37 @@ public class DeviceLostTests [AllDevices] public async Task DeviceLost_RaiseEvent(Device device) { - using GraphicsDevice graphicsDevice = device.Get(); + using ComPtr d3D12Device = default; - List<(object? Sender, DeviceLostEventArgs Args)> args = new(); + using (GraphicsDevice graphicsDevice = device.Get()) + { + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); - // Register the device lost callback - graphicsDevice.DeviceLost += (s, e) => args.Add((s, e)); + List<(object? Sender, DeviceLostEventArgs Args)> args = new(); - await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); + // Register the device lost callback + graphicsDevice.DeviceLost += (s, e) => args.Add((s, e)); - Assert.AreEqual(1, args.Count); - Assert.IsNotNull(args[0].Sender); - Assert.IsNotNull(args[0].Args); - Assert.AreSame(args[0].Sender, graphicsDevice); - Assert.AreEqual(args[0].Args.Reason, DeviceLostReason.DeviceRemoved); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); - // Trying to remove the device again does nothing - GraphicsDeviceHelper.RemoveDevice(graphicsDevice); - GraphicsDeviceHelper.RemoveDevice(graphicsDevice); - GraphicsDeviceHelper.RemoveDevice(graphicsDevice); - GraphicsDeviceHelper.RemoveDevice(graphicsDevice); + Assert.AreEqual(1, args.Count); + Assert.IsNotNull(args[0].Sender); + Assert.IsNotNull(args[0].Args); + Assert.AreSame(args[0].Sender, graphicsDevice); + Assert.AreEqual(args[0].Args.Reason, DeviceLostReason.DeviceRemoved); - // The event can only be raised once - Assert.AreEqual(1, args.Count); + // 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); + } + + // Ensure the device has been disposed correctly + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); } [CombinatorialTestMethod]