public async Task Encode_IsCancellable(JpegColorType colorType) { var cts = new CancellationTokenSource(); using var pausedStream = new PausedStream(new MemoryStream()); pausedStream.OnWaiting(s => { // after some writing if (s.Position >= 500) { cts.Cancel(); pausedStream.Release(); } else { // allows this/next wait to unblock pausedStream.Next(); } }); using var image = new Image <Rgba32>(5000, 5000); await Assert.ThrowsAsync <TaskCanceledException>(async() => { var encoder = new JpegEncoder() { ColorType = colorType }; await image.SaveAsync(pausedStream, encoder, cts.Token); }); }
public async Task SaveAsync_WithNonSeekableStream_IsCancellable() { using var image = new Image <Rgba32>(4000, 4000); var encoder = new PngEncoder() { CompressionLevel = PngCompressionLevel.BestCompression }; using var stream = new MemoryStream(); var asyncStream = new AsyncStreamWrapper(stream, () => false); var cts = new CancellationTokenSource(); var pausedStream = new PausedStream(asyncStream); pausedStream.OnWaiting(s => { cts.Cancel(); pausedStream.Release(); }); await Assert.ThrowsAsync <TaskCanceledException>(async() => await image.SaveAsync(pausedStream, encoder, cts.Token)); }