public async Task WriteAsync_PreferOperationCanceledException_MidExecution() { var handler = new DelayedWriter(this.sendingStream, this.receivingStream, new JsonMessageFormatter()); var cts = new CancellationTokenSource(); var writeTask = handler.WriteAsync(CreateRequestMessage(), cts.Token); cts.Cancel(); await handler.DisposeAsync(); // Unblock writer. It should not throw anything as it is to emulate not recognizing the // CancellationToken before completing its work. handler.WriteBlock.Set(); await Assert.ThrowsAnyAsync <OperationCanceledException>(() => writeTask.AsTask()); }