Exemplo n.º 1
0
        public async Task ReceiveAsyncDetectsAndCapturesAStolenPartition()
        {
            var eventHub          = "fake-hub";
            var terminalException = new AmqpException(AmqpErrorCode.Stolen, "This is a terminal exception message.");
            var expectedException = terminalException.TranslateServiceException(eventHub);
            var capturedException = default(Exception);
            var mockConsumer      = new MockAmqpConsumer(eventHub, true, terminalException);

            mockConsumer.MockConnectionScope
            .Setup(scope => scope.OpenConsumerLinkAsync(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <EventPosition>(),
                       It.IsAny <TimeSpan>(),
                       It.IsAny <TimeSpan>(),
                       It.IsAny <uint>(),
                       It.IsAny <long?>(),
                       It.IsAny <long?>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <CancellationToken>()))
            .Throws(terminalException);

            try
            {
                await mockConsumer.ReceiveAsync(10, TimeSpan.FromSeconds(1), CancellationToken.None);
            }
            catch (Exception ex)
            {
                capturedException = ex;
            }

            Assert.That(capturedException, Is.Not.Null, "An exception should have been surfaced.");
            Assert.That(capturedException.GetType(), Is.EqualTo(expectedException.GetType()), "The captured exception was not of the expected type.");
            Assert.That(capturedException.Message, Is.EqualTo(expectedException.Message), "The mocked terminal exception should have been surfaced.");

            var preservedException = GetActivePartitionStolenException(mockConsumer);

            Assert.That(preservedException, Is.SameAs(terminalException), "The preserved exception should match the terminal exception.");
        }
Exemplo n.º 2
0
        public async Task ReceiveAsyncSurfacesStolenPartitionWhenInvalidated()
        {
            var eventHub          = "fake-hub";
            var terminalException = new AmqpException(AmqpErrorCode.Stolen, "This is a terminal exception message.");
            var expectedException = terminalException.TranslateServiceException(eventHub);
            var capturedException = default(Exception);
            var mockConsumer      = new MockAmqpConsumer(eventHub, true, terminalException);

            SetActivePartitionStolenException(mockConsumer, terminalException);

            try
            {
                await mockConsumer.ReceiveAsync(10, TimeSpan.FromSeconds(1), CancellationToken.None);
            }
            catch (Exception ex)
            {
                capturedException = ex;
            }

            Assert.That(capturedException, Is.Not.Null, "An exception should have been surfaced.");
            Assert.That(capturedException.GetType(), Is.EqualTo(expectedException.GetType()), "The captured exception was not of the expected type.");
            Assert.That(capturedException.Message, Is.EqualTo(expectedException.Message), "The mocked terminal exception should have been surfaced.");

            // Because the terminal exception was injected, it should not attempt to open the actual link.

            mockConsumer.MockConnectionScope
            .Verify(scope => scope.OpenConsumerLinkAsync(
                        It.IsAny <string>(),
                        It.IsAny <string>(),
                        It.IsAny <EventPosition>(),
                        It.IsAny <TimeSpan>(),
                        It.IsAny <TimeSpan>(),
                        It.IsAny <uint>(),
                        It.IsAny <long?>(),
                        It.IsAny <long?>(),
                        It.IsAny <bool>(),
                        It.IsAny <string>(),
                        It.IsAny <CancellationToken>()),
                    Times.Never);
        }