Example #1
0
        public void TestWithinInvoke()
        {
            var connectionFactory = new Mock <Connection.IConnectionFactory>();
            var connection        = new Mock <Connection.IConnection>();

            connectionFactory.Setup((f) => f.CreateConnection()).Returns(connection.Object);

            var channel1 = new Mock <RC.IModel>();
            var channel2 = new Mock <RC.IModel>();

            connection.SetupSequence((c) => c.CreateChannel(false)).Returns(channel1.Object).Returns(channel2.Object);
            var declareOk = new RC.QueueDeclareOk("foo", 0, 0);

            channel1.Setup((c) => c.QueueDeclare(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >())).Returns(declareOk);
            var template = new RabbitTemplate(connectionFactory.Object);
            var admin    = new RabbitAdmin(template);

            template.Invoke <object>((o) =>
            {
                admin.DeclareQueue();
                admin.DeclareQueue();
                admin.DeclareQueue();
                admin.DeclareQueue();
                return(null);
            });
            connection.Verify((c) => c.CreateChannel(false), Times.Once);
            channel1.Verify((c) => c.QueueDeclare(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >()), Times.Exactly(4));
            channel1.Verify((c) => c.Close(), Times.Once);
            channel2.VerifyNoOtherCalls();
        }
Example #2
0
        public void TestAvoidHangAMQP_508()
        {
            var cf       = new CachingConnectionFactory("localhost");
            var admin    = new RabbitAdmin(cf);
            var bytes    = new byte[300];
            var longName = Encoding.UTF8.GetString(bytes).Replace('\u0000', 'x');

            try
            {
                admin.DeclareQueue(new Queue(longName));
                throw new Exception("expected exception");
            }
            catch (Exception)
            {
                // Ignore
            }

            var goodName = "foobar";
            var name     = admin.DeclareQueue(new Queue(goodName));

            Assert.Null(admin.GetQueueProperties(longName));
            Assert.NotNull(admin.GetQueueProperties(goodName));
            admin.DeleteQueue(goodName);
            cf.Destroy();
        }
Example #3
0
        public async Task TestMasterLocator()
        {
            var factory = new RC.ConnectionFactory
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672/")
            };
            var cf    = new CachingConnectionFactory(factory);
            var admin = new RabbitAdmin(cf);
            var queue = new AnonymousQueue();

            admin.DeclareQueue(queue);
            var client    = new HttpClient();
            var authToken = Encoding.ASCII.GetBytes("guest:guest");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));

            var result = await client.GetAsync("http://localhost:15672/api/queues/%3F/" + queue.QueueName);

            var n = 0;

            while (n++ < 100 && result.StatusCode == HttpStatusCode.NotFound)
            {
                await Task.Delay(100);

                result = await client.GetAsync("http://localhost:15672/api/queues/%2F/" + queue.QueueName);
            }

            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            var content = await result.Content.ReadAsStringAsync();

            Assert.Contains("x-queue-master-locator", content);
            Assert.Contains("client-local", content);

            queue = new AnonymousQueue
            {
                MasterLocator = null
            };
            admin.DeclareQueue(queue);

            result = await client.GetAsync("http://localhost:15672/api/queues/%3F/" + queue.QueueName);

            n = 0;
            while (n++ < 100 && result.StatusCode == HttpStatusCode.NotFound)
            {
                await Task.Delay(100);

                result = await client.GetAsync("http://localhost:15672/api/queues/%2F/" + queue.QueueName);
            }

            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            content = await result.Content.ReadAsStringAsync();

            Assert.DoesNotContain("x-queue-master-locator", content);
            Assert.DoesNotContain("client-local", content);
            cf.Destroy();
        }
        public void TestDoubleDeclarationOfExclusiveQueue()
        {
            services.AddRabbitConnectionFactory("connectionFactory1", (p, f) =>
            {
                f.Host = "localhost";
            });
            services.AddRabbitConnectionFactory("connectionFactory2", (p, f) =>
            {
                f.Host = "localhost";
            });
            provider = services.BuildServiceProvider();

            var queue       = new Queue("test.queue", false, true, true);
            var rabbitAdmin = provider.GetRabbitAdmin();

            rabbitAdmin.DeleteQueue(queue.QueueName);

            var context = provider.GetApplicationContext();
            var admin1  = new RabbitAdmin(context, context.GetService <IConnectionFactory>("connectionFactory1"));

            admin1.DeclareQueue(queue);
            try
            {
                var admin2 = new RabbitAdmin(context, context.GetService <IConnectionFactory>("connectionFactory2"));
                Assert.Throws <RabbitIOException>(() => admin2.DeclareQueue(queue));
            }
            finally
            {
                var cf1 = context.GetService <IConnectionFactory>("connectionFactory1");
                var cf2 = context.GetService <IConnectionFactory>("connectionFactory2");
                cf1.Destroy();
                cf2.Destroy();
            }
        }
        public void TestDoubleDeclarationOfAutodeleteQueue()
        {
            services.AddRabbitConnectionFactory("connectionFactory1", (p, f) =>
            {
                f.Host = "localhost";
            });
            services.AddRabbitConnectionFactory("connectionFactory2", (p, f) =>
            {
                f.Host = "localhost";
            });
            provider = services.BuildServiceProvider();
            var queue = new Queue("test.queue", false, false, true);

            var context = provider.GetApplicationContext();
            var cf1     = context.GetService <IConnectionFactory>("connectionFactory1");
            var cf2     = context.GetService <IConnectionFactory>("connectionFactory2");
            var admin1  = new RabbitAdmin(context, cf1);

            admin1.DeclareQueue(queue);
            var admin2 = new RabbitAdmin(context, cf2);

            admin2.DeclareQueue(queue);
            cf1.Destroy();
            cf2.Destroy();
        }
        public BatchingRabbitTemplateTest(ITestOutputHelper testOutputHelper)
        {
            connectionFactory = new CachingConnectionFactory("localhost");
            var admin = new RabbitAdmin(connectionFactory);

            admin.DeclareQueue(new Queue(ROUTE));
            this.testOutputHelper = testOutputHelper;
        }
Example #7
0
        public void TestIgnoreDeclarationExceptionsTimeout()
        {
            var rabbitConnectionFactory = new Mock <RC.IConnectionFactory>();
            var toBeThrown = new TimeoutException("test");

            rabbitConnectionFactory.Setup((c) => c.CreateConnection(It.IsAny <string>())).Throws(toBeThrown);
            var ccf   = new CachingConnectionFactory(rabbitConnectionFactory.Object);
            var admin = new RabbitAdmin(ccf)
            {
                IgnoreDeclarationExceptions = true
            };

            admin.DeclareQueue(new AnonymousQueue("test"));
            var lastEvent = admin.LastDeclarationExceptionEvent;

            Assert.Same(admin, lastEvent.Source);
            Assert.Same(toBeThrown, lastEvent.Exception.InnerException);
            Assert.IsType <AnonymousQueue>(lastEvent.Declarable);

            admin.DeclareQueue();
            lastEvent = admin.LastDeclarationExceptionEvent;
            Assert.Same(admin, lastEvent.Source);
            Assert.Same(toBeThrown, lastEvent.Exception.InnerException);
            Assert.Null(lastEvent.Declarable);

            admin.DeclareExchange(new DirectExchange("foo"));
            lastEvent = admin.LastDeclarationExceptionEvent;
            Assert.Same(admin, lastEvent.Source);
            Assert.Same(toBeThrown, lastEvent.Exception.InnerException);
            Assert.IsType <DirectExchange>(lastEvent.Declarable);

            admin.DeclareBinding(new Binding("foo", "foo", DestinationType.QUEUE, "bar", "baz", null));
            lastEvent = admin.LastDeclarationExceptionEvent;
            Assert.Same(admin, lastEvent.Source);
            Assert.Same(toBeThrown, lastEvent.Exception.InnerException);
            Assert.IsType <Binding>(lastEvent.Declarable);
        }
Example #8
0
        public async Task TestGetQueueProperties()
        {
            var serviceCollection = CreateContainer();

            serviceCollection.AddRabbitConnectionFactory <SingleConnectionFactory>((p, f) =>
            {
                f.Host = "localhost";
            });
            var provider           = serviceCollection.BuildServiceProvider();
            var applicationContext = provider.GetService <IApplicationContext>();
            var connectionFactory  = applicationContext.GetService <IConnectionFactory>();
            var rabbitAdmin        = new RabbitAdmin(applicationContext, connectionFactory);
            var queueName          = "test.properties." + DateTimeOffset.Now.ToUnixTimeMilliseconds();

            try
            {
                rabbitAdmin.DeclareQueue(new Config.Queue(queueName));
                var template = new RabbitTemplate(connectionFactory);
                template.ConvertAndSend(queueName, "foo");
                var n = 0;
                while (n++ < 100 && MessageCount(rabbitAdmin, queueName) == 0)
                {
                    await Task.Delay(100);
                }

                Assert.True(n < 100);
                var channel  = connectionFactory.CreateConnection().CreateChannel(false);
                var consumer = new RC.DefaultBasicConsumer(channel);
                RC.IModelExensions.BasicConsume(channel, queueName, true, consumer);
                n = 0;
                while (n++ < 100 && MessageCount(rabbitAdmin, queueName) > 0)
                {
                    await Task.Delay(100);
                }

                Assert.True(n < 100);

                var props = rabbitAdmin.GetQueueProperties(queueName);
                Assert.True(props.TryGetValue(RabbitAdmin.QUEUE_CONSUMER_COUNT, out var consumerCount));
                Assert.Equal(1U, consumerCount);
                channel.Close();
            }
            finally
            {
                rabbitAdmin.DeleteQueue(queueName);
                connectionFactory.Destroy();
            }
        }
Example #9
0
        public void TestFailOnFirstUseWithMissingBroker()
        {
            var serviceCollection = CreateContainer();

            serviceCollection.AddRabbitQueue(new Config.Queue("foo"));
            serviceCollection.AddRabbitConnectionFactory <SingleConnectionFactory>((p, f) =>
            {
                f.Host = "localhost";
                f.Port = 434343;
            });

            var provider           = serviceCollection.BuildServiceProvider();
            var applicationContext = provider.GetService <IApplicationContext>();
            var connectionFactory  = applicationContext.GetService <IConnectionFactory>();
            var rabbitAdmin        = new RabbitAdmin(applicationContext, connectionFactory)
            {
                AutoStartup = true
            };

            Assert.Throws <RabbitConnectException>(() => rabbitAdmin.DeclareQueue());
            connectionFactory.Destroy();
        }