Skip to content

Commit 91a9b68

Browse files
committed
fix(tests): handle lack of await using
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`
1 parent 1c2961f commit 91a9b68

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

test/ICSharpCode.SharpZipLib.Tests/GZip/GZipAsyncTests.cs

+38-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace ICSharpCode.SharpZipLib.Tests.GZip
99
{
1010

11-
#if NETCOREAPP3_1_OR_GREATER
11+
1212
[TestFixture]
1313
public class GZipAsyncTests
1414
{
@@ -21,6 +21,7 @@ public async Task SmallBufferDecompressionAsync([Values(0, 1, 3)] int seed)
2121
var outputBuffer = new byte[outputBufferSize];
2222
var inputBuffer = Utils.GetDummyBytes(outputBufferSize * 4, seed);
2323

24+
#if NETCOREAPP3_1_OR_GREATER
2425
await using var msGzip = new MemoryStream();
2526
await using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false})
2627
{
@@ -44,6 +45,31 @@ public async Task SmallBufferDecompressionAsync([Values(0, 1, 3)] int seed)
4445
Assert.AreEqual(inputBuffer[i], resultBuffer[i]);
4546
}
4647
}
48+
#else
49+
using var msGzip = new MemoryStream();
50+
using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false})
51+
{
52+
await gzos.WriteAsync(inputBuffer, 0, inputBuffer.Length);
53+
}
54+
55+
msGzip.Seek(0, SeekOrigin.Begin);
56+
57+
using (var gzis = new GZipInputStream(msGzip))
58+
using (var msRaw = new MemoryStream())
59+
{
60+
int readOut;
61+
while ((readOut = gzis.Read(outputBuffer, 0, outputBuffer.Length)) > 0)
62+
{
63+
await msRaw.WriteAsync(outputBuffer, 0, readOut);
64+
}
65+
66+
var resultBuffer = msRaw.ToArray();
67+
for (var i = 0; i < resultBuffer.Length; i++)
68+
{
69+
Assert.AreEqual(inputBuffer[i], resultBuffer[i]);
70+
}
71+
}
72+
#endif
4773
}
4874

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

85+
#if NETCOREAPP3_1_OR_GREATER
5986
await using var ms = new MemoryStream();
6087
await using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false })
6188
{
6289
outStream.FileName = "/path/to/file.ext";
6390
outStream.Write(Encoding.ASCII.GetBytes(content));
6491
}
65-
92+
#else
93+
var ms = new MemoryStream();
94+
var outStream = new GZipOutputStream(ms){ IsStreamOwner = false };
95+
outStream.FileName = "/path/to/file.ext";
96+
var bytes = Encoding.ASCII.GetBytes(content);
97+
outStream.Write(bytes, 0, bytes.Length);
98+
await outStream.FinishAsync(System.Threading.CancellationToken.None);
99+
outStream.Dispose();
100+
101+
#endif
66102
ms.Seek(0, SeekOrigin.Begin);
67103

68104
using (var inStream = new GZipInputStream(ms))
@@ -74,5 +110,4 @@ public async Task OriginalFilenameAsync()
74110
}
75111
}
76112
}
77-
#endif
78113
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
using System.IO;
22
using System.Threading;
33
using System.Threading.Tasks;
4-
using ICSharpCode.SharpZipLib.Tests.TestSupport;
54
using ICSharpCode.SharpZipLib.Zip;
5+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
66
using NUnit.Framework;
77

88
namespace ICSharpCode.SharpZipLib.Tests.Zip
99
{
1010
[TestFixture]
1111
public class ZipStreamAsyncTests
1212
{
13-
#if NETCOREAPP3_1_OR_GREATER
1413
[Test]
1514
[Category("Zip")]
1615
[Category("Async")]
1716
public async Task WriteZipStreamUsingAsync()
1817
{
18+
#if NETCOREAPP3_1_OR_GREATER
1919
await using var ms = new MemoryStream();
2020

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

3030
ZipTesting.AssertValidZip(ms);
31-
}
31+
#else
32+
await Task.CompletedTask;
33+
Assert.Ignore("Async Using is not supported");
3234
#endif
35+
}
3336

3437
[Test]
3538
[Category("Zip")]
@@ -119,4 +122,4 @@ public async Task WriteReadOnlyZipStreamAsync ()
119122
}
120123

121124
}
122-
}
125+
}

0 commit comments

Comments
 (0)