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(); }
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(); }
public void Execute_SecondCallAfterException_Executes() { SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = 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(); }
public void SafeContinueWith_NothingSet_HandleExceptionRuns() { SpecificException specificException = new SpecificException(); SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = 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(); }
public void HandleException_SpecificExceptionThrown_NoHandlers_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); //MockForms.Init(); //For Device.BeginInvokeOnMainThread var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); Assert.Throws <SafeExecutionHelpersException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); }
public void HandleException_SpecificException_TargetThrowsDifferentException_NotHandledByGivenDelegate() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.SetDefaultExceptionHandler((ex) => { }); bool isHandled = false; SafeExecutionHelpers.HandleException <SpecificException>(new DifferentException(), (ex) => isHandled = true); Assert.False(isHandled); }
public void HandleException_SpecificExceptionThrown_NoHandlers_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); Assert.Throws <SafeExecutionHelpersException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); }
public void HandleException_Exception_SpecificExceptionOnException_NotHandled() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.SetDefaultExceptionHandler((ex) => { }); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); Assert.Empty(handler.Invocations); }
public void HandleException_SpecificException_SpecificExceptionOnException_Handled() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new SpecificException(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_ShouldRethrow_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); //MockForms.Init(); //For Device.BeginInvokeOnMainThread SafeExecutionHelpers.Configure(s => s.ShouldAlwaysRethrowException = true); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new NullReferenceException(); var handler = new Mock <Action <Exception> >(); Assert.Throws <NullReferenceException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_DefaultHandlerSet_Exception_ExceptionOnException_DefaultNotInvoked() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var defaultHandler = new Mock <Action <Exception> >(); SafeExecutionHelpers.SetDefaultExceptionHandler(defaultHandler.Object); var exception = new Exception(); SafeExecutionHelpers.HandleException(exception, new Mock <Action <Exception> >().Object); Assert.Empty(defaultHandler.Invocations); }
public void Execute_TargetThrows_HandlesException() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = 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(); }
private void AfterEachTest() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.Configure(s => s.ShouldAlwaysRethrowException = false); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); }