public void TestAvoidHangAMQP_508()
        {
            var connectionFactory     = new CachingConnectionFactory("localhost");
            var longName              = new string('x', 300);
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                true,
                1,
                null,
                longName,
                "foobar");

            try
            {
                blockingQueueConsumer.Start();
                throw new Exception("Expected exception");
            }
            catch (FatalListenerStartupException e)
            {
                Assert.IsType <RC.Exceptions.WireFormattingException>(e.InnerException);
            }
            finally
            {
                connectionFactory.Destroy();
            }
        }
        public void TestNoLocalConsumerConfiguration()
        {
            var connectionFactory = new Mock <IConnectionFactory>();
            var connection        = new Mock <IConnection>();
            var channel           = new Mock <RC.IModel>();

            connectionFactory.Setup((f) => f.CreateConnection()).Returns(connection.Object);
            connection.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel.Object);
            connection.Setup(c => c.IsOpen).Returns(true);
            channel.Setup(c => c.IsOpen).Returns(true);

            var queue   = "testQ";
            var noLocal = true;
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory.Object,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                true,
                1,
                true,
                null,
                noLocal,
                false,
                null,
                queue);

            blockingQueueConsumer.Start();
            channel.Verify(c => c.BasicConsume("testQ", AcknowledgeMode.AUTO.IsAutoAck(), string.Empty, noLocal, false, It.IsAny <IDictionary <string, object> >(), It.IsAny <RC.IBasicConsumer>()));
            blockingQueueConsumer.Stop();
        }
Ejemplo n.º 3
0
 public InternalConsumer(BlockingQueueConsumer consumer, RC.IModel channel, string queue, ILogger <InternalConsumer> logger = null)
     : base(channel)
 {
     Consumer  = consumer;
     QueueName = queue;
     Logger    = logger;
 }
        public void TestTransactionalLowLevel()
        {
            var connectionFactory = new CachingConnectionFactory("localhost");
            var admin             = new RabbitAdmin(connectionFactory);

            admin.DeclareQueue(new Queue(QUEUE1_NAME));
            admin.DeclareQueue(new Queue(QUEUE2_NAME));
            var template = new RabbitTemplate(connectionFactory);
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                true,
                1,
                null,
                QUEUE1_NAME,
                QUEUE2_NAME);
            var prefix = Guid.NewGuid().ToString();

            blockingQueueConsumer.TagStrategy = new TagStrategy(prefix);
            try
            {
                blockingQueueConsumer.Start();
                int n         = 0;
                var consumers = blockingQueueConsumer.CurrentConsumers();

                // Wait for consumers
                while (n < 100)
                {
                    if (consumers.Count < 2)
                    {
                        n++;
                        Thread.Sleep(100);
                        consumers = blockingQueueConsumer.CurrentConsumers();
                    }
                    else
                    {
                        break;
                    }
                }

                Assert.Equal(2, consumers.Count);
                var tags = new List <string>()
                {
                    consumers[0].ConsumerTag, consumers[1].ConsumerTag
                };
                Assert.Contains(prefix + "#" + QUEUE1_NAME, tags);
                Assert.Contains(prefix + "#" + QUEUE2_NAME, tags);
                blockingQueueConsumer.Stop();
                Assert.Null(template.ReceiveAndConvert <object>(QUEUE1_NAME));
            }
            finally
            {
                admin.DeleteQueue(QUEUE1_NAME);
                admin.DeleteQueue(QUEUE2_NAME);
                connectionFactory.Destroy();
            }
        }
        public void TestPrefetchIsSetOnFailedPassiveDeclaration()
        {
            var connectionFactory = new Mock <IConnectionFactory>();
            var connection        = new Mock <IConnection>();
            var channel           = new Mock <RC.IModel>();

            connectionFactory.Setup((f) => f.CreateConnection()).Returns(connection.Object);
            connection.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel.Object);
            connection.Setup(c => c.IsOpen).Returns(true);
            channel.Setup(c => c.IsOpen).Returns(true);
            var throws = new AtomicBoolean(false);

            channel.Setup(c => c.QueueDeclarePassive(It.IsAny <string>()))
            .Callback <string>((arg) =>
            {
                if (arg != "good")
                {
                    throws.Value = true;
                }
                else
                {
                    throws.Value = false;
                }
            })
            .Returns(() =>
            {
                if (throws.Value)
                {
                    throw new RC.Exceptions.OperationInterruptedException(new RC.ShutdownEventArgs(RC.ShutdownInitiator.Peer, 0, "Expected"));
                }

                return(new RC.QueueDeclareOk("any", 0, 0));
            });
            channel.Setup(c => c.BasicConsume(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >(), It.IsAny <RC.IBasicConsumer>()))
            .Returns("consumerTag");
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory.Object,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                true,
                20,
                null,
                "good",
                "bad")
            {
                DeclarationRetries             = 1,
                RetryDeclarationInterval       = 10,
                FailedDeclarationRetryInterval = 10
            };

            blockingQueueConsumer.Start();
            channel.Verify(c => c.BasicQos(It.IsAny <uint>(), 20, It.IsAny <bool>()));
            blockingQueueConsumer.Stop();
        }
        private void TestRequeueOrNotDefaultYes(Exception ex, bool expectedRequeue)
        {
            var connectionFactory     = new Mock <IConnectionFactory>();
            var channel               = new Mock <RC.IModel>();
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory.Object,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                true,
                1,
                null,
                "testQ");

            TestRequeueOrNotGuts(ex, expectedRequeue, channel, blockingQueueConsumer);
        }
        private void TestRequeueOrNotGuts(Exception ex, bool expectedRequeue, Mock <RC.IModel> channel, BlockingQueueConsumer blockingQueueConsumer)
        {
            blockingQueueConsumer.Channel = channel.Object;
            var deliveryTags = new HashSet <ulong>
            {
                1UL
            };

            blockingQueueConsumer.DeliveryTags = deliveryTags;
            blockingQueueConsumer.RollbackOnExceptionIfNecessary(ex);
            channel.Verify((c) => c.BasicNack(1UL, true, expectedRequeue));
        }
        public void TestRecoverAfterDeletedQueueAndLostConnection()
        {
            var connectionFactory = new Mock <IConnectionFactory>();
            var connection        = new Mock <IConnection>();
            var channel           = new Mock <RC.IModel>();

            connectionFactory.Setup((f) => f.CreateConnection()).Returns(connection.Object);
            connection.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel.Object);
            connection.Setup(c => c.IsOpen).Returns(true);
            channel.Setup(c => c.IsOpen).Returns(true);
            var n = new AtomicInteger();
            var consumerCaptor = new AtomicReference <RC.IBasicConsumer>();
            var consumerLatch  = new CountdownEvent(2);

            channel.Setup(c => c.BasicConsume(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >(), It.IsAny <RC.IBasicConsumer>()))
            .Callback <string, bool, string, bool, bool, IDictionary <string, object>, RC.IBasicConsumer>((a1, a2, a3, a4, a5, a6, a7) =>
            {
                consumerCaptor.Value = a7;
                consumerLatch.Signal();
            })
            .Returns("consumer" + n.IncrementAndGet());
            channel.Setup(c => c.BasicCancel("consumer2"))
            .Throws(new Exception("Intentional cancel fail"));
            var blockingQueueConsumer = new BlockingQueueConsumer(
                connectionFactory.Object,
                new DefaultMessageHeadersConverter(),
                new ActiveObjectCounter <BlockingQueueConsumer>(),
                AcknowledgeMode.AUTO,
                false,
                1,
                null,
                "testQ1",
                "testQ2");
            var latch = new CountdownEvent(1);

            Task.Run(() =>
            {
                blockingQueueConsumer.Start();
                while (true)
                {
                    try
                    {
                        blockingQueueConsumer.NextMessage(1000);
                    }
                    catch (ConsumerCancelledException)
                    {
                        latch.Signal();
                        break;
                    }
                    catch (ShutdownSignalException)
                    {
                        // Noop
                    }
                    catch (Exception)
                    {
                        // noop
                    }
                }
            });
            Assert.True(consumerLatch.Wait(TimeSpan.FromSeconds(10)));
            var consumer = consumerCaptor.Value;

            consumer.HandleBasicCancel("consumer1");
            Assert.True(latch.Wait(TimeSpan.FromSeconds(10)));
        }