Skip to content

Commit

Permalink
fix(tests): handle lack of await using
Browse files Browse the repository at this point in the history
For ZipStreamAsyncTest we are explicitly testing for `await using`, so
it's now being marked as ignored and a dummy await is added to skip warnings

For GzipAsyncTests we are testing multiple async methods, so variants are
added that simply skips the `await using`/`DisposeAsync` and calls the
underlying `FinishAsync`
  • Loading branch information
piksel committed Sep 15, 2022
1 parent 1c2961f commit 91a9b68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
41 changes: 38 additions & 3 deletions test/ICSharpCode.SharpZipLib.Tests/GZip/GZipAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace ICSharpCode.SharpZipLib.Tests.GZip
{

#if NETCOREAPP3_1_OR_GREATER

[TestFixture]
public class GZipAsyncTests
{
Expand All @@ -21,6 +21,7 @@ public async Task SmallBufferDecompressionAsync([Values(0, 1, 3)] int seed)
var outputBuffer = new byte[outputBufferSize];
var inputBuffer = Utils.GetDummyBytes(outputBufferSize * 4, seed);

#if NETCOREAPP3_1_OR_GREATER
await using var msGzip = new MemoryStream();
await using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false})
{
Expand All @@ -44,6 +45,31 @@ public async Task SmallBufferDecompressionAsync([Values(0, 1, 3)] int seed)
Assert.AreEqual(inputBuffer[i], resultBuffer[i]);
}
}
#else
using var msGzip = new MemoryStream();
using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false})
{
await gzos.WriteAsync(inputBuffer, 0, inputBuffer.Length);
}

msGzip.Seek(0, SeekOrigin.Begin);

using (var gzis = new GZipInputStream(msGzip))
using (var msRaw = new MemoryStream())
{
int readOut;
while ((readOut = gzis.Read(outputBuffer, 0, outputBuffer.Length)) > 0)
{
await msRaw.WriteAsync(outputBuffer, 0, readOut);
}

var resultBuffer = msRaw.ToArray();
for (var i = 0; i < resultBuffer.Length; i++)
{
Assert.AreEqual(inputBuffer[i], resultBuffer[i]);
}
}
#endif
}

/// <summary>
Expand All @@ -56,13 +82,23 @@ public async Task OriginalFilenameAsync()
{
var content = "FileContents";

#if NETCOREAPP3_1_OR_GREATER
await using var ms = new MemoryStream();
await using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false })
{
outStream.FileName = "/path/to/file.ext";
outStream.Write(Encoding.ASCII.GetBytes(content));
}

#else
var ms = new MemoryStream();
var outStream = new GZipOutputStream(ms){ IsStreamOwner = false };
outStream.FileName = "/path/to/file.ext";
var bytes = Encoding.ASCII.GetBytes(content);
outStream.Write(bytes, 0, bytes.Length);
await outStream.FinishAsync(System.Threading.CancellationToken.None);
outStream.Dispose();

#endif
ms.Seek(0, SeekOrigin.Begin);

using (var inStream = new GZipInputStream(ms))
Expand All @@ -74,5 +110,4 @@ public async Task OriginalFilenameAsync()
}
}
}
#endif
}
11 changes: 7 additions & 4 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Tests.TestSupport;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Tests.TestSupport;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
[TestFixture]
public class ZipStreamAsyncTests
{
#if NETCOREAPP3_1_OR_GREATER
[Test]
[Category("Zip")]
[Category("Async")]
public async Task WriteZipStreamUsingAsync()
{
#if NETCOREAPP3_1_OR_GREATER
await using var ms = new MemoryStream();

await using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false})
Expand All @@ -28,8 +28,11 @@ public async Task WriteZipStreamUsingAsync()
}

ZipTesting.AssertValidZip(ms);
}
#else
await Task.CompletedTask;
Assert.Ignore("Async Using is not supported");
#endif
}

[Test]
[Category("Zip")]
Expand Down Expand Up @@ -119,4 +122,4 @@ public async Task WriteReadOnlyZipStreamAsync ()
}

}
}
}

0 comments on commit 91a9b68

Please sign in to comment.