public void NextTaskInChainWhilePreviousIsCanceled() { /* Arrange */ Task <int> canceled = Factory.CreateCanceledTask(); /* Act */ Task <int> result = canceled .ContinueWith(t => t.Result + 5); /* Assert */ AdditionalAssertions.WaitIsFaulted(result); AggregateException exception = result.Exception; AggregateException resultException = Assert.Throws <AggregateException>(() => result.Result); AggregateException waitException = Assert.Throws <AggregateException>(() => result.Wait(0)); Assert.NotNull(exception); AdditionalAssertions.IsTaskCanceledAggregateException(exception.Flatten()); AdditionalAssertions.IsTaskCanceledAggregateException(resultException.Flatten()); AdditionalAssertions.IsTaskCanceledAggregateException(waitException.Flatten()); AssertTaskCanceledAggregateExceptionInside(exception); AssertTaskCanceledAggregateExceptionInside(resultException); AssertTaskCanceledAggregateExceptionInside(waitException); }
public override async Task OpenWriteAsync_WithIntermediateFlushes() { // Arrange await using IDisposingContainer <BlobContainerClient> disposingContainer = await GetDisposingContainerAsync(); PageBlobClient client = GetResourceClient(disposingContainer.Container); // Act using (Stream stream = await OpenWriteAsync(client, overwrite: true, maxDataSize: 2 * Constants.KB, bufferSize: 2 * Constants.KB)) { using (var writer = new StreamWriter(stream, Encoding.ASCII)) { writer.Write(new string('A', 512)); writer.Flush(); writer.Write(new string('B', 1024)); writer.Flush(); writer.Write(new string('C', 512)); writer.Flush(); } } // Assert byte[] dataResult = (await DownloadAsync(client)).ToArray(); Assert.AreEqual(new string('A', 512) + new string('B', 1024) + new string('C', 512), Encoding.ASCII.GetString(dataResult)); await(AdditionalAssertions?.Invoke(client) ?? Task.CompletedTask); }
private void AssertTaskCanceledAggregateExceptionInside(AggregateException aggregateException) { // detailed structure Exception innerException = aggregateException.InnerExceptions.Single(); AdditionalAssertions.IsTaskCanceledAggregateException(innerException); }
public void CancelResultWhenFirstIsCanceledOption2() { Task <int> aTask = CreateCanceledTask(); Task <string> result = Expression2(aTask, alwaysFinished); AdditionalAssertions.WaitIsCanceled(result); }
public void FailedCase() { /* Arrange */ Task <int> faulted = Factory.CreateFaultedTask(); /* Act */ Task <int> second = AddTaskToChain(faulted); /* Assert */ AdditionalAssertions.WaitIsFaulted(second); }
public void CanceledCase() { /* Arrange */ Task <int> canceled = Factory.CreateCanceledTask(); /* Act */ Task <int> second = AddTaskToChain(canceled); /* Assert */ AdditionalAssertions.WaitIsCanceled(second); }
public void TaskCanceledBeforeStart() { /* Arrange */ var source = new CancellationTokenSource(); source.Cancel(); // Act Task <int> task = Task.Run(() => 5, source.Token); // Assert AdditionalAssertions.WaitIsCanceled(task); }
public void ThrowOperationCanceledExceptionWithCancellationTokenSupplied() { /* Arrange */ var source = new CancellationTokenSource(); source.CancelAfter(TimeSpan.FromSeconds(1)); /* Act */ Task <int> task = Task.Run(() => ThrowAfterSomeTimeIfCancellationRequested(source.Token), source.Token); /* Assert */ AdditionalAssertions.WaitIsCanceled(task); }
public void SelfCanceledTask() { /* Arrange */ CancellationTokenSource selfCts = new CancellationTokenSource(); /* Act */ Task <int> task = Task.Run(() => { selfCts.Cancel(); selfCts.Token.ThrowIfCancellationRequested(); return(5); }, selfCts.Token); /* Assert */ AdditionalAssertions.WaitIsCanceled(task); }
public void NextTaskInChainWhilePreviousIsFaulted() { /* Arrange */ var applicationException = new ApplicationException(); Task <int> faulted = Task.FromException <int>(applicationException); /* Act */ Task <int> result = faulted .ContinueWith(t => t.Result + 5); /* Assert */ AdditionalAssertions.WaitIsFaulted(result); Assert.NotNull(result.Exception); Assert.Throws <AggregateException>(() => result.Wait(timeout)); TaskCanceledException exception = (TaskCanceledException)result.Exception.Flatten().InnerException; }
public void Canceled() { /* Arrange */ Task <int> task = Factory.CreateCanceledTask(); /* Assert */ Assert.True(task.IsCompleted); Assert.Equal(TaskStatus.Canceled, task.Status); Assert.True(task.IsCanceled); Assert.Null(task.Exception); AdditionalAssertions.ThrowsTaskCanceledException(() => task.Result); AdditionalAssertions.ThrowsTaskCanceledException(() => task.Wait(0)); // same as AdditionalAssertions.WaitIsCanceled(task); }
public void RunToCompletionContinuationOptionWhileFirstIsCanceled() { /* Arrange */ Task <int> canceled = Factory.CreateCanceledTask(); /* Act */ Task <int> second = canceled .ContinueWith(t => t.Result + 5, TaskContinuationOptions.OnlyOnRanToCompletion); /* Assert */ AdditionalAssertions.WaitIsCanceled(second); Assert.Null(second.Exception); AggregateException resultException = Assert.Throws <AggregateException>(() => second.Result); AggregateException waitException = Assert.Throws <AggregateException>(() => second.Wait(0)); AdditionalAssertions.IsTaskCanceledAggregateException(resultException); AdditionalAssertions.IsTaskCanceledAggregateException(waitException); }
public void CancelResultWhenFirstIsCanceled() { CancellationTokenSource selfCts = new CancellationTokenSource(); Task <int> aTask = CreateCanceledTask(); Task <string> result = Task.Factory.ContinueWhenAll(new Task[] { aTask, alwaysFinished }, tasks => { if (aTask.IsCanceled) { selfCts.Cancel(); selfCts.Token.ThrowIfCancellationRequested(); return(null); } return(aTask.Result + alwaysFinished.Result); }, selfCts.Token); AdditionalAssertions.WaitIsCanceled(result); }