Exemplo n.º 1
0
        public void SubscribeToEvent_handles_multiple_events([Frozen] IMessageDescriptorStore messageDescriptorStore, RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            sut.SubscribeToEvent <SecondTestEvent>();

            Assert.That(messageDescriptorStore.Events, Contains.Item(typeof(FirstTestEvent)));

            Assert.That(messageDescriptorStore.Events, Contains.Item(typeof(SecondTestEvent)));
        }
Exemplo n.º 2
0
        public async Task NotifySuccess_acks_event_messages([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestEvent @event)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync().ConfigureAwait(false);

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]     = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]   = encoding.GetBytes(DescriptorName(@event.GetType())),
                    ["Nybus:CorrelationId"] = correlationId.ToByteArray()
                }
            };

            var body = configuration.Serializer.SerializeObject(@event, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            await sut.NotifySuccessAsync(incomingMessages.First());

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Verify(p => p.BasicAck(deliveryTag, It.IsAny <bool>()));
        }
Exemplo n.º 3
0
        public async Task Events_with_invalid_type_format_are_ignored_and_nacked([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestEvent @event)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]     = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]   = encoding.GetBytes(@event.GetType().FullName),
                    ["Nybus:CorrelationId"] = correlationId.ToByteArray()
                }
            };

            var body = configuration.Serializer.SerializeObject(@event, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Assert.That(incomingMessages, Is.Empty);

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Verify(p => p.BasicNack(deliveryTag, It.IsAny <bool>(), It.IsAny <bool>()));
        }
Exemplo n.º 4
0
        public async Task Queue_is_bound_to_exchange_when_a_event_is_registered([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Verify(p => p.QueueBind(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IDictionary <string, object> >()));
        }
Exemplo n.º 5
0
        public async Task Exchange_is_declared_when_a_event_is_registered([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            Mock.Get(configuration.EventExchangeManager).Verify(p => p.EnsureExchangeExists(It.IsAny <IModel>(), It.IsAny <string>(), It.IsAny <string>()));
        }
Exemplo n.º 6
0
        public async Task QueueFactory_is_invoked_when_a_event_is_registered([Frozen] IMessageDescriptorStore messageDescriptorStore, [Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            Mock.Get(configuration.EventQueueFactory).Verify(p => p.CreateQueue(It.IsAny <IModel>()));
        }
Exemplo n.º 7
0
        public async Task Events_can_be_subscribed(RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            var incomingMessages = sequence.DumpInList();

            Assert.That(incomingMessages, Is.Empty);
        }
Exemplo n.º 8
0
        public async Task Event_consumer_is_exposed_when_sequence_is_subscribed(RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            sequence.Subscribe(_ => { }); // subscribes to the sequence but takes no action when items are published

            Assert.That(sut.Consumers.Count, Is.EqualTo(1));
        }
Exemplo n.º 9
0
        public async Task RabbitMq_headers_are_read_from_incoming_message([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestEvent Event, string headerKey, string headerValue)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            var sequence = await sut.StartAsync();

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]       = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]     = encoding.GetBytes(DescriptorName(Event.GetType())),
                    ["Nybus:CorrelationId"]   = correlationId.ToByteArray(),
                    [$"RabbitMq:{headerKey}"] = headerValue
                }
            };

            var body = configuration.Serializer.SerializeObject(Event, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Assert.That(incomingMessages, Has.Exactly(1).InstanceOf <EventMessage <FirstTestEvent> >());

            var message = incomingMessages[0] as EventMessage <FirstTestEvent>;

            Assert.That(message, Is.Not.Null);
            Assert.That(message.MessageId, Is.EqualTo(messageId));
            Assert.That(message.MessageType, Is.EqualTo(MessageType.Event));
            Assert.That(message.Type, Is.EqualTo(Event.GetType()));
            Assert.That(message.Event, Is.Not.Null);

            Assert.That(message.Headers, Contains.Key($"RabbitMq:{headerKey}"));
            Assert.That(message.Headers[$"RabbitMq:{headerKey}"], Is.EqualTo(headerValue));
        }
Exemplo n.º 10
0
        public void SubscribeToEvent_registers_event_type([Frozen] IMessageDescriptorStore messageDescriptorStore, RabbitMqBusEngine sut)
        {
            sut.SubscribeToEvent <FirstTestEvent>();

            Assert.That(messageDescriptorStore.Events, Contains.Item(typeof(FirstTestEvent)));
        }