From 0352740ea10fc8f97478decb9173d3868f7750d6 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 28 Apr 2026 09:14:31 +0100 Subject: [PATCH] more async tests --- .../Rar/UnpackV2017/Unpack.unpack15_async.cs | 4 +- .../Rar/UnpackV2017/Unpack.unpack20_async.cs | 24 +++++++- .../Mocks/AsyncOnlyStream.cs | 9 +-- .../Mocks/SyncWriteNotSupportedStream.cs | 58 ------------------- .../Rar/RarArchiveAsyncTests.cs | 2 +- .../Rar/RarReaderAsyncTests.cs | 2 +- 6 files changed, 27 insertions(+), 72 deletions(-) delete mode 100644 tests/SharpCompress.Test/Mocks/SyncWriteNotSupportedStream.cs diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_async.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_async.cs index e615527e..de8036b2 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_async.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_async.cs @@ -41,7 +41,7 @@ internal partial class Unpack if (((WrPtr - UnpPtr) & MaxWinMask) < 270 && WrPtr != UnpPtr) { - UnpWriteBuf20(); + await UnpWriteBuf20Async(cancellationToken).ConfigureAwait(false); } if (StMode != 0) @@ -95,6 +95,6 @@ internal partial class Unpack } } } - UnpWriteBuf20(); + await UnpWriteBuf20Async(cancellationToken).ConfigureAwait(false); } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_async.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_async.cs index 0619189a..89083eb4 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_async.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_async.cs @@ -49,7 +49,7 @@ internal partial class Unpack if (((WrPtr - UnpPtr) & MaxWinMask) < 270 && WrPtr != UnpPtr) { - UnpWriteBuf20(); + await UnpWriteBuf20Async(cancellationToken).ConfigureAwait(false); if (Suspended) { return; @@ -165,8 +165,8 @@ internal partial class Unpack continue; } } - ReadLastTables(); - UnpWriteBuf20(); + await ReadLastTables20Async(cancellationToken).ConfigureAwait(false); + await UnpWriteBuf20Async(cancellationToken).ConfigureAwait(false); } private async Task UnpWriteBuf20Async(CancellationToken cancellationToken = default) @@ -316,4 +316,22 @@ internal partial class Unpack Array.Copy(Table, 0, this.UnpOldTable20, 0, UnpOldTable20.Length); return true; } + + private async Task ReadLastTables20Async(CancellationToken cancellationToken = default) + { + if (ReadTop >= Inp.InAddr + 5) + { + if (UnpAudioBlock) + { + if (DecodeNumber(Inp, MD[UnpCurChannel]) == 256) + { + await ReadTables20Async(cancellationToken).ConfigureAwait(false); + } + } + else if (DecodeNumber(Inp, BlockTables.LD) == 269) + { + await ReadTables20Async(cancellationToken).ConfigureAwait(false); + } + } + } } diff --git a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs index 07a679ef..cf6416fe 100644 --- a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs +++ b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs @@ -5,14 +5,9 @@ using System.Threading.Tasks; namespace SharpCompress.Test.Mocks; -public class AsyncOnlyStream : Stream +public class AsyncOnlyStream(Stream stream) : Stream { - private readonly Stream _stream; - - public AsyncOnlyStream(Stream stream) - { - _stream = stream ?? throw new ArgumentNullException(nameof(stream)); - } + private readonly Stream _stream = stream ?? throw new ArgumentNullException(nameof(stream)); public override bool CanRead => _stream.CanRead; public override bool CanSeek => _stream.CanSeek; diff --git a/tests/SharpCompress.Test/Mocks/SyncWriteNotSupportedStream.cs b/tests/SharpCompress.Test/Mocks/SyncWriteNotSupportedStream.cs deleted file mode 100644 index 6ee98480..00000000 --- a/tests/SharpCompress.Test/Mocks/SyncWriteNotSupportedStream.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace SharpCompress.Test.Mocks; - -public sealed class SyncWriteNotSupportedStream(Stream stream) : Stream -{ - public override bool CanRead => stream.CanRead; - - public override bool CanSeek => stream.CanSeek; - - public override bool CanWrite => stream.CanWrite; - - public override long Length => stream.Length; - - public override long Position - { - get => stream.Position; - set => stream.Position = value; - } - - public override void Flush() => stream.Flush(); - - public override int Read(byte[] buffer, int offset, int count) => - stream.Read(buffer, offset, count); - - public override long Seek(long offset, SeekOrigin origin) => stream.Seek(offset, origin); - - public override void SetLength(long value) => stream.SetLength(value); - - public override void Write(byte[] buffer, int offset, int count) => - throw new NotSupportedException("Synchronous Write is not supported"); - - public override Task WriteAsync( - byte[] buffer, - int offset, - int count, - CancellationToken cancellationToken - ) => stream.WriteAsync(buffer, offset, count, cancellationToken); - -#if NET8_0_OR_GREATER - public override ValueTask WriteAsync( - ReadOnlyMemory buffer, - CancellationToken cancellationToken = default - ) => stream.WriteAsync(buffer, cancellationToken); -#endif - - protected override void Dispose(bool disposing) - { - if (disposing) - { - stream.Dispose(); - } - base.Dispose(disposing); - } -} diff --git a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs index 09c274c5..3293fe75 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs @@ -30,7 +30,7 @@ public class RarArchiveAsyncTests : ArchiveTests await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) { - using var output = new SyncWriteNotSupportedStream(new MemoryStream()); + using var output = new AsyncOnlyStream(new MemoryStream()); await entry.WriteToAsync(output); extractedEntries++; } diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index 1ed4b8f5..11c40f77 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -32,7 +32,7 @@ public class RarReaderAsyncTests : ReaderTests { if (!reader.Entry.IsDirectory) { - using var output = new SyncWriteNotSupportedStream(new MemoryStream()); + using var output = new AsyncOnlyStream(new MemoryStream()); await reader.WriteEntryToAsync(output); } }