示例#1
0
        public void SafeContinueWith_DefaultExceptionHandlerSet_HandlesException()
        {
            SpecificException  specificException       = new SpecificException();
            Action <Exception> defaultExceptionHandler = new Mock <Action <Exception> >().Object;

            SafeExecutionHelpers.Initialize();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            //Crux - DefaultHandler returns non-null delegate
            mockHelpers.SetupGet(h => h.DefaultExceptionHandler).Returns(defaultExceptionHandler);

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            var sut = new SafeTask();

            sut.SafeContinueWith <Exception>(
                Task.Factory.StartNew(
                    () => throw specificException,
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    dts),
                null,
                dts);

            dts.RunTasksUntilIdle();

            Assert.Contains(specificException, dts.Exceptions);
            mockHelpers.Verify(h => h.HandleException <Exception>(specificException, null));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
示例#2
0
        public void Execute_SecondCallAfterException_Executes()
        {
            SafeExecutionHelpers.Initialize();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            Exception exception = new Exception();

            int times = 0;

            void MockTask(string text)
            {
                if (times++ == 0)
                {
                    throw exception;                     //Throws only on first try
                }
            }

            ICommand command = new SafeCommand <string>(MockTask);

            //Assert.Throws<Exception>(()=>command.Execute("test"));

            //First run
            command.Execute("test");
            mockHelpers.Verify(h => h.HandleException <Exception>(exception, null));

            //Second run
            command.Execute("test");
            Assert.Equal(2, times);

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
示例#3
0
        public void SafeContinueWith_OnExceptionExceptionSet_HandlesException()
        {
            SpecificException  specificException = new SpecificException();
            Action <Exception> onException       = new Mock <Action <Exception> >().Object;

            SafeExecutionHelpers.Initialize();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false);
            var sut = new SafeTask();

            sut.SafeContinueWith <Exception>(
                Task.Factory.StartNew(
                    () => throw specificException,
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    dts),
                onException, //The crux
                dts);

            dts.RunTasksUntilIdle();

            Assert.Contains(specificException, dts.Exceptions);
            mockHelpers.Verify(h => h.HandleException(specificException, onException));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }
示例#4
0
        public void Execute_TargetThrows_HandlesException()
        {
            SafeExecutionHelpers.Initialize();
            SafeExecutionHelpers.RemoveDefaultExceptionHandler();
            var mockHelpers = new Mock <ISafeExecutionHelpers>();

            SafeExecutionHelpers.SetImplementation(mockHelpers.Object);

            Exception exception = new Exception();

            var command = new SafeCommand(executeAction: () => throw exception);

            //Assert.Throws<Exception>(() => command.Execute(null));
            command.Execute(null);
            mockHelpers.Verify(h => h.HandleException <Exception>(exception, null));

            SafeExecutionHelpers.RevertToDefaultImplementation();
        }