Esempio n. 1
0
        public async Task Test_that_filter_can_interrupt_invocation_chain_with_state()
        {
            // Arrange

            var          messageId     = Guid.NewGuid();
            const string state         = "state";
            const string correlationId = "correlationId";
            const string exchange      = "exchange";
            const string routingKey    = "routingKey";
            const int    retryCount    = 123;

            var options = new MessageSubscriptionOptions <TestMessage, string>(routingKey);

            options.WithHandler <TestMessageHandlerWithState>();
            options.WithState(state);

            var filterMock = new Mock <IMessageFilter>();

            filterMock
            .Setup(x => x.HandleMessageAsync(It.IsNotNull <MessageFilteringContext>()))
            .Returns <MessageFilteringContext>(context => Task.FromResult(MessageHandlingResult.TransientFailure()));

            var globalFilters = new List <IMessageFilter>
            {
                filterMock.Object
            };

            var subscription = new MessageSubscription <TestMessage, string>(options, globalFilters);

            // Action

            var result = await subscription.InvokeHandlerAsync
                         (
                _serviceProvider,
                new TestMessage
            {
                Id = messageId
            },
                new MessageHeaders(correlationId),
                new MessageHandlingContext(exchange, retryCount, routingKey),
                _messagePublisherMock.Object
                         );

            // Assert

            Assert.AreEqual(MessageHandlingResult.TransientFailure(), result);

            _testHandlerWithStateImplMock.Verify
            (
                x => x.HandleAsync
                (
                    It.IsAny <string>(),
                    It.IsAny <TestMessage>(),
                    It.IsAny <MessageHeaders>(),
                    It.IsAny <IMessagePublisher>()
                ),
                times: Times.Never
            );
            filterMock.Verify(x => x.HandleMessageAsync(It.IsNotNull <MessageFilteringContext>()), Times.Once());
        }
Esempio n. 2
0
        public async Task Test_that_handler_with_state_invoked_without_filters()
        {
            // Arrange

            var          messageId     = Guid.NewGuid();
            const string state         = "state";
            const string correlationId = "correlationId";
            const string exchange      = "exchange";
            const string routingKey    = "routingKey";
            const int    retryCount    = 123;

            var options = new MessageSubscriptionOptions <TestMessage, string>(routingKey);

            options.WithHandler <TestMessageHandlerWithState>();
            options.WithState(state);

            var subscription = new MessageSubscription <TestMessage, string>(options, Array.Empty <IMessageFilter>());

            _testHandlerWithStateImplMock
            .Setup(x => x.HandleAsync
                   (
                       It.IsAny <string>(),
                       It.IsAny <TestMessage>(),
                       It.IsAny <MessageHeaders>(),
                       It.IsAny <IMessagePublisher>()
                   ))
            .Returns <string, TestMessage, MessageHeaders, IMessagePublisher>((s, m, h, p) => Task.FromResult(MessageHandlingResult.Success()));

            // Action

            var result = await subscription.InvokeHandlerAsync
                         (
                _serviceProvider,
                new TestMessage
            {
                Id = messageId
            },
                new MessageHeaders(correlationId),
                new MessageHandlingContext(exchange, retryCount, routingKey),
                _messagePublisherMock.Object
                         );

            // Assert

            Assert.AreEqual(MessageHandlingResult.Success(), result);

            _testHandlerWithStateImplMock.Verify
            (
                x => x.HandleAsync
                (
                    It.Is <string>(s => s == state),
                    It.Is <TestMessage>(m => m.Id == messageId),
                    It.IsNotNull <MessageHeaders>(),
                    It.IsNotNull <IMessagePublisher>()
                ),
                times: Times.Once
            );
        }
Esempio n. 3
0
        public void Test_that_exception_propagated_through_the_filters_with_state()
        {
            // Arrange

            var          messageId     = Guid.NewGuid();
            const string state         = "state";
            const string correlationId = "correlationId";
            const string exchange      = "exchange";
            const string routingKey    = "routingKey";
            const int    retryCount    = 123;

            var options = new MessageSubscriptionOptions <TestMessage, string>(routingKey);

            options.WithHandler <TestMessageHandlerWithState>();
            options.WithState(state);

            var       filterMock      = new Mock <IMessageFilter>();
            Exception caughtException = null;

            filterMock
            .Setup(x => x.HandleMessageAsync(It.IsNotNull <MessageFilteringContext>()))
            .Returns <MessageFilteringContext>(async context =>
            {
                try
                {
                    return(await context.InvokeNextAsync());
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                    throw;
                }
            });

            _testHandlerWithStateImplMock
            .Setup(x => x.HandleAsync
                   (
                       It.IsAny <string>(),
                       It.IsAny <TestMessage>(),
                       It.IsAny <MessageHeaders>(),
                       It.IsAny <IMessagePublisher>()
                   ))
            .Returns <string, TestMessage, MessageHeaders, IMessagePublisher>((s, m, h, p) => throw new TestException());

            var globalFilters = new List <IMessageFilter>
            {
                filterMock.Object
            };

            var subscription = new MessageSubscription <TestMessage, string>(options, globalFilters);

            // Action

            Assert.ThrowsAsync <TestException>(async() =>
            {
                await subscription.InvokeHandlerAsync
                (
                    _serviceProvider,
                    new TestMessage
                {
                    Id = messageId
                },
                    new MessageHeaders(correlationId),
                    new MessageHandlingContext(exchange, retryCount, routingKey),
                    _messagePublisherMock.Object
                );
            });

            // Assert

            Assert.IsInstanceOf <TestException>(caughtException);

            _testHandlerWithStateImplMock.Verify
            (
                x => x.HandleAsync
                (
                    It.IsAny <string>(),
                    It.IsAny <TestMessage>(),
                    It.IsAny <MessageHeaders>(),
                    It.IsAny <IMessagePublisher>()
                ),
                times: Times.Once
            );
            filterMock.Verify(x => x.HandleMessageAsync(It.IsNotNull <MessageFilteringContext>()), Times.Once());
        }