Browse Source

fix Identify cancellation tests

af/decoder-tests
Anton Firszov 3 years ago
parent
commit
00188456b1
  1. 18
      tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs
  2. 4
      tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs
  3. 2
      tests/ImageSharp.Tests/TestUtilities/PausedStream.cs

18
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<bool, string, double> GetTestData()
private static TheoryData<bool, string, double> 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<bool, string, double> TestData { get; } = GetTestData();
public static TheoryData<bool, string, double> 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<bool, string, double> 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();

4
tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs

@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities;
/// <summary>
/// <see cref="PausedMemoryStream"/> is a variant of <see cref="PausedStream"/> that derives from <see cref="MemoryStream"/> instead of encapsulating it.
/// It is used to test decoder cancellation without relying on of our standard prefetching of arbitrary streams to <see cref="ImageSharp.IO.ChunkedMemoryStream"/>
/// It is used to test decoder REacellation without relying on of our standard prefetching of arbitrary streams to <see cref="ImageSharp.IO.ChunkedMemoryStream"/>
/// on asynchronous path.
/// </summary>
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

2
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

Loading…
Cancel
Save