Beispiel #1
0
 public void TestDoubleDeclarationOfExclusiveQueue()
 {
     // Expect exception because the queue is locked when it is declared a second time.
     var connectionFactory1 = new CachingConnectionFactory();
     connectionFactory1.Port = BrokerTestUtils.GetPort();
     var connectionFactory2 = new CachingConnectionFactory();
     connectionFactory2.Port = BrokerTestUtils.GetPort();
     var queue = new Queue("test.queue", false, true, true);
     this.rabbitAdmin.DeleteQueue(queue.Name);
     new RabbitAdmin(connectionFactory1).DeclareQueue(queue);
     try
     {
         new RabbitAdmin(connectionFactory2).DeclareQueue(queue);
         Assert.Fail("Expected an exception, and one was not thrown.");
     }
     catch (Exception ex)
     {
         Assert.True(ex is AmqpIOException, "Expecting an AmqpIOException");
     }
     finally
     {
         // Need to release the connection so the exclusive queue is deleted
         connectionFactory1.Dispose();
     }
 }
Beispiel #2
0
        /// <summary>
        /// Creates the connection factory.
        /// </summary>
        /// <returns>The connection factory.</returns>
        protected override IConnectionFactory CreateConnectionFactory()
        {
            var connectionFactory = new SingleConnectionFactory();

            connectionFactory.Port = BrokerTestUtils.GetPort();
            return(connectionFactory);
        }
Beispiel #3
0
        /// <summary>Queues the exists.</summary>
        /// <param name="queue">The queue.</param>
        /// <returns>The System.Boolean.</returns>
        /// Use native Rabbit API to test queue, bypassing all the connection and channel caching and callbacks in Spring
        /// AMQP.
        /// @param connection the raw connection to use
        /// @param queue the Queue to test
        /// @return true if the queue exists
        private bool QueueExists(Queue queue)
        {
            var connectionFactory = new ConnectionFactory();
            connectionFactory.Port = BrokerTestUtils.GetPort();
            var connection = connectionFactory.CreateConnection();
            var channel = connection.CreateModel();

            try
            {
                var result = channel.QueueDeclarePassive(queue.Name);
                return result != null;
            }
            catch (Exception e)
            {
                if (e.Message.Contains("RESOURCE_LOCKED"))
                {
                    return true;
                }

                return false;
            }
            finally
            {
                connection.Close();
            }
        }
        /// <summary>
        /// Creates the connection factory.
        /// </summary>
        /// <returns>The connection factory.</returns>
        protected virtual IConnectionFactory CreateConnectionFactory()
        {
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = this.concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            return(connectionFactory);
        }
        public void Create()
        {
            this.brokerIsRunning = BrokerRunning.IsRunningWithEmptyQueues(ROUTE);
            this.brokerIsRunning.Apply();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.Port = BrokerTestUtils.GetPort();
            this.template          = new RabbitTemplate(connectionFactory);
        }
Beispiel #6
0
        public void DeclareQueue()
        {
            this.brokerIsRunning.Apply();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = this.concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            this.template.ConnectionFactory    = connectionFactory;
        }
        public void CreateConnectionFactory()
        {
            this.brokerIsRunning = BrokerRunning.IsRunningWithEmptyQueues(this.queue);
            this.brokerIsRunning.Apply();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = this.concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            this.template.ConnectionFactory    = connectionFactory;
        }
 public void DeclareQueue()
 {
     // if (repeat.isInitialized()) {
     // // Important to prevent concurrent re-initialization
     // return;
     // }
     this.connectionFactory = new CachingConnectionFactory();
     this.connectionFactory.ChannelCacheSize = 4;
     this.connectionFactory.Port             = BrokerTestUtils.GetPort();
     this.template.ConnectionFactory         = this.connectionFactory;
 }
        private RabbitTemplate CreateTemplate(int concurrentConsumers)
        {
            var template = new RabbitTemplate();

            // SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            template.ConnectionFactory         = connectionFactory;
            return(template);
        }
Beispiel #10
0
        /// <summary>Does the test.</summary>
        /// <param name="concurrentConsumers">The concurrent consumers.</param>
        /// <param name="configurer">The configurer.</param>
        private void DoTest(int concurrentConsumers, IContainerConfigurer configurer)
        {
            var messageCount      = 10;
            var template          = new RabbitTemplate();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            template.ConnectionFactory         = connectionFactory;
            var messageConverter = new SimpleMessageConverter();

            messageConverter.CreateMessageIds = true;
            template.MessageConverter         = messageConverter;
            for (var i = 0; i < messageCount; i++)
            {
                template.ConvertAndSend(queue1.Name, i);
                template.ConvertAndSend(queue2.Name, i);
            }

            var container = new SimpleMessageListenerContainer(connectionFactory);
            var latch     = new CountdownEvent(messageCount * 2);
            var listener  = new MultiplePocoListener(latch);

            container.MessageListener     = new MessageListenerAdapter(listener);
            container.AcknowledgeMode     = AcknowledgeModeUtils.AcknowledgeMode.Auto;
            container.ChannelTransacted   = true;
            container.ConcurrentConsumers = concurrentConsumers;
            configurer.Configure(container);
            container.AfterPropertiesSet();
            container.Start();
            try
            {
                var timeout = Math.Min((1 + messageCount) / concurrentConsumers, 50);
                Logger.Info("Timeout: " + timeout);
                var waited = latch.Wait(timeout * 1000);
                Logger.Info("All messages recovered: " + waited);
                Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount);
                Assert.True(waited, "Timed out waiting for messages");
            }
            catch (ThreadInterruptedException e)
            {
                Thread.CurrentThread.Interrupt();
                throw new ThreadStateException("unexpected interruption");
            }
            finally
            {
                container.Shutdown();
                Assert.AreEqual(0, container.ActiveConsumerCount);
            }

            Assert.Null(template.ReceiveAndConvert(queue1.Name));
            Assert.Null(template.ReceiveAndConvert(queue2.Name));
        }
Beispiel #11
0
 public void TestDoubleDeclarationOfAutodeleteQueue()
 {
     // No error expected here: the queue is autodeleted when the last consumer is cancelled, but this one never has
     // any consumers.
     var connectionFactory1 = new CachingConnectionFactory();
     connectionFactory1.Port = BrokerTestUtils.GetPort();
     var connectionFactory2 = new CachingConnectionFactory();
     connectionFactory2.Port = BrokerTestUtils.GetPort();
     var queue = new Queue("test.queue", false, false, true);
     new RabbitAdmin(connectionFactory1).DeclareQueue(queue);
     new RabbitAdmin(connectionFactory2).DeclareQueue(queue);
     connectionFactory1.Dispose();
     connectionFactory2.Dispose();
 }
        /// <summary>Creates the template.</summary>
        /// <param name="concurrentConsumers">The concurrent consumers.</param>
        /// <returns>The template.</returns>
        private RabbitTemplate CreateTemplate(int concurrentConsumers)
        {
            var template          = new RabbitTemplate();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.ChannelCacheSize = concurrentConsumers;
            connectionFactory.Port             = BrokerTestUtils.GetPort();
            template.ConnectionFactory         = connectionFactory;
            if (this.messageConverter == null)
            {
                var internalmessageConverter = new SimpleMessageConverter();
                internalmessageConverter.CreateMessageIds = true;
                this.messageConverter = internalmessageConverter;
            }

            template.MessageConverter = this.messageConverter;
            return(template);
        }
Beispiel #13
0
        public void TestTransactionalLowLevel()
        {
            var template          = new RabbitTemplate();
            var connectionFactory = new CachingConnectionFactory();

            connectionFactory.Port     = BrokerTestUtils.GetPort();
            template.ConnectionFactory = connectionFactory;

            var blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter <BlockingQueueConsumer>(), AcknowledgeModeUtils.AcknowledgeMode.Auto, true, 1, queue.Name);

            blockingQueueConsumer.Start();
            connectionFactory.Dispose();

            // TODO: make this into a proper assertion. An exception can be thrown here by the Rabbit client and printed to
            // stderr without being rethrown (so hard to make a test fail).
            blockingQueueConsumer.Stop();
            Assert.IsNull(template.ReceiveAndConvert(queue.Name));
        }
        public void TestArgumentsQueue()
        {
            var queue = this.objectFactory.GetObject <Queue>("arguments");

            Assert.IsNotNull(queue);

            var template    = new RabbitTemplate(new CachingConnectionFactory(BrokerTestUtils.GetPort()));
            var rabbitAdmin = new RabbitAdmin(template.ConnectionFactory);

            rabbitAdmin.DeleteQueue(queue.Name);
            rabbitAdmin.DeclareQueue(queue);

            Assert.AreEqual(100L, queue.Arguments["x-message-ttl"]);
            template.ConvertAndSend(queue.Name, "message");

            Thread.Sleep(200);
            var result = (string)template.ReceiveAndConvert(queue.Name);

            Assert.AreEqual(null, result);
        }
Beispiel #15
0
        public void Init()
        {
            this.factory.HostName = Dns.GetHostName().ToUpper();
            this.factory.Port     = BrokerTestUtils.GetPort();
            this.conn             = this.factory.CreateConnection();
            this.noTxChannel      = this.conn.CreateModel();
            this.txChannel        = this.conn.CreateModel();

            // Note: the Java client includes a convenience method with one int argument that sets the prefetchSize to 0 and global to false.
            this.txChannel.BasicQos(0, 1, false);
            this.txChannel.TxSelect();

            try
            {
                this.noTxChannel.QueueDelete("test.queue");
            }
            catch (Exception e)
            {
                this.noTxChannel = this.conn.CreateModel();
            }

            this.noTxChannel.QueueDeclare("test.queue", true, false, false, null);
        }
Beispiel #16
0
 public void SetUp()
 {
     this.connectionFactory      = new CachingConnectionFactory();
     this.brokerIsRunning        = BrokerRunning.IsRunning();
     this.connectionFactory.Port = BrokerTestUtils.GetPort();
 }
Beispiel #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RabbitAdminIntegrationTests"/> class. 
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 public RabbitAdminIntegrationTests() { this.connectionFactory.Port = BrokerTestUtils.GetPort(); }