diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index a4ee66e4..73c4c8cb 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -12,13 +12,14 @@ namespace ComputeSharp.Tests.DeviceLost; [TestClass] [TestCategory("DeviceLost")] +[DoNotParallelize] public partial class DeviceLostTests { [CombinatorialTestMethod] [AllDevices] public async Task DeviceLost_RaiseEvent(Device device) { - GraphicsDevice graphicsDevice = device.Get(); + using GraphicsDevice graphicsDevice = device.Get(); TaskCompletionSource<(object? Sender, DeviceLostReason Reason)> tcs = new(); @@ -28,19 +29,133 @@ public partial class DeviceLostTests RemoveDevice(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(100)); + await Task.WhenAny(tcs.Task, Task.Delay(1000)); // Ensure the event has been raised, and get the results Assert.IsTrue(tcs.Task.IsCompleted); - Assert.AreEqual(tcs.Task.Status, TaskStatus.RanToCompletion); - (object? sender, DeviceLostReason reason) = tcs.Task.Result; + (object? sender, DeviceLostReason reason) = await tcs.Task; Assert.IsNotNull(sender); Assert.AreSame(sender, graphicsDevice); Assert.AreEqual(reason, DeviceLostReason.DeviceRemoved); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_AllocateBuffer(Device device, Type bufferType) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + using var _ = graphicsDevice.AllocateBuffer(bufferType, 128); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_AllocateTexture2D(Device device, Type textureType) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + using var _ = graphicsDevice.AllocateTexture2D(textureType, 128, 128); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_AllocateTexture3D(Device device, Type textureType) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + using var _ = graphicsDevice.AllocateTexture3D(textureType, 128, 128, 3); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_IsDoublePrecisionSupportAvailable(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + _ = graphicsDevice.IsDoublePrecisionSupportAvailable(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_IsReadOnlyTexture2DSupportedForType(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + _ = graphicsDevice.IsReadOnlyTexture2DSupportedForType(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_IsReadWriteTexture2DSupportedForType(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + _ = graphicsDevice.IsReadWriteTexture2DSupportedForType(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_IsReadOnlyTexture3DSupportedForType(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + _ = graphicsDevice.IsReadOnlyTexture3DSupportedForType(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_IsReadWriteTexture3DSupportedForType(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + _ = graphicsDevice.IsReadWriteTexture3DSupportedForType(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public async Task DeviceLost_CreateComputeContext(Device device) + { + using GraphicsDevice graphicsDevice = device.Get(); + + await RemoveDeviceAsync(graphicsDevice); + + using var _ = graphicsDevice.CreateComputeContext(); + } + /// /// Removes the underlying device for a given instance. /// @@ -67,4 +182,24 @@ public partial class DeviceLostTests } } } + + /// + /// Removes the underlying device for a given instance and waits for it to be reported. + /// + /// The target instance. + private static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice) + { + TaskCompletionSource tcs = new(); + + graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null); + + RemoveDevice(graphicsDevice); + + await Task.WhenAny(tcs.Task, Task.Delay(1000)); + + if (!tcs.Task.IsCompleted) + { + Assert.Fail(); + } + } }