Fix race conditions in device removed unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:14 +02:00
1 親 f9c9b5a24b
コミット 22fccc032c
3ファイル変更
+50 -53

検索条件に一致するファイルはありません

+9 -14
ファイルの表示
@@ -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]
+10 -6
ファイルの表示
@@ -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<ID3D12Device> 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<InvalidOperationException>(() => GraphicsDevice.GetDefault());
+31 -33
ファイルの表示
@@ -45,50 +45,48 @@ internal static class GraphicsDeviceHelper
return d3D12Device.Get()->Release();
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
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();
}
}
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance and waits for it to be reported.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
/// <remarks>
/// 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.
/// </remarks>
public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
{
TaskCompletionSource<object?> 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;
}
}