Example #1
0
        public async Task NotifyFault()
        {
            var expectedException = new Exception("Unhandled exception");

            var args = new BasicDeliverEventArgs
            {
                DeliveryTag     = 1234L,
                BasicProperties = new BasicProperties()
            };

            var realConsumer = new Mock <Consumer <FakeConsumedMessage> >();
            var message      = new FakeConsumedMessage(args, _ => new Success(_, new ConsumedMessage.ConsumingResult[] { new ConsumingResultStub(_, realConsumer.Object) }));
            var builder      = new Mock <IConsumedMessageBuilder>();

            builder.Setup(_ => _.Build(It.IsAny <BasicDeliverEventArgs>())).Returns(message);

            var configuration = new ConsumingConfiguration(new Mock <IBroker>().Object, default(Queue));

            configuration.Consumes(realConsumer.Object);

            var failingInboundChannel = new FailingInboundChannel(expectedException);

            var consumer = new AtLeastOnceConsumerWrapper(failingInboundChannel, new Mock <IOutboundChannel>().Object, default(Queue), builder.Object, configuration);
            await consumer.CallConsumeInternal(args);

            realConsumer.Verify(_ => _.OnError(expectedException));
        }
Example #2
0
        public void HeaderMapping()
        {
            var          content           = new Foo();
            const String messageId         = "one-id";
            const Int64  timestamp         = 123456789L;
            const String replyExchangeType = "direct";
            const String replyExchangeName = "exchange-name";
            const String replyRoutingKey   = "routing-key";
            const String correlationId     = "one-correlation-id";
            var          replyTo           = $"{replyExchangeType}://{replyExchangeName}/{replyRoutingKey}";

            var args = new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties
                {
                    MessageId     = messageId,
                    Timestamp     = new AmqpTimestamp(timestamp),
                    CorrelationId = correlationId,
                    ReplyTo       = replyTo
                }
            };
            var message = new FakeConsumedMessage(content, args);
            var actual  = message.To <Foo>();

            Assert.Equal(messageId, actual.Headers.MessageId);
            Assert.Equal(timestamp, actual.Headers.Timestamp);
            Assert.Equal(correlationId, actual.Headers.CorrelationId);
            Assert.Equal(replyTo, actual.Headers.ReplyConfiguration.ToString());
        }
Example #3
0
        public void OnReiteratedError()
        {
            var inboundChannel = new Mock <IInboundChannel>();
            var strategy       = new Mock <IFallbackStrategy>();
            var args           = FakeBasicDeliverEventArgs();

            args.Redelivered = true;
            var message = new FakeConsumedMessage(new Object(), args);

            _configuration.FallbackBy((c, q) => strategy.Object);
            _configuration.Consumes(new FakeConsumer(consumedMessage => { throw new Exception(); }));
            var builder = new Mock <IConsumedMessageBuilder>();

            builder.Setup(_ => _.Build(args)).Returns(message);
            var outboundChannel = new Mock <IOutboundChannel>().Object;
            var consumer        = new AtLeastOnceConsumerWrapper(inboundChannel.Object,
                                                                 outboundChannel,
                                                                 default(Queue),
                                                                 builder.Object,
                                                                 _configuration);
            var result = consumer.CallConsumeInternalAsync(args).Result;

            Assert.IsType <ReiteratedConsumingFailure>(result);
            strategy.Verify(_ => _.Apply(outboundChannel, message), Times.Once);
        }
Example #4
0
        public void WrongContentType()
        {
            var content = new Foo();
            var message = new FakeConsumedMessage(content, FakeBasicDeliverEventArgs());

            Assert.Throws <InvalidCastException>(() => message.To <Bar>());
        }
Example #5
0
        public void NoSubscriptions()
        {
            var args          = FakeBasicDeliverEventArgs();
            var message       = new FakeConsumedMessage(new Foo(), args);
            var subscriptions = _configuration.FindSubscriptions(message);

            Assert.Empty(subscriptions);
        }
Example #6
0
        public void ProperContentType()
        {
            var content = new Foo();
            var message = new FakeConsumedMessage(content, FakeBasicDeliverEventArgs());
            var actual  = message.To <Foo>();

            Assert.Equal(content, actual.Content);
        }
Example #7
0
        public void OneSubscription()
        {
            _configuration.Consumes(new FakeConsumer(_ => null));
            var args          = FakeBasicDeliverEventArgs();
            var message       = new FakeConsumedMessage(new Foo(), args);
            var subscriptions = _configuration.FindSubscriptions(message);

            Assert.Single(subscriptions);
        }
Example #8
0
        public void TwoSubscriptionsForTheSameMessage()
        {
            _configuration.Consumes(new FakeConsumer(_ => null));
            _configuration.Consumes(new FakeConsumer(_ => null));
            var args          = FakeBasicDeliverEventArgs();
            var message       = new FakeConsumedMessage(new Foo(), args);
            var subscriptions = _configuration.FindSubscriptions(message);

            Assert.Equal(2, subscriptions.Count());
        }
Example #9
0
        public void ConsumerTag()
        {
            var          content     = new Foo();
            const String consumerTag = "one-tag";
            var          args        = new BasicDeliverEventArgs
            {
                ConsumerTag     = consumerTag,
                BasicProperties = new BasicProperties()
            };
            var message = new FakeConsumedMessage(content, args);
            var actual  = message.To <Foo>();

            Assert.Equal(consumerTag, actual.ConsumerTag);
        }
Example #10
0
        public void CustomHeader()
        {
            var content = new Foo();
            var args    = new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties
                {
                    Headers = new Dictionary <String, Object>
                    {
                        { "a", "b" }
                    }
                }
            };
            var message = new FakeConsumedMessage(content, args);
            var actual  = message.To <Foo>();

            Assert.Equal("b", actual.Headers["a"]);
        }
Example #11
0
        public void HeaderMapping()
        {
            var          content   = new Foo();
            const String messageId = "one-id";
            const Int64  timestamp = 123456789L;
            var          args      = new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties
                {
                    MessageId = messageId,
                    Timestamp = new AmqpTimestamp(timestamp)
                }
            };
            var message = new FakeConsumedMessage(content, args);
            var actual  = message.As <Foo>();

            Assert.Equal(messageId, actual.Headers.MessageId);
            Assert.Equal(timestamp, actual.Headers.Timestamp);
        }
        public void DeadLetterExchangeStrategy()
        {
            var args                = FakeBasicDeliverEventArgs();
            var message             = new FakeConsumedMessage(null, args);
            var broker              = new Mock <IBroker>();
            var queue               = new Queue("queue_name");
            Func <String, String> f = _ => $"{_}-DeadLetter";
            var dleName             = f(queue.Name);

            broker.Setup(_ => _.DeclareDurableDirectExchange(dleName)).Returns(new Exchange(dleName, "direct"));
            var strategy = DeadLetterStrategy.New(broker.Object,
                                                  queue,
                                                  _ => $"{_}-DeadLetter");
            var outboundChannel = new Mock <IOutboundChannel>();

            strategy.Apply(outboundChannel.Object, message);
            outboundChannel.Verify(_ => _.ForwardAsync(message,
                                                       It.Is <Exchange>(__ => __.Name == dleName),
                                                       String.Empty),
                                   Times.Once);
        }
Example #13
0
        public void OnSuccess()
        {
            var inboundChannel = new Mock <IInboundChannel>();
            var args           = FakeBasicDeliverEventArgs();
            var message        = new FakeConsumedMessage(new Object(), args);
            var strategy       = new Mock <IFallbackStrategy>();

            _configuration.FallbackBy((c, q) => strategy.Object);
            var builder = new Mock <IConsumedMessageBuilder>();

            builder.Setup(_ => _.Build(args)).Returns(message);
            var outboundChannel = new Mock <IOutboundChannel>().Object;
            var consumer        = new AtLeastOnceConsumer(inboundChannel.Object,
                                                          outboundChannel,
                                                          default(Queue),
                                                          builder.Object,
                                                          _configuration);
            var result = consumer.ConsumeAsync(args).Result;

            Assert.IsType <Success>(result);
            strategy.Verify(_ => _.Apply(outboundChannel, message), Times.Never);
        }
Example #14
0
        public void CustomHeader()
        {
            var key     = "a";
            var value   = "b";
            var content = new Foo();
            var args    = new BasicDeliverEventArgs
            {
                BasicProperties = new BasicProperties
                {
                    Headers = new Dictionary <String, Object>
                    {
                        { key, value }
                    }
                }
            };
            var message = new FakeConsumedMessage(content, args);

            var actual = message.To <Foo>();

            Assert.True(actual.ContainsHeader(key));
            Assert.Equal(value, actual.Headers[key]);
        }
Example #15
0
        private static Mock <IInboundChannel> BuildInboundChannel(UInt64 deliveryTag,
                                                                  Func <ConsumedMessageBase, AggregateConsumingResult> func,
                                                                  ConsumingConfiguration configuration)
        {
            var args = new BasicDeliverEventArgs
            {
                DeliveryTag     = deliveryTag,
                BasicProperties = new BasicProperties()
            };
            var builder = new Mock <IConsumedMessageBuilder>();
            var message = new FakeConsumedMessage(args, func);

            builder.Setup(_ => _.Build(args)).Returns(message);
            var channel  = new Mock <IInboundChannel>();
            var consumer = new AtLeastOnceConsumerWrapper(channel.Object,
                                                          new Mock <IOutboundChannel>().Object,
                                                          default(Queue),
                                                          builder.Object,
                                                          configuration);

            consumer.CallConsumeInternal(args).Wait();
            return(channel);
        }