예제 #1
0
        public async Task StartConsumeAsync_Test(bool autoDelete)
        {
            int maxConcurrentMessages = 100;
            var queueName             = RabbitMapper.GetQueueName(_serviceName, _topicName);

            var consumer = new RabbitConsumer(_connection, _serviceName, _topicName, MessageHandler,
                                              () => { }, false, maxConcurrentMessages, _options, _logger);

            // first start
            await consumer.StartConsumeAsync(_cancellationTokenSource.Token);

            A.CallTo(() => _rabbitChannel.BasicQos(0, (ushort)maxConcurrentMessages, false))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => _rabbitChannel.QueueDeclare(queueName, true, false, false, null))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => _rabbitChannel.ExchangeDeclare(_options.Exchange, ExchangeType.Direct, true, false, null))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => _rabbitChannel.QueueBind(queueName, _options.Exchange, _topicName, null))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() =>
                     _rabbitChannel.BasicConsume(queueName, false, "", false, false, null,
                                                 A <AsyncEventingBasicConsumer> .Ignored))
            .MustHaveHappenedOnceExactly();
            Assert.IsTrue(consumer.IsStarted);

            // does not repeat execution if started multiple times
            await consumer.StartConsumeAsync(_cancellationTokenSource.Token);

            A.CallTo(() => _rabbitChannel.QueueDeclare(queueName, true, false, false, null))
            .MustHaveHappenedOnceExactly();
            Assert.IsTrue(consumer.IsStarted);
        }
예제 #2
0
        public async Task ConsumeRequest_WithError_Test()
        {
            var deliveryTag = (ulong)new Random().Next();

            var consumeAbortedCalled = false;
            var consumer             = new RabbitConsumer(_connection, _serviceName, _topicName, MessageHandlerError, () =>
            {
                consumeAbortedCalled = true;
            }, false, 100, _options, _logger);


            await consumer.StartConsumeAsync(_cancellationTokenSource.Token);

            var properties = A.Fake <IBasicProperties>();

            properties.ReplyTo       = "reply-to";
            properties.CorrelationId = Guid.NewGuid().ToString();
            await _basicConsumer.HandleBasicDeliver(_consumerTag, deliveryTag, false, "exchange", "routingKey", properties, null);

            A.CallTo(() => _rabbitChannel.BasicAck(deliveryTag, false))
            .MustNotHaveHappened();
            A.CallTo(() => _rabbitChannel.BasicNack(deliveryTag, false, true))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() =>
                     _rabbitChannel.BasicPublish("", properties.ReplyTo, false,
                                                 A <IBasicProperties> .That.Matches(bp => bp.CorrelationId == properties.CorrelationId),
                                                 A <ReadOnlyMemory <byte> > .Ignored))
            .MustNotHaveHappened();

            Assert.IsTrue(consumeAbortedCalled);
        }
예제 #3
0
        public async Task ConsumePublish_WithError_Test()
        {
            var deliveryTag = (ulong)new Random().Next();

            var consumeAbortedCalled = false;
            var consumer             = new RabbitConsumer(_connection, _serviceName, _topicName, MessageHandlerError, () =>
            {
                consumeAbortedCalled = true;
            }, false, 100, _options, _logger);

            await consumer.StartConsumeAsync(_cancellationTokenSource.Token);

            await _basicConsumer.HandleBasicDeliver(_consumerTag, deliveryTag, false, "exchange", "routingKey", null, null);

            A.CallTo(() => _rabbitChannel.BasicCancel(_consumerTag))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => _rabbitChannel.BasicNack(deliveryTag, false, true))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => _rabbitChannel.BasicAck(deliveryTag, false))
            .MustNotHaveHappened();
            A.CallTo(() =>
                     _rabbitChannel.BasicPublish("", A <string> .Ignored, A <bool> .Ignored,
                                                 A <IBasicProperties> .Ignored,
                                                 A <ReadOnlyMemory <byte> > .Ignored))
            .MustNotHaveHappened();

            Assert.IsTrue(consumeAbortedCalled);
        }
예제 #4
0
        public async Task StartConsumeAsyncCanceled_Test()
        {
            var consumer = new RabbitConsumer(_connection, _serviceName, _topicName, MessageHandler,
                                              () => { }, false, 100, _options, _logger);

            A.CallTo(() => _rabbitChannel.BasicConsume(A <string> .Ignored, A <bool> .Ignored, A <string> .Ignored, A <bool> .Ignored, A <bool> .Ignored, A <IDictionary <string, object> > .Ignored, A <IBasicConsumer> .Ignored))
            .Invokes(_ =>
            {
                _cancellationTokenSource.Cancel();
            });

            await Assert.ThrowsExceptionAsync <OperationCanceledException>(() =>
                                                                           consumer.StartConsumeAsync(_cancellationTokenSource.Token));
        }
예제 #5
0
        public async Task StopConsumeAsync_Test()
        {
            var consumer = new RabbitConsumer(_connection, _serviceName, _topicName, MessageHandler,
                                              () => { }, false, 100, _options, _logger);

            await consumer.StartConsumeAsync(_cancellationTokenSource.Token);

            Assert.IsTrue(consumer.IsStarted);

            // first stop
            consumer.AbortConsume();
            A.CallTo(() => _rabbitChannel.BasicCancel(_consumerTag))
            .MustHaveHappenedOnceExactly();
            Assert.IsFalse(consumer.IsStarted);


            // does not repeat execution if stopped multiple times
            consumer.AbortConsume();
            A.CallTo(() => _rabbitChannel.BasicCancel(_consumerTag))
            .MustHaveHappenedOnceExactly();
            Assert.IsFalse(consumer.IsStarted);
        }