public async Task ListenToQueueOverloadCalls_EventReceived_WhenEventIsReceivedForMultipleQueues()
        {
            const string queueName2 = "otherQueue";

            EventReceivedCallback callbackFromBus1 = null;
            EventReceivedCallback callbackFromBus2 = null;

            SetupBusProviderMockBasicConsume(
                QueueName,
                (queue, callback, ack) => callbackFromBus1 = callback);

            SetupBusProviderMockBasicConsume(
                queueName2,
                (queue, callback, ack) => callbackFromBus2 = callback);

            var listenToQueueAndTopicStub2 = new ListenToQueueAndTopicStub <Person>();

            await _sut.SetupQueueListenerAsync(QueueName, WildcardTopic, _listenToQueueAndTopicStub.GenericTypeEventListenerCallback());

            await _sut.SetupQueueListenerAsync(queueName2, WildcardTopic, listenToQueueAndTopicStub2.GenericTypeEventListenerCallback());

            callbackFromBus1.Invoke(EventMessage);
            callbackFromBus2.Invoke(EventMessage);

            _listenToQueueAndTopicStub.WaitHandle.WaitOne(Timeout).ShouldBeTrue();
            listenToQueueAndTopicStub2.WaitHandle.WaitOne(Timeout).ShouldBeTrue();

            _listenToQueueAndTopicStub.ReceivedObject.Name.ShouldBe(Person.Name);
            listenToQueueAndTopicStub2.ReceivedObject.Name.ShouldBe(Person.Name);
        }
        private static void HandleReceivedEvent(BasicDeliverEventArgs args, EventReceivedCallback callback)
        {
            var message = Encoding.UTF8.GetString(args.Body);

            Guid?correlationId = null;

            if (args.BasicProperties.CorrelationId != null &&
                Guid.TryParse(args.BasicProperties.CorrelationId, out Guid parsedId))
            {
                correlationId = parsedId;
            }

            bool?isError = (bool?)args.BasicProperties.Headers?.FirstOrDefault(a => a.Key == "isError").Value;

            var eventMessage = new EventMessage()
            {
                JsonMessage    = message,
                RoutingKey     = args.RoutingKey,
                CorrelationId  = correlationId,
                Timestamp      = args.BasicProperties.Timestamp.UnixTime,
                ReplyQueueName = args.BasicProperties.ReplyTo,
                Type           = args.BasicProperties.Type,
                IsError        = isError != null && isError.Value,
                DeliveryTag    = args.DeliveryTag
            };

            callback(eventMessage);
        }
        public void EventCanBePublishedAndReceived()
        {
            using (var sut = new RabbitBusProvider(BusOptions))
            {
                sut.EnsureConnection();
                var queue = GetUniqueQueue();
                var topic = GetUniqueTopic();

                EventMessage          receivedMessage       = null;
                var                   waitHandle            = new ManualResetEvent(false);
                EventReceivedCallback eventReceivedCallback = (message) =>
                {
                    receivedMessage = message;
                    waitHandle.Set();
                };

                var sentMessage = new EventMessage
                {
                    JsonMessage = "Something",
                    RoutingKey  = topic,
                    Type        = TopicType
                };

                sut.BasicTopicBind(queue, topic);
                sut.BasicConsume(queue, eventReceivedCallback);
                sut.BasicPublish(sentMessage);

                waitHandle.WaitOne(2000).ShouldBeTrue();
                receivedMessage.JsonMessage.ShouldBe(sentMessage.JsonMessage);
            }
        }
        public void EventCanBeReceived()
        {
            using (var sut = new RabbitBusProvider(BusOptions))
            {
                sut.EnsureConnection();
                var          queue       = GetUniqueQueue();
                var          topic       = GetUniqueTopic();
                const string jsonMessage = "Something";

                EventMessage          passedMessage         = null;
                var                   waitHandle            = new ManualResetEvent(false);
                EventReceivedCallback eventReceivedCallback = (message) =>
                {
                    passedMessage = message;
                    waitHandle.Set();
                };

                sut.BasicTopicBind(queue, topic);
                sut.BasicConsume(queue, eventReceivedCallback);

                SendRabbitEventToExchange(topic, jsonMessage);

                waitHandle.WaitOne(2000).ShouldBeTrue();
                passedMessage.ShouldNotBeNull();
                passedMessage.JsonMessage.ShouldBe(jsonMessage);
            }
        }
Esempio n. 5
0
        protected override void RegisterCallbackListener(string queueName, EventReceivedCallback callback)
        {
            _busProvider.BasicConsume(
                queueName: queueName,
                callback: callback,
                autoAcknowledge: false);

            _busProvider.BasicQos(0, 1);
        }
        public async Task ListenToQueueOverloadCalls_EventReceived_WhenEventIsReceivedForCorrectTopic()
        {
            EventReceivedCallback callbackFromBus = null;

            SetupBusProviderMockBasicConsume(
                QueueName,
                (queue, callback, ack) => callbackFromBus = callback);

            await _sut.SetupQueueListenerAsync(QueueName, WildcardTopic, _listenToQueueAndTopicStub.GenericTypeEventListenerCallback());

            callbackFromBus.Invoke(EventMessage);

            _listenToQueueAndTopicStub.WaitHandle.WaitOne(Timeout).ShouldBeTrue();
            _listenToQueueAndTopicStub.ReceivedObject.Name.ShouldBe(Person.Name);
        }
        public void BasicConsume(string queueName, EventReceivedCallback callback, bool autoAcknowledge = true)
        {
            EnsureArg.IsNotNullOrWhiteSpace(queueName, nameof(queueName));
            EnsureArg.IsNotNull(callback, nameof(callback));

            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += (sender, args) => HandleReceivedEvent(args, callback);

            QueueDeclare(queueName);

            lock (_channel)
            {
                _channel.BasicConsume(
                    queue: queueName,
                    autoAck: autoAcknowledge,
                    consumer: consumer);
            }
        }
Esempio n. 8
0
        public void TestInitialize()
        {
            _busProviderMock
            .Setup(b => b.BasicConsume(It.IsAny <string>(), It.IsAny <EventReceivedCallback>(), It.IsAny <bool>()))
            .Callback <string, EventReceivedCallback, bool>((que, callback, ack) => _eventReceivedCallback = callback);

            _busProviderMock
            .Setup(b => b.EnsureConnection());

            _busProviderMock
            .Setup(b => b.BasicTopicBind(It.IsAny <string>(), It.IsAny <string>(), ExchangeName));

            _busProviderMock
            .Setup(b => b.BasicPublish(It.Is(CorrectEventMessage), ExchangeName))
            .Callback(BasicPublishCallback)
            .Verifiable();

            _sut = new CommandPublisher(_busProviderMock.Object);
        }
Esempio n. 9
0
 protected override void RegisterCallbackListener(string queueName, EventReceivedCallback callback)
 {
     _busProvider.BasicConsume(queueName, callback);
 }
 protected abstract void RegisterCallbackListener(string queueName, EventReceivedCallback callback);