public async Task InvokeAsync_WhenFallbackFails_ReleasesSemaphore() { var mockSemaphore = new Mock <IIsolationSemaphore>(); mockSemaphore.Setup(m => m.TryEnter()).Returns(true); var exception = new ExpectedTestException("Expected"); var command = new FaultingTaskWithEchoThrowingFallbackCommand(exception) { FallbackSemaphore = mockSemaphore.Object, }; try { await command.InvokeAsync(); } catch (ExpectedTestException e) { Assert.Equal(exception, e); mockSemaphore.Verify(m => m.Release(), Times.Once); return; // Expected. } AssertX.FailExpectedException(); }
public async Task InvokeAsync_TaskFaultsAndFallbackNotImplemented() { var exception = new ExpectedTestException("foo"); var mockStats = new Mock <IStats>(); var command = new FaultingTaskEchoCommandWithoutFallback(exception) { Stats = mockStats.Object, }; try { await command.InvokeAsync(); } catch (CommandFailedException e) { if (e.GetBaseException() != exception) { throw; } mockStats.Verify(m => m.Elapsed("mjolnir command test.FaultingTaskEchoCommandWithoutFallback fallback", "NotImplemented", It.IsAny <TimeSpan>()), Times.Once); return; // Expected. } AssertX.FailExpectedException(); }
public void WhenUnsuccessful_HasPopulatedException() { var exception = new ExpectedTestException("expected"); var result = new CommandResult <object>(new { }, exception); Assert.Equal(exception, result.Exception); }
public async Task InvokeAsync_TaskFaultsAndFallbackThrowsNonInstigator() { var expected = new ExpectedTestException("foo"); var mockStats = new Mock <IStats>(); var command = new FaultingTaskWithEchoThrowingFallbackCommand(expected) { Stats = mockStats.Object, }; try { await command.InvokeAsync(); } catch (ExpectedTestException e) { if (e != expected) { throw; } mockStats.Verify(m => m.Elapsed("mjolnir command test.FaultingTaskWithEchoThrowingFallback fallback", "Failure", It.IsAny <TimeSpan>()), Times.Once); return; // Expected. } AssertX.FailExpectedException(); }
public void Get_UnderlyingGetResultThrowsException_RethrowsInnerException() { // To avoid leaking STP's WorkItemResultException out beyond our thread pooling layer, // our wrappers around STP should unwrap the exception thrown by the pool and just // rethrow its inner exception, which should be what got thrown from ExecuteAsync(). var inner = new ExpectedTestException("Root cause inner"); var cause = new ExpectedTestException("Test root cause", inner); var itemException = new WorkItemResultException("Work item result exception", cause); var timeout = TimeSpan.FromSeconds(1); var mockWorkItemResult = new Mock <IWorkItemResult <object> >(); mockWorkItemResult.Setup(m => m.GetResult(timeout, false)).Throws(itemException); var stpWorkItem = new StpWorkItem <object>(mockWorkItemResult.Object); try { stpWorkItem.Get(new CancellationToken(), timeout); } catch (ExpectedTestException e) { Assert.Equal(cause, e); Assert.Equal(inner, e.InnerException); return; // Expected. } AssertX.FailExpectedException(); }
public void Invoke_FaultingTask_PropagatesException() { var expected = new ExpectedTestException("Exception"); var command = new FaultingTaskEchoCommandWithoutFallback(expected); var result = Assert.Throws <CommandFailedException>(() => { command.Invoke(); }); Assert.Equal(expected, result.InnerException); }
public void InvokeAsync_WhenExceptionThrownFromExecute_HasExpectedExceptionInsideAggregateException() { var expected = new ExpectedTestException("Exception"); var command = new FaultingExecuteEchoCommandWithoutFallback(expected); var result = Assert.Throws <AggregateException>(() => { var foo = command.InvokeAsync().Result; }); // AggregateException -> CommandFailedException -> ExpectedTestException Assert.Equal(expected, result.InnerException.InnerException); }
public void String_WithThrowingImplementation_ThrowsCommandFailedException() { var exception = new ExpectedTestException("Expected"); var instance = new TestThrowingImplementation(exception); var proxy = CreateForImplementation(instance); var ex = Assert.Throws <CommandFailedException>(() => { proxy.InvokeString(); }); Assert.Equal(exception, ex.InnerException); }
public async Task InvokeAsync_TaskFaultsAndFallbackThrows_RethrowsFallbackException() { var expected = new ExpectedTestException("Expected rethrown exception"); var command = new FaultingTaskWithEchoThrowingFallbackCommand(expected); try { await command.InvokeAsync(); } catch (ExpectedTestException e) { Assert.Equal(expected, e); return; } AssertX.FailExpectedException(); }
public async Task InvokeAsync_CommandExceptionFromTask_RetainsOriginalExceptionCause() { var cause = new ExpectedTestException("Root cause exception"); var command = new FaultingTaskEchoCommandWithoutFallback(cause); try { await command.InvokeAsync(); } catch (CommandFailedException e) { Assert.Equal(cause, e.GetBaseException()); return; } AssertX.FailExpectedException(); }
public async Task GenericTask_WithThrowingImplementation_ThrowsCommandFailedExceptionOnAwait() { var exception = new ExpectedTestException("Expected"); var instance = new TestThrowingImplementation(exception); var proxy = CreateForImplementation(instance); var task = proxy.InvokeGenericTask(); try { // Using Assert.Throws(async () => {}) doesn't work right here, // so falling back to the ol' try/catch. await task; } catch (CommandFailedException e) { Assert.Equal(exception, e.InnerException); return; } AssertX.FailExpectedException(); }
public async Task InvokeAsync_WhenReturnedTaskThrowsException_RethrowsException() { var exception = new ExpectedTestException("Expected"); var mockBreaker = CreateMockBreaker(true); var command = new FaultingTaskEchoCommandWithoutFallback(exception) { CircuitBreaker = mockBreaker.Object, }; try { await command.InvokeAsync(); } catch (CommandFailedException e) { Assert.True(e.InnerException == exception); return; // Expected. } AssertX.FailExpectedException(); }
public TestThrowingImplementation(ExpectedTestException exceptionToThrow) { _exceptionToThrow = exceptionToThrow; }
internal TimingOutWithEchoThrowingFallbackCommand(ExpectedTestException toRethrow) : base(TimeSpan.FromMilliseconds(100)) { _exception = toRethrow; }
internal FaultingTaskWithEchoThrowingFallbackCommand(ExpectedTestException toRethrow) { _exception = toRethrow; }