public async Task WriteAsync_SemaphoreIncludesWriteCoreAsync_Task() { var handler = new DelayedWriter(this.sendingStream, this.receivingStream, new MessagePackFormatter()); var writeTask = handler.WriteAsync(CreateRequestMessage(), CancellationToken.None).AsTask(); var write2Task = handler.WriteAsync(CreateRequestMessage(), CancellationToken.None).AsTask(); // Give the library extra time to do the wrong thing asynchronously. await Task.Delay(ExpectedTimeout); Assert.Equal(1, handler.WriteCoreCallCount); handler.WriteBlock.Set(); await Task.WhenAll(writeTask, write2Task).WithTimeout(UnexpectedTimeout); Assert.Equal(2, handler.WriteCoreCallCount); }
public async Task WriteAsync_PreferOperationCanceledException_MidExecution() { var handler = new DelayedWriter(this.sendingStream, this.receivingStream, new MessagePackFormatter()); var cts = new CancellationTokenSource(); var writeTask = handler.WriteAsync(CreateRequestMessage(), cts.Token); cts.Cancel(); handler.Dispose(); // 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()); }