Exemplo 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());
        }