コード例 #1
0
        public void TaskT1_Then_FuncT1T2__Normal()
        {
            var  mock = new Mock <IAfter>();
            Task task = SimpleTaskFactory.Run(() => 12)
                        .Then(result => mock.Object.NextFunctionWithInput(result));

            task.Wait();

            mock.Verify(then => then.NextFunctionWithInput(12), Times.Once);
        }
コード例 #2
0
        public void Task_Then_FuncT2__Normal()
        {
            var           mock = new Mock <IAfter>();
            Task <string> task = SimpleTaskFactory.Run(() => { })
                                 .Then(() => mock.Object.NextFunction());

            task.Wait();

            mock.Verify(then => then.NextFunction(), Times.Once);
        }
コード例 #3
0
        public void Task_Then_TaskAction__NullTask()
        {
            Task task = SimpleTaskFactory.Run(() => { /*do nothing*/ })
                        .Then(() => (Task)null);

            try
            {
                task.Wait();
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf <ArgumentNullException>(e.InnerException);
            }
        }
コード例 #4
0
        public void TaskT1_Then_TaskFuncT1T2__NullTask()
        {
            Task task = SimpleTaskFactory.Run(() => 12)
                        .Then(result => (Task <string>)null);

            try
            {
                task.Wait();
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf <ArgumentNullException>(e.InnerException);
            }
        }
コード例 #5
0
        public void Task_Catch__Return()
        {
            var mock            = new Mock <IAfter>();
            var thrownException = new Exception();

            Task task = SimpleTaskFactory
                        .Run(() =>
            {
                throw thrownException;
            })
                        .Catch <Exception>(exception => mock.Object.CatchExceptionHandler(exception));

            task.Wait();

            mock.Verify(then => then.CatchExceptionHandler(thrownException), Times.Once);
        }
コード例 #6
0
        public void Task_Finally_Action__ExceptionInFirst()
        {
            var  mock            = new Mock <IAfter>();
            var  thrownException = new Exception();
            Task task            = SimpleTaskFactory.Run(() => { throw thrownException; })
                                   .Finally(() => mock.Object.NextAction());

            try
            {
                task.Wait();
                Assert.Fail("task must bubble up the exception");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException, e.InnerException);
                mock.Verify(then => then.NextAction(), Times.Once);
                Assert.Pass();
            }
        }
コード例 #7
0
        public void TaskT_Catch__Return()
        {
            var mock            = new Mock <IAfter>();
            var thrownException = new Exception();

            mock.Setup(o => o.CatchExceptionHandlerWithOutput(thrownException)).Returns(123);

            Task <int> task = SimpleTaskFactory
                              .Run(() =>
            {
                throw thrownException;
                return(default(int));
            })
                              .Catch <int, Exception>(exception => mock.Object.CatchExceptionHandlerWithOutput(exception));

            task.Wait();

            mock.Verify(then => then.CatchExceptionHandlerWithOutput(thrownException), Times.Once);
            Assert.AreEqual(123, task.Result);
        }
コード例 #8
0
        public void TaskT_Finally_Action__ExceptionInSecond()
        {
            var        thrownException = new Exception();
            Task <int> task            = SimpleTaskFactory.Run(() => 12)
                                         .Finally(() =>
            {
                throw thrownException;
            });

            try
            {
                task.Wait();
                Assert.Fail("task must bubble up the exception");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException, e.InnerException);
                Assert.Pass();
            }
        }
コード例 #9
0
        public void Task_2Catch__FirstMatches_SecondDoesntMatch()
        {
            var mock1 = new Mock <IAfter>();
            var mock2 = new Mock <IAfter>();

            var ex1 = new Ex1();

            Task task = SimpleTaskFactory
                        .Run(() =>
            {
                throw ex1;
            })
                        .Catch <Ex1>(exception => mock1.Object.CatchExceptionHandler(exception))
                        .Catch <Ex2>(exception => mock2.Object.CatchExceptionHandler(exception));

            task.Wait();

            mock1.Verify(then => then.CatchExceptionHandler(ex1), Times.Once);
            mock2.Verify(then => then.CatchExceptionHandler(It.IsAny <Exception>()), Times.Never);
            Assert.Pass();
        }
コード例 #10
0
        public void Task_Finally_Action__ExceptionInFirstAndSecond()
        {
            var  thrownException1 = new Exception();
            var  thrownException2 = new Exception();
            Task task             = SimpleTaskFactory.Run(() => { throw thrownException1; })
                                    .Finally(() =>
            {
                throw thrownException2;
            });

            try
            {
                task.Wait();
                Assert.Fail("task must bubble up the exception");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException2, e.InnerException);
                Assert.Pass();
            }
        }
コード例 #11
0
        public void Task_Then_TaskAction__ExceptionInSecond()
        {
            var  thrownException = new Exception();
            Task task            = SimpleTaskFactory.Run(() => { })
                                   .Then(() =>
                                         SimpleTaskFactory.Run(() =>
            {
                throw thrownException;
            }));

            try
            {
                task.Wait();
                Assert.Fail("task must bubble up the exception");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException, e.InnerException);
                Assert.Pass();
            }
        }
コード例 #12
0
        public void TaskT_2Catch__FirstDoesntMatch_SecondMatches()
        {
            var mock1 = new Mock <IAfter>();
            var mock2 = new Mock <IAfter>();

            var ex1 = new Ex1();

            Task <int> task = SimpleTaskFactory
                              .Run(() =>
            {
                throw ex1;
                return(default(int));
            })
                              .Catch <int, Ex2>(exception => mock1.Object.CatchExceptionHandlerWithOutput(exception))
                              .Catch <int, Ex1>(exception => mock2.Object.CatchExceptionHandlerWithOutput(exception));

            task.Wait();

            mock1.Verify(then => then.CatchExceptionHandlerWithOutput(It.IsAny <Exception>()), Times.Never);
            mock2.Verify(then => then.CatchExceptionHandlerWithOutput(ex1), Times.Once);
            Assert.Pass();
        }
コード例 #13
0
        public void Task_Catch__ThrowAgain()
        {
            var thrownException1 = new Exception();
            var thrownException2 = new Exception();

            Task task = SimpleTaskFactory
                        .Run(() =>
            {
                throw thrownException1;
            })
                        .Catch <Exception>(exception => { throw thrownException2; });

            try
            {
                task.Wait();
                Assert.Fail("thrownException2 not propagated");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException2, e.InnerException);
                Assert.Pass();
            }
        }
コード例 #14
0
        public void Task_2Catch__FirstMatchesAndRethrow_SecondMatches()
        {
            var mock1 = new Mock <IAfter>();
            var mock2 = new Mock <IAfter>();

            var ex1 = new Ex1();
            var ex2 = new Ex2();

            mock1.Setup(then => then.CatchExceptionHandler(ex1)).Throws(ex2);

            Task task = SimpleTaskFactory
                        .Run(() =>
            {
                throw ex1;
            })
                        .Catch <Ex1>(exception => mock1.Object.CatchExceptionHandler(exception))
                        .Catch <Ex2>(exception => mock2.Object.CatchExceptionHandler(exception));

            task.Wait();
            mock1.Verify(then => then.CatchExceptionHandler(ex1), Times.Once);
            mock2.Verify(then => then.CatchExceptionHandler(ex2), Times.Once);
            Assert.Pass();
        }
コード例 #15
0
        public void TaskT1_Then_TaskFuncT1T2__ExceptionInSecond()
        {
            var thrownException = new Exception();

            Task task = SimpleTaskFactory
                        .Run(() => 12)
                        .Then(result => SimpleTaskFactory.Run(() =>
            {
                throw thrownException;
                return(default(string));
            }));

            try
            {
                task.Wait();
                Assert.Fail("task must bubble up the exception");
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(thrownException, e.InnerException);
                Assert.Pass();
            }
        }
コード例 #16
0
        public void Task_Then_FuncT2__SecondCancelled()
        {
            var source = new CancellationTokenSource();
            CancellationToken token = source.Token;

            var syncer = new Syncer();

            var  mock = new Mock <IAfter>();
            Task task = SimpleTaskFactory.Run(() =>
            {
                // do nothing
            }, token)
                        .Then(() => SimpleTaskFactory.Run(() =>
            {
                syncer.Step(1);
                syncer.Step(4);
                token.ThrowIfCancellationRequested();
                return(mock.Object.NextFunction());
            }, token), token);

            syncer.Step(2);
            source.Cancel();
            syncer.Step(3);

            try
            {
                // ReSharper disable once MethodSupportsCancellation
                task.Wait();
                Assert.Fail("task must bubble up the TaskCanceledException");
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf <OperationCanceledException>(e.InnerException);
                mock.Verify(then => then.NextFunction(), Times.Never);
                Assert.Pass();
            }
        }