Example #1
0
        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();
        }
Example #2
0
        public async Task InvokeAsync_TaskFaultsAndFallbackThrows_RethrowsFallbackException()
        {
            var expected = new ExpectedTestException("Expected rethrown exception");
            var command = new FaultingTaskWithEchoThrowingFallbackCommand(expected);

            var e = await Assert.ThrowsAsync<ExpectedTestException>(() => command.InvokeAsync());
            Assert.Equal(expected, e);
        }
        public async Task GenericTask_WithThrowingImplementation_ThrowsCommandFailedExceptionOnAwait()
        {
            var exception = new ExpectedTestException("Expected");
            var instance = new TestThrowingImplementation(exception);
            var proxy = CreateForImplementation(instance);

            var task = proxy.InvokeGenericTask();

            var e = await Assert.ThrowsAsync<CommandFailedException>(() => task);
            Assert.Equal(exception, e.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_WhenReturnedTaskThrowsException_RethrowsException()
        {
            var exception = new ExpectedTestException("Expected");

            var mockBreaker = CreateMockBreaker(true);
            var command = new FaultingTaskEchoCommandWithoutFallback(exception)
            {
                CircuitBreaker = mockBreaker.Object,
            };

            var e = await Assert.ThrowsAsync<CommandFailedException>(() => command.InvokeAsync());
            Assert.True(e.InnerException == exception);
        }
        public async Task InvokeAsync_TimesOutAndFallbackThrows_RethrowsFallbackException()
        {
            var expected = new ExpectedTestException("Expected rethrown exception");
            var command = new TimingOutWithEchoThrowingFallbackCommand(expected);
            try
            {
                await command.InvokeAsync();
            }
            catch (ExpectedTestException e)
            {
                Assert.Equal(expected, e);
                return; // Expected.
            }

            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_WhenExecuteThrowsException_RethrowsException()
        {
            var exception = new ExpectedTestException("Expected");

            var mockBreaker = CreateMockBreaker(true);
            var command = new FaultingExecuteEchoCommandWithoutFallback(exception)
            {
                CircuitBreaker = mockBreaker.Object,
            };

            try
            {
                await command.InvokeAsync();
            }
            catch (CommandFailedException e)
            {
                Assert.True(e.InnerException == exception);
                return; // Expected.
            }
            
            AssertX.FailExpectedException();
        }
Example #9
0
        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();
        }
Example #10
0
        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();
        }
Example #11
0
        public async Task InvokeAsync_CommandExceptionFromExecute_RetainsOriginalExceptionCause()
        {
            var cause = new ExpectedTestException("Root cause exception");
            var command = new FaultingExecuteEchoCommandWithoutFallback(cause);

            var e = await Assert.ThrowsAsync<CommandFailedException>(() => command.InvokeAsync());
            Assert.Equal(cause, e.GetBaseException());
        }
Example #12
0
        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();
        }
Example #13
0
        public async Task InvokeAsync_TaskFaultsAndFallbackThrowsNonInstigator()
        {
            var expected = new ExpectedTestException("foo");
            var mockStats = new Mock<IStats>();
            var command = new FaultingTaskWithEchoThrowingFallbackCommand(expected)
            {
                Stats = mockStats.Object,
            };

            var e = await Assert.ThrowsAsync<ExpectedTestException>(command.InvokeAsync);

            Assert.Equal(expected, e);
            mockStats.Verify(m => m.Elapsed("mjolnir command test.FaultingTaskWithEchoThrowingFallback fallback", "Failure", It.IsAny<TimeSpan>()), Times.Once);
        }
 public TestThrowingImplementation(ExpectedTestException exceptionToThrow)
 {
     _exceptionToThrow = exceptionToThrow;
 }
Example #15
0
 public void WhenUnsuccessful_HasPopulatedException()
 {
     var exception = new ExpectedTestException("expected");
     var result = new CommandResult<object>(new { }, exception);
     Assert.Equal(exception, result.Exception);
 }
Example #16
0
        public async Task InvokeAsync_TaskFaultsAndFallbackNotImplemented()
        {
            var exception = new ExpectedTestException("foo");
            var mockStats = new Mock<IStats>();
            var command = new FaultingTaskEchoCommandWithoutFallback(exception)
            {
                Stats = mockStats.Object,
            };

            var e = await Assert.ThrowsAsync<CommandFailedException>(command.InvokeAsync);

            Assert.Equal(exception, e.GetBaseException());
            mockStats.Verify(m => m.Elapsed("mjolnir command test.FaultingTaskEchoCommandWithoutFallback fallback", "NotImplemented", It.IsAny<TimeSpan>()), Times.Once);
        }
Example #17
0
 internal TimingOutWithEchoThrowingFallbackCommand(ExpectedTestException toRethrow) : base(TimeSpan.FromMilliseconds(100))
 {
     _exception = toRethrow;
 }
 internal FaultingTaskWithEchoThrowingFallbackCommand(ExpectedTestException toRethrow)
 {
     _exception = toRethrow;
 }
Example #19
0
        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();
        }