From f40fc3a76deb46a02e20eca0263654e8f033fc9e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 30 Oct 2021 22:41:36 +0200 Subject: [PATCH] test exception safety of ProcessPixelRows --- .../Image/ProcessPixelRowsTestBase.cs | 100 +++++++++++++----- 1 file changed, 73 insertions(+), 27 deletions(-) diff --git a/tests/ImageSharp.Tests/Image/ProcessPixelRowsTestBase.cs b/tests/ImageSharp.Tests/Image/ProcessPixelRowsTestBase.cs index 53eb1da14..255e1a9a4 100644 --- a/tests/ImageSharp.Tests/Image/ProcessPixelRowsTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ProcessPixelRowsTestBase.cs @@ -171,13 +171,16 @@ namespace SixLabors.ImageSharp.Tests Assert.Throws(() => this.ProcessPixelRowsImpl(nonDisposed, nonDisposed, disposed, (_, _, _) => { })); } - [Fact] - public void RetainsUnmangedBuffers1() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void RetainsUnmangedBuffers1(bool throwException) { - RemoteExecutor.Invoke(RunTest, this.GetType().FullName).Dispose(); + RemoteExecutor.Invoke(RunTest, this.GetType().FullName, throwException.ToString()).Dispose(); - static void RunTest(string testTypeName) + static void RunTest(string testTypeName, string throwExceptionStr) { + bool throwExceptionInner = bool.Parse(throwExceptionStr); var buffer = new UnmanagedBuffer(100); var allocator = new MockUnmanagedMemoryAllocator(buffer); Configuration.Default.MemoryAllocator = allocator; @@ -185,22 +188,36 @@ namespace SixLabors.ImageSharp.Tests var image = new Image(10, 10); Assert.Equal(1, UnmanagedMemoryHandle.TotalOutstandingHandles); - GetTest(testTypeName).ProcessPixelRowsImpl(image, _ => + try + { + GetTest(testTypeName).ProcessPixelRowsImpl(image, _ => + { + buffer.BufferHandle.Dispose(); + Assert.Equal(1, UnmanagedMemoryHandle.TotalOutstandingHandles); + if (throwExceptionInner) + { + throw new NonFatalException(); + } + }); + } + catch (NonFatalException) { - buffer.BufferHandle.Dispose(); - Assert.Equal(1, UnmanagedMemoryHandle.TotalOutstandingHandles); - }); + } + Assert.Equal(0, UnmanagedMemoryHandle.TotalOutstandingHandles); } } - [Fact] - public void RetainsUnmangedBuffers2() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void RetainsUnmangedBuffers2(bool throwException) { - RemoteExecutor.Invoke(RunTest, this.GetType().FullName).Dispose(); + RemoteExecutor.Invoke(RunTest, this.GetType().FullName, throwException.ToString()).Dispose(); - static void RunTest(string testTypeName) + static void RunTest(string testTypeName, string throwExceptionStr) { + bool throwExceptionInner = bool.Parse(throwExceptionStr); var buffer1 = new UnmanagedBuffer(100); var buffer2 = new UnmanagedBuffer(100); var allocator = new MockUnmanagedMemoryAllocator(buffer1, buffer2); @@ -210,23 +227,37 @@ namespace SixLabors.ImageSharp.Tests var image2 = new Image(10, 10); Assert.Equal(2, UnmanagedMemoryHandle.TotalOutstandingHandles); - GetTest(testTypeName).ProcessPixelRowsImpl(image1, image2, (_, _) => + try { - buffer1.BufferHandle.Dispose(); - buffer2.BufferHandle.Dispose(); - Assert.Equal(2, UnmanagedMemoryHandle.TotalOutstandingHandles); - }); + GetTest(testTypeName).ProcessPixelRowsImpl(image1, image2, (_, _) => + { + buffer1.BufferHandle.Dispose(); + buffer2.BufferHandle.Dispose(); + Assert.Equal(2, UnmanagedMemoryHandle.TotalOutstandingHandles); + if (throwExceptionInner) + { + throw new NonFatalException(); + } + }); + } + catch (NonFatalException) + { + } + Assert.Equal(0, UnmanagedMemoryHandle.TotalOutstandingHandles); } } - [Fact] - public void RetainsUnmangedBuffers3() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void RetainsUnmangedBuffers3(bool throwException) { - RemoteExecutor.Invoke(RunTest, this.GetType().FullName).Dispose(); + RemoteExecutor.Invoke(RunTest, this.GetType().FullName, throwException.ToString()).Dispose(); - static void RunTest(string testTypeName) + static void RunTest(string testTypeName, string throwExceptionStr) { + bool throwExceptionInner = bool.Parse(throwExceptionStr); var buffer1 = new UnmanagedBuffer(100); var buffer2 = new UnmanagedBuffer(100); var buffer3 = new UnmanagedBuffer(100); @@ -238,13 +269,24 @@ namespace SixLabors.ImageSharp.Tests var image3 = new Image(10, 10); Assert.Equal(3, UnmanagedMemoryHandle.TotalOutstandingHandles); - GetTest(testTypeName).ProcessPixelRowsImpl(image1, image2, image3, (_, _, _) => + try + { + GetTest(testTypeName).ProcessPixelRowsImpl(image1, image2, image3, (_, _, _) => + { + buffer1.BufferHandle.Dispose(); + buffer2.BufferHandle.Dispose(); + buffer3.BufferHandle.Dispose(); + Assert.Equal(3, UnmanagedMemoryHandle.TotalOutstandingHandles); + if (throwExceptionInner) + { + throw new NonFatalException(); + } + }); + } + catch (NonFatalException) { - buffer1.BufferHandle.Dispose(); - buffer2.BufferHandle.Dispose(); - buffer3.BufferHandle.Dispose(); - Assert.Equal(3, UnmanagedMemoryHandle.TotalOutstandingHandles); - }); + } + Assert.Equal(0, UnmanagedMemoryHandle.TotalOutstandingHandles); } } @@ -255,6 +297,10 @@ namespace SixLabors.ImageSharp.Tests return (ProcessPixelRowsTestBase)Activator.CreateInstance(type); } + private class NonFatalException : Exception + { + } + private class MockUnmanagedMemoryAllocator : MemoryAllocator where T1 : struct {