Example #1
0
        public void CancelResultWhenFirstIsCanceledOption2()
        {
            Task <int> aTask = CreateCanceledTask();

            Task <string> result = Expression2(aTask, alwaysFinished);

            AdditionalAssertions.WaitIsCanceled(result);
        }
        public void CanceledCase()
        {
            /* Arrange */
            Task <int> canceled = Factory.CreateCanceledTask();

            /* Act */
            Task <int> second = AddTaskToChain(canceled);

            /* Assert */
            AdditionalAssertions.WaitIsCanceled(second);
        }
Example #3
0
        public void TaskCanceledBeforeStart()
        {
            /* Arrange */
            var source = new CancellationTokenSource();

            source.Cancel();

            // Act
            Task <int> task = Task.Run(() => 5, source.Token);

            // Assert
            AdditionalAssertions.WaitIsCanceled(task);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #8
0
        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);
        }