From c53ca372f2f28a7bb562afcdafa68aa0805be8b8 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 31 Oct 2025 11:39:57 +0000 Subject: [PATCH] don't use pools in tests --- .../Streams/LzmaStreamTests.cs | 3 +- .../Streams/SharpCompressStreamAsyncTests.cs | 169 +++++++----------- .../Streams/SharpCompressStreamTest.cs | 14 +- 3 files changed, 72 insertions(+), 114 deletions(-) diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs index fd909b79..5b00140b 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs @@ -581,7 +581,7 @@ public class LzmaStreamTests false ); - var buffer = ArrayPool.Shared.Rent(1024); + var buffer = new byte[1024]; long totalRead = 0; while (totalRead < decompressedSize) { @@ -597,6 +597,5 @@ public class LzmaStreamTests break; } } - ArrayPool.Shared.Return(buffer); } } diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamAsyncTests.cs index a33560a6..bca1ad5f 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamAsyncTests.cs @@ -28,148 +28,113 @@ public class SharpCompressStreamAsyncTests [Fact] public async Task BufferReadAsyncTest() { - byte[] data = ArrayPool.Shared.Rent(0x100000); - byte[] test = ArrayPool.Shared.Rent(0x1000); - try + byte[] data = new byte[0x100000]; + byte[] test = new byte[0x1000]; + using (MemoryStream ms = new MemoryStream(data)) { - using (MemoryStream ms = new MemoryStream(data)) + CreateData(ms); + + using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) { - CreateData(ms); + IStreamStack stack = (IStreamStack)scs; - using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) - { - IStreamStack stack = (IStreamStack)scs; + scs.Seek(0x1000, SeekOrigin.Begin); + Assert.Equal(0x1000, scs.Position); // position in the SharpCompressionStream + Assert.Equal(0x1000, ms.Position); // initial seek + full buffer read - scs.Seek(0x1000, SeekOrigin.Begin); - Assert.Equal(0x1000, scs.Position); // position in the SharpCompressionStream - Assert.Equal(0x1000, ms.Position); // initial seek + full buffer read + await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 + Assert.Equal(0x2000, scs.Position); // stream has correct position + Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct + Assert.Equal(0x11000, ms.Position); // seek plus read bytes - await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 - Assert.Equal(0x2000, scs.Position); // stream has correct position - Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct - Assert.Equal(0x11000, ms.Position); // seek plus read bytes - - scs.Seek(0x500, SeekOrigin.Begin); // seek before the buffer start - await scs.ReadAsync(test, 0, test.Length); // read bytes 0x500 to 0x1500 - Assert.Equal(0x1500, scs.Position); // stream has correct position - Assert.True(data.Skip(0x500).Take(test.Length).SequenceEqual(test)); // is the data correct - Assert.Equal(0x10500, ms.Position); // seek plus read bytes - } + scs.Seek(0x500, SeekOrigin.Begin); // seek before the buffer start + await scs.ReadAsync(test, 0, test.Length); // read bytes 0x500 to 0x1500 + Assert.Equal(0x1500, scs.Position); // stream has correct position + Assert.True(data.Skip(0x500).Take(test.Length).SequenceEqual(test)); // is the data correct + Assert.Equal(0x10500, ms.Position); // seek plus read bytes } } - finally - { - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test); - } } [Fact] public async Task BufferReadAndSeekAsyncTest() { - byte[] data = ArrayPool.Shared.Rent(0x100000); - byte[] test = ArrayPool.Shared.Rent(0x1000); - try + byte[] data = new byte[0x100000]; + byte[] test = new byte[0x1000]; + using (MemoryStream ms = new MemoryStream(data)) { - using (MemoryStream ms = new MemoryStream(data)) + CreateData(ms); + + using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) { - CreateData(ms); + IStreamStack stack = (IStreamStack)scs; - using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) - { - IStreamStack stack = (IStreamStack)scs; + await scs.ReadAsync(test, 0, test.Length); // read bytes 0 to 0x1000 + Assert.True(data.Take(test.Length).SequenceEqual(test)); // is the data correct + Assert.Equal(0x1000, scs.Position); // stream has correct position + Assert.Equal(0x10000, ms.Position); // moved the base stream on by buffer size - await scs.ReadAsync(test, 0, test.Length); // read bytes 0 to 0x1000 - Assert.True(data.Take(test.Length).SequenceEqual(test)); // is the data correct - Assert.Equal(0x1000, scs.Position); // stream has correct position - Assert.Equal(0x10000, ms.Position); // moved the base stream on by buffer size + await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 + Assert.Equal(0x2000, scs.Position); // stream has correct position + Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct + Assert.Equal(0x10000, ms.Position); // the base stream has not moved - await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 - Assert.Equal(0x2000, scs.Position); // stream has correct position - Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct - Assert.Equal(0x10000, ms.Position); // the base stream has not moved + // rewind the buffer + stack.Rewind(0x1000); // rewind buffer back by 0x1000 bytes - // rewind the buffer - stack.Rewind(0x1000); // rewind buffer back by 0x1000 bytes - - // repeat the previous test - await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 - Assert.Equal(0x2000, scs.Position); // stream has correct position - Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct - Assert.Equal(0x10000, ms.Position); // the base stream has not moved - } + // repeat the previous test + await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000 + Assert.Equal(0x2000, scs.Position); // stream has correct position + Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct + Assert.Equal(0x10000, ms.Position); // the base stream has not moved } } - finally - { - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test); - } } [Fact] public async Task MultipleAsyncReadsTest() { - byte[] data = ArrayPool.Shared.Rent(0x100000); - byte[] test1 = ArrayPool.Shared.Rent(0x800); - byte[] test2 = ArrayPool.Shared.Rent(0x800); - try + byte[] data = new byte[0x100000]; + byte[] test1 = new byte[0x800]; + byte[] test2 = new byte[0x800]; + using (MemoryStream ms = new MemoryStream(data)) { - using (MemoryStream ms = new MemoryStream(data)) + CreateData(ms); + + using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) { - CreateData(ms); + // Read first chunk + await scs.ReadAsync(test1, 0, test1.Length); + Assert.Equal(0x800, scs.Position); + Assert.True(data.Take(test1.Length).SequenceEqual(test1)); // first read is correct - using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) - { - // Read first chunk - await scs.ReadAsync(test1, 0, test1.Length); - Assert.Equal(0x800, scs.Position); - Assert.True(data.Take(test1.Length).SequenceEqual(test1)); // first read is correct - - // Read second chunk - await scs.ReadAsync(test2, 0, test2.Length); - Assert.Equal(0x1000, scs.Position); - Assert.True(data.Skip(test1.Length).Take(test2.Length).SequenceEqual(test2)); // second read is correct - } + // Read second chunk + await scs.ReadAsync(test2, 0, test2.Length); + Assert.Equal(0x1000, scs.Position); + Assert.True(data.Skip(test1.Length).Take(test2.Length).SequenceEqual(test2)); // second read is correct } } - finally - { - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test1); - ArrayPool.Shared.Return(test2); - } } [Fact] public async Task LargeBufferAsyncReadTest() { - byte[] data = ArrayPool.Shared.Rent(0x200000); - byte[] test = ArrayPool.Shared.Rent(0x8000); - try + byte[] data = new byte[0x200000]; + byte[] test = new byte[0x8000]; + using (MemoryStream ms = new MemoryStream(data)) { - using (MemoryStream ms = new MemoryStream(data)) - { - CreateData(ms); + CreateData(ms); - using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) + using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000)) + { + for (int i = 0; i < 10; i++) { - for (int i = 0; i < 10; i++) - { - await scs.ReadAsync(test, 0, test.Length); - long expectedPosition = (long)(i + 1) * test.Length; - Assert.Equal(expectedPosition, scs.Position); - Assert.True( - data.Skip(i * test.Length).Take(test.Length).SequenceEqual(test) - ); - } + await scs.ReadAsync(test, 0, test.Length); + long expectedPosition = (long)(i + 1) * test.Length; + Assert.Equal(expectedPosition, scs.Position); + Assert.True(data.Skip(i * test.Length).Take(test.Length).SequenceEqual(test)); } } } - finally - { - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test); - } } } diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamTest.cs index 5161076b..1c4a8b57 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamTest.cs @@ -27,8 +27,8 @@ public class SharpCompressStreamTests [Fact] public void BufferReadTest() { - byte[] data = ArrayPool.Shared.Rent(0x100000); - byte[] test = ArrayPool.Shared.Rent(0x1000); + byte[] data = new byte[0x100000]; + byte[] test = new byte[0x1000]; using (MemoryStream ms = new MemoryStream(data)) { createData(ms); @@ -53,16 +53,13 @@ public class SharpCompressStreamTests Assert.Equal(0x10500, ms.Position); //seek plus read bytes } } - - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test); } [Fact] public void BufferReadAndSeekTest() { - byte[] data = ArrayPool.Shared.Rent(0x100000); - byte[] test = ArrayPool.Shared.Rent(0x1000); + byte[] data = new byte[0x100000]; + byte[] test = new byte[0x1000]; using (MemoryStream ms = new MemoryStream(data)) { createData(ms); @@ -91,8 +88,5 @@ public class SharpCompressStreamTests Assert.Equal(0x10000, ms.Position); //the base stream has not moved } } - - ArrayPool.Shared.Return(data); - ArrayPool.Shared.Return(test); } }