diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs index c93831491f..88afa07474 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs @@ -16,7 +16,7 @@ public partial class ImageTests public Decode_Cancellation() => this.TopLevelConfiguration.StreamProcessingBufferSize = 128; - private static TheoryData GetTestData() + private static TheoryData GetTestData(bool identify) { string[] testFileForEachCodec = new[] { @@ -39,17 +39,23 @@ public partial class ImageTests foreach (double p in percentages) { data.Add(false, file, p); - data.Add(true, file, p); + + // Do not test "direct" decoder cancellation for Identify for percentages other than 0% to avoid fine-tuning the percentages. + // Cancellation should happen before we read enough data to consider the stream "identified". This can be very early for some formats/files. + if (!identify && p > 0) + { + data.Add(true, file, p); + } } } return data; } - public static TheoryData TestData { get; } = GetTestData(); + public static TheoryData IdentifyData { get; } = GetTestData(identify: true); [Theory] - [MemberData(nameof(TestData))] + [MemberData(nameof(IdentifyData))] public async Task IdentifyAsync_IsCancellable(bool useMemoryStream, string file, double percentageOfStreamReadToCancel) { CancellationTokenSource cts = new(); @@ -82,8 +88,10 @@ public partial class ImageTests .WaitAsync(TimeSpan.FromSeconds(10)); } + public static TheoryData LoadData { get; } = GetTestData(identify: false); + [Theory] - [MemberData(nameof(TestData))] + [MemberData(nameof(LoadData))] public async Task LoadAsync_IsCancellable(bool useMemoryStream, string file, double percentageOfStreamReadToCancel) { CancellationTokenSource cts = new(); diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs index 17d742e30f..5b285b4caa 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs @@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities; /// /// is a variant of that derives from instead of encapsulating it. -/// It is used to test decoder cancellation without relying on of our standard prefetching of arbitrary streams to +/// It is used to test decoder REacellation without relying on of our standard prefetching of arbitrary streams to /// on asynchronous path. /// public class PausedMemoryStream : MemoryStream, IPausedStream @@ -82,8 +82,6 @@ public class PausedMemoryStream : MemoryStream, IPausedStream public override bool CanTimeout => base.CanTimeout; - public override void Close() => this.Await(() => base.Close()); - public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // To make sure the copy operation is buffered and pausable, we should override MemoryStream's strategy diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs index 05f5f7a060..8af168d67f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs @@ -85,8 +85,6 @@ public class PausedStream : Stream, IPausedStream public override bool CanTimeout => this.innerStream.CanTimeout; - public override void Close() => this.Await(() => this.innerStream.Close()); - public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { // To make sure the copy operation is buffered and pausable, we should override MemoryStream's strategy