public void Send_EmptyMessage_PublishCalled()
        {
            var busFactory = Given.AdvancedBusFactory;
            var config     = new RabbitMqQueueConfiguration {
                Id = "routingKey"
            };
            var adapter = Given.RabbitMqQueueMessageAdapter
                          .WithConfiguration(config)
                          .WithBusFactory(busFactory)
                          .Please();
            var message = new TextMessage {
                Body = "test"
            };
            var bodyBytes = message.GetMessageBodyAsBytes();

            adapter.Send(message);

            busFactory.AdvancedBusMock.Verify(
                x => x.Publish(
                    null,
                    config.Id,
                    false,
                    It.IsAny <MessageProperties>(),
                    It.Is <byte[]>(a => a.Where((b, i) => b == bodyBytes[i]).Count() == bodyBytes.Length)));
        }
        public void ConvertToProperties_UnknownMessage_Throws()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.Zero
            };
            var message = new TestMessage();

            Assert.Throws <ArgumentException>(() => message.ConvertToProperties(queueConfiguration));
        }
        public void ConvertToProperties_ConfigurationWithoutLifeTime_DefaultExpirationReturned()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId
            };
            var message = new TextMessage();

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(properties.Expiration, Is.EqualTo(Expiration));
        }
        public void ConvertToProperties_NoMessageId_MessageIdFilledWithNewGuid()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.Zero
            };
            var message = new TextMessage();

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(Guid.TryParse(properties.MessageId, out _));
        }
        public void ConvertToProperties_BytesMessage_TypeIsText()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.Zero
            };
            var message = new BytesMessage();

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(properties.ContentType, Is.EqualTo(BytesContentType));
        }
        public void ConvertToProperties_ConfigurationWithLifeTime_ExpirationFromConfigurationReturned()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.FromMilliseconds(100)
            };
            var message = new TextMessage();

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(properties.Expiration, Is.EqualTo("100"));
        }
        public void GetConfiguration_HasTwoQueueWithSameId_Exception()
        {
            var configuration      = new RabbitMqConfiguration();
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = "id"
            };

            configuration.Queues.Add(queueConfiguration);
            configuration.Queues.Add(queueConfiguration);

            Assert.Throws <MessagingConfigurationException>(() => configuration.GetQueueConfiguration("id"));
        }
        public void GetConfiguration_HasOneQueueWithId_ReturnsQueueConfiguration()
        {
            var configuration      = new RabbitMqConfiguration();
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = "id"
            };

            configuration.Queues.Add(queueConfiguration);

            var result = configuration.GetQueueConfiguration("id");

            Assert.That(result == queueConfiguration);
        }
        public void ConvertToProperties_TextMessage_TypeIsCorrect()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.Zero
            };
            const string contentType = "application/xml";
            var          message     = new TextMessage {
                ContentType = contentType
            };

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(properties.ContentType, Is.EqualTo(contentType));
        }
Beispiel #10
0
        public void ConvertToProperties_MessageWithLifetime_MessageLifetimeReturned()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId
            };
            var message = new TextMessage()
            {
                LifeTime = TimeSpan.FromMilliseconds(100)
            };

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.That(properties.Expiration == "100");
        }
Beispiel #11
0
        public static void SubscribeToExchange <TEvent>(this IActor actor,
                                                        string exchange,
                                                        Expression <Func <TEvent, bool> >?routingStrategy = null,
                                                        string queueName                        = "",
                                                        string exchangeType                     = "topic",
                                                        bool isExchangeDurable                  = true,
                                                        bool isExchangeAutoDelete               = true,
                                                        bool createExchangeIfNotExist           = true,
                                                        bool createDeadLetterExchangeIfNotExist = true,
                                                        bool isQueueDurable                     = false,
                                                        bool isQueueAutoAck                     = false,
                                                        bool isQueueAutoDelete                  = true,
                                                        bool isQueueExclusive                   = true)
            where TEvent : class, IRabbitMqEvent
        {
            var rabbitMqBus = actor.GetConnectedBus <IRabbitMqBus>();

            var rabbitMqQueueConfiguration = new RabbitMqQueueConfiguration <TEvent>(routingStrategy,
                                                                                     queueName,
                                                                                     isQueueAutoAck,
                                                                                     isQueueDurable,
                                                                                     isQueueAutoDelete,
                                                                                     isQueueExclusive);

            var rabbitMqExchangeConfiguration = new RabbitMqExchangeConfiguration(exchange,
                                                                                  exchangeType,
                                                                                  createExchangeIfNotExist,
                                                                                  createDeadLetterExchangeIfNotExist,
                                                                                  isExchangeAutoDelete,
                                                                                  isExchangeDurable);

            var onMessage = new Func <IRabbitMqQueueMessage, Task>((@event) =>
            {
                actor.OnMessageReceived(@event);

                return(Task.CompletedTask);
            });

            var rabbitMqSubscription = new RabbitMqEventSubscription <TEvent>(onMessage,
                                                                              rabbitMqQueueConfiguration,
                                                                              rabbitMqExchangeConfiguration);

            rabbitMqBus.SubscribeToExchange(rabbitMqSubscription);

            var disposable = Disposable.Create(() => rabbitMqBus.UnsubscribeFromExchange(rabbitMqSubscription));

            actor.AddDisposable(disposable);
        }
        public void Connect_VariousExchangeAndQueueNames_InitializeBindsExpectedCalled(string exchange, string queue, bool isBindCalled)
        {
            var busFactory = Given.AdvancedBusFactory;
            var config     = new RabbitMqQueueConfiguration {
                ExchangeName = exchange, QueueName = queue
            };

            var adapter = Given.RabbitMqQueueMessageAdapter
                          .WithConfiguration(config)
                          .WithBusFactory(busFactory)
                          .Please();

            adapter.Connect();

            var times = isBindCalled ? Times.AtLeastOnce() : Times.Never();

            busFactory.AdvancedBusMock.Verify(
                x => x.Bind(It.IsAny <IExchange>(), It.IsAny <IQueue>(), It.IsAny <string>()), times);
        }
Beispiel #13
0
        public static IObservable <TEvent> SubscribeToExchange <TEvent>(this IRabbitMqBus rabbitMqBus,
                                                                        string exchange,
                                                                        string queueName          = "",
                                                                        string exchangeType       = "topic",
                                                                        bool isExchangeDurable    = true,
                                                                        bool isExchangeAutoDelete = true,
                                                                        bool isQueueDurable       = false,
                                                                        bool isQueueAutoAck       = false,
                                                                        bool isQueueAutoDelete    = true,
                                                                        bool isQueueExclusive     = true,
                                                                        Expression <Func <TEvent, bool> >?routingStrategy = null)
            where TEvent : class, IRabbitMqEvent
        {
            var rabbitMqQueueConfiguration = new RabbitMqQueueConfiguration <TEvent>(routingStrategy,
                                                                                     queueName,
                                                                                     isQueueAutoAck,
                                                                                     isQueueDurable,
                                                                                     isQueueAutoDelete,
                                                                                     isQueueExclusive);

            var rabbitMqExchangeConfiguration = new RabbitMqExchangeConfiguration(exchange,
                                                                                  exchangeType,
                                                                                  true,
                                                                                  true,
                                                                                  isExchangeAutoDelete,
                                                                                  isExchangeDurable);

            var observable = Observable.Create <TEvent>((observer) =>
            {
                var rabbitMqSubscription = new RabbitMqEventSubscription <TEvent>((@event) =>
                {
                    observer.OnNext((TEvent)@event.Content);

                    return(Task.CompletedTask);
                }, rabbitMqQueueConfiguration, rabbitMqExchangeConfiguration);

                rabbitMqBus.SubscribeToExchange(rabbitMqSubscription);

                return(Disposable.Create(() => rabbitMqBus.UnsubscribeFromExchange(rabbitMqSubscription)));
            });

            return(observable);
        }
        public void Connect_ExchangeAndQueueNamesIsNotEmptyListRoutingsIsNotNull_CreateRoutingsBinds()
        {
            var busFactory = Given.AdvancedBusFactory;
            var routings   = new [] { "1", "2" };
            var config     = new RabbitMqQueueConfiguration {
                ExchangeName = "exchange", QueueName = "queue", Routings = routings
            };

            var adapter = Given.RabbitMqQueueMessageAdapter
                          .WithConfiguration(config)
                          .WithBusFactory(busFactory)
                          .Please();

            adapter.Connect();

            busFactory.AdvancedBusMock.Verify(
                x => x.Bind(It.IsAny <IExchange>(), It.IsAny <IQueue>(), It.Is <string>(key => key == routings[0])), Times.AtLeastOnce);
            busFactory.AdvancedBusMock.Verify(
                x => x.Bind(It.IsAny <IExchange>(), It.IsAny <IQueue>(), It.Is <string>(key => key == routings[1])), Times.AtLeastOnce);
        }
        public void ConvertToProperties_CorrectMessage_CorrectResult()
        {
            var queueConfiguration = new RabbitMqQueueConfiguration {
                Id = QueueId, Lifetime = TimeSpan.Zero
            };
            var message = new TextMessage {
                MessageId = MessageId, CorrelationId = CorrelationId, ReplyQueue = ReplyQueue
            };

            message.Properties.Add("a", 1);

            var properties = message.ConvertToProperties(queueConfiguration);

            Assert.Multiple(() =>
            {
                Assert.That(properties.MessageId, Is.EqualTo(MessageId));
                Assert.That(properties.CorrelationId, Is.EqualTo(CorrelationId));
                Assert.That(properties.Headers, Is.EqualTo(message.Properties));
                Assert.That(properties.DeliveryMode, Is.EqualTo(2));
                Assert.That(properties.ReplyTo, Is.EqualTo(ReplyQueue));
                Assert.That(properties.Expiration, Is.EqualTo(((int)queueConfiguration.Lifetime.Value.TotalMilliseconds).ToString()));
            });
        }
 public RabbitMqQueueMessageAdapterBuilder WithConfiguration(RabbitMqQueueConfiguration configuration)
 {
     _configuration = configuration;
     return(this);
 }