mirror of https://github.com/SixLabors/ImageSharp
12 changed files with 274 additions and 95 deletions
@ -0,0 +1,82 @@ |
|||||
|
// Copyright (c) Six Labors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests |
||||
|
{ |
||||
|
public partial class ImageTests |
||||
|
{ |
||||
|
public class Decode_Cancellation : ImageLoadTestBase |
||||
|
{ |
||||
|
private bool isTestStreamSeekable; |
||||
|
private readonly SemaphoreSlim notifyWaitPositionReachedSemaphore = new SemaphoreSlim(0); |
||||
|
private readonly SemaphoreSlim continueSemaphore = new SemaphoreSlim(0); |
||||
|
private readonly CancellationTokenSource cts = new CancellationTokenSource(); |
||||
|
|
||||
|
public Decode_Cancellation() |
||||
|
{ |
||||
|
this.TopLevelConfiguration.StreamProcessingBufferSize = 128; |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public async Task LoadAsync_Specific_Stream_WhenCancelledDuringRead(bool isInputStreamSeekable) |
||||
|
{ |
||||
|
this.isTestStreamSeekable = isInputStreamSeekable; |
||||
|
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
||||
|
|
||||
|
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync<Rgb24>(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public async Task LoadAsync_Agnostic_Stream_WhenCancelledDuringRead(bool isInputStreamSeekable) |
||||
|
{ |
||||
|
this.isTestStreamSeekable = isInputStreamSeekable; |
||||
|
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
||||
|
|
||||
|
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task LoadAsync_Agnostic_Path_WhenCancelledDuringRead() |
||||
|
{ |
||||
|
this.isTestStreamSeekable = true; |
||||
|
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
||||
|
|
||||
|
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task LoadAsync_Specific_Path_WhenCancelledDuringRead() |
||||
|
{ |
||||
|
this.isTestStreamSeekable = true; |
||||
|
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
||||
|
|
||||
|
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync<Rgb24>(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
||||
|
} |
||||
|
|
||||
|
private async Task DoCancel() |
||||
|
{ |
||||
|
// wait until we reach the middle of the steam
|
||||
|
await this.notifyWaitPositionReachedSemaphore.WaitAsync(); |
||||
|
|
||||
|
// set the cancellation
|
||||
|
this.cts.Cancel(); |
||||
|
|
||||
|
// continue processing the stream
|
||||
|
this.continueSemaphore.Release(); |
||||
|
} |
||||
|
|
||||
|
protected override Stream CreateStream() => this.TestFormat.CreateAsyncSamaphoreStream(this.notifyWaitPositionReachedSemaphore, this.continueSemaphore, this.isTestStreamSeekable); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue