diff --git a/src/ImageSharp/Image/PixelPool{TColor}.cs b/src/ImageSharp/Image/PixelPool{TColor}.cs index 8673499a88..1f33926214 100644 --- a/src/ImageSharp/Image/PixelPool{TColor}.cs +++ b/src/ImageSharp/Image/PixelPool{TColor}.cs @@ -36,15 +36,7 @@ namespace ImageSharp /// The array to return to the buffer pool. public static void ReturnPixels(TColor[] array) { - try - { - ArrayPool.Return(array, true); - } - catch - { - // Do nothing. - // Hacky but it allows us to attempt to return non-pooled arrays and arrays that have already been returned - } + ArrayPool.Return(array, true); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/PixelPoolTests.cs b/tests/ImageSharp.Tests/Image/PixelPoolTests.cs new file mode 100644 index 0000000000..0b762cf7c3 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/PixelPoolTests.cs @@ -0,0 +1,74 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System.Linq; + + using Xunit; + + /// + /// Tests the class. + /// + public class PixelPoolTests + { + [Fact] + public void PixelPoolRentsMinimumSize() + { + Color[] pixels = PixelPool.RentPixels(1024); + + Assert.True(pixels.Length >= 1024); + } + + [Fact] + public void PixelPoolRentsEmptyArray() + { + for (int i = 16; i < 1024; i += 16) + { + Color[] pixels = PixelPool.RentPixels(i); + + Assert.True(pixels.All(p => p == default(Color))); + + PixelPool.ReturnPixels(pixels); + } + + for (int i = 16; i < 1024; i += 16) + { + Color[] pixels = PixelPool.RentPixels(i); + + Assert.True(pixels.All(p => p == default(Color))); + + PixelPool.ReturnPixels(pixels); + } + } + + [Fact] + public void PixelPoolDoesNotThrowWhenReturningNonPooled() + { + Color[] pixels = new Color[1024]; + + PixelPool.ReturnPixels(pixels); + + Assert.True(pixels.Length >= 1024); + } + + [Fact] + public void PixelPoolCleansRentedArray() + { + Color[] pixels = PixelPool.RentPixels(256); + + for (int i = 0; i < pixels.Length; i++) + { + pixels[i] = Color.Azure; + } + + Assert.True(pixels.All(p => p == Color.Azure)); + + PixelPool.ReturnPixels(pixels); + + Assert.True(pixels.All(p => p == default(Color))); + } + } +} \ No newline at end of file