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 <R.Exceptions.WireFormattingException>(e.InnerException); } finally { connectionFactory.Destroy(); } }
public InternalConsumer(BlockingQueueConsumer consumer, R.IModel channel, string queue, ILogger <InternalConsumer> logger = null) : base(channel) { Consumer = consumer; QueueName = queue; Logger = logger; }
public void TestNoLocalConsumerConfiguration() { var connectionFactory = new Mock <IConnectionFactory>(); var connection = new Mock <IConnection>(); var channel = new Mock <R.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 <R.IBasicConsumer>())); blockingQueueConsumer.Stop(); }
public void TestPrefetchIsSetOnFailedPassiveDeclaration() { var connectionFactory = new Mock <IConnectionFactory>(); var connection = new Mock <IConnection>(); var channel = new Mock <R.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 R.Exceptions.OperationInterruptedException(new R.ShutdownEventArgs(R.ShutdownInitiator.Peer, 0, "Expected")); } return(new R.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 <R.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 <R.IModel>(); var blockingQueueConsumer = new BlockingQueueConsumer( connectionFactory.Object, new DefaultMessageHeadersConverter(), new ActiveObjectCounter <BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, null, "testQ"); TestRequeueOrNotGuts(ex, expectedRequeue, channel, blockingQueueConsumer); }
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); var latch = new CountdownEvent(2); try { blockingQueueConsumer.Start(); var consumers = blockingQueueConsumer.CurrentConsumers(); 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(); } }
private void TestRequeueOrNotGuts(Exception ex, bool expectedRequeue, Mock <R.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 <R.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 <R.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 <R.IBasicConsumer>())) .Callback <string, bool, string, bool, bool, IDictionary <string, object>, R.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))); }