public void Iterate_Generic_ExecutesNextTaskOnlyAfterPreviousTaskSucceeded()
        {
            Task <IEnumerable <int> > result = TaskHelpers.Iterate(SuccessTaskEnumerable_Generic());

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.RanToCompletion, result.Status);
            Assert.Equal(new[] { 42, 2112 }, result.Result);
        }
Ejemplo n.º 2
0
        public void Iterate_NonGeneric_TasksRunSequentiallyRegardlessOfExecutionTime()
        {
            List <string> log = new List <string>();

            Task task = TaskHelpers.Iterate(TasksWithVaryingDelays(log, 100, 1, 50, 2));

            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Equal(new[] { "ENTER: 100", "EXIT: 100", "ENTER: 1", "EXIT: 1", "ENTER: 50", "EXIT: 50", "ENTER: 2", "EXIT: 2" }, log);
        }
Ejemplo n.º 3
0
        public Task Iterate_NonGeneric_IteratorRunsInSynchronizationContext()
        {
            ThreadPoolSyncContext sc = new ThreadPoolSyncContext();

            SynchronizationContext.SetSynchronizationContext(sc);

            return(TaskHelpers.Iterate(SyncContextVerifyingEnumerable(sc)).Then(() =>
            {
                Assert.Same(sc, SynchronizationContext.Current);
            }));
        }
Ejemplo n.º 4
0
        public void Iterate_NonGeneric_IfProvidedEnumerableExecutesCancellingTask_ReturnsCanceledTaskAndHaltsEnumeration()
        {
            List <string> log = new List <string>();

            var result = TaskHelpers.Iterate(CanceledTaskEnumerable(log));

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Canceled, result.Status);
            Assert.Equal(new[] { "first" }, log.ToArray());
        }
Ejemplo n.º 5
0
        public void Iterate_NonGeneric_IfProvidedEnumerationContainsNullValue_ReturnsFaultedTaskWithNullReferenceException()
        {
            List <string> log = new List <string>();

            var result = TaskHelpers.Iterate(NullTaskEnumerable(log));

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Faulted, result.Status);
            Assert.IsType <NullReferenceException>(result.Exception.GetBaseException());
        }
        public Task Iterate_Generic_IteratorRunsInSynchronizationContext()
        {
            ThreadPoolSyncContext sc = new ThreadPoolSyncContext();

            SynchronizationContext.SetSynchronizationContext(sc);

            return(TaskHelpers.Iterate(SyncContextVerifyingEnumerable_Generic(sc)).Then(result =>
            {
                Assert.Same(sc, SynchronizationContext.Current);
                Assert.Equal(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, result);
            }));
        }
Ejemplo n.º 7
0
        public void Iterate_NonGeneric_ExecutesNextTaskOnlyAfterPreviousTaskSucceeded()
        {
            List <string> log = new List <string>();

            var result = TaskHelpers.Iterate(SuccessTaskEnumerable(log));

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.RanToCompletion, result.Status);
            Assert.Equal(
                new[] { "first", "Executing first task. Log size: 1", "second", "Executing second task. Log size: 3" },
                log.ToArray());
        }
Ejemplo n.º 8
0
        public void Iterate_NonGeneric_IfProvidedEnumerationThrowsException_ReturnsFaultedTask()
        {
            List <string> log       = new List <string>();
            Exception     exception = new Exception();

            var result = TaskHelpers.Iterate(ThrowingTaskEnumerable(exception, log));

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Faulted, result.Status);
            Assert.Same(exception, result.Exception.InnerException);
            Assert.Equal(new[] { "first" }, log.ToArray());
        }
        public void Iterate_Generic_IfProvidedEnumerableExecutesFaultingTask_ReturnsCanceledTaskAndHaltsEnumeration()
        {
            List <string> log       = new List <string>();
            Exception     exception = new Exception();

            Task <IEnumerable <object> > result = TaskHelpers.Iterate(FaultedTaskEnumerable_Generic(exception, log));

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Faulted, result.Status);
            Assert.Same(exception, result.Exception.InnerException);
            Assert.Equal(new[] { "first" }, log.ToArray());
        }
Ejemplo n.º 10
0
        public void Iterate_NonGeneric_StopsTaskIterationIfCancellationWasRequested()
        {
            List <string>           log = new List <string>();
            CancellationTokenSource cts = new CancellationTokenSource();

            var result = TaskHelpers.Iterate(CancelingTaskEnumerable(log, cts), cts.Token);

            Assert.NotNull(result);
            result.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Canceled, result.Status);
            Assert.Equal(
                new[] { "first", "Executing first task. Log size: 1" },
                log.ToArray());
        }