Exemple #1
0
            public CustomStartupFixture()
            {
                adminCf = new CachingConnectionFactory("localhost");
                admin   = new RabbitAdmin(adminCf, null);
                foreach (var q in Queues)
                {
                    var queue = new Config.Queue(q);
                    admin.DeclareQueue(queue);
                }

                services = CreateContainer();
                Provider = services.BuildServiceProvider();
                Provider.GetRequiredService <IHostedService>().StartAsync(default).Wait();
Exemple #2
0
        public virtual void AddQueueNames(params string[] queueNames)
        {
            if (queueNames == null)
            {
                throw new ArgumentNullException(nameof(queueNames));
            }

            var qs    = new Config.Queue[queueNames.Length];
            var index = 0;

            foreach (var name in queueNames)
            {
                if (name == null)
                {
                    throw new ArgumentNullException("queue names cannot be null");
                }

                qs[index++] = new Config.Queue(name);
            }

            AddQueues(qs);
        }
Exemple #3
0
        public void TestHardErrorAndReconnectNoAuto()
        {
            var template = new RabbitTemplate(connectionFactory);
            var admin    = new RabbitAdmin(connectionFactory);
            var queue    = new Config.Queue(CF_INTEGRATION_TEST_QUEUE);

            admin.DeclareQueue(queue);
            var route = queue.QueueName;
            var latch = new CountdownEvent(1);

            try
            {
                template.Execute((channel) =>
                {
                    channel.ModelShutdown += (sender, args) =>
                    {
                        latch.Signal();
                        throw new ShutdownSignalException(args);
                    };
                    var tag    = RC.IModelExensions.BasicConsume(channel, route, false, new RC.DefaultBasicConsumer(channel));
                    var result = RC.IModelExensions.BasicConsume(channel, route, false, tag, new RC.DefaultBasicConsumer(channel));
                    throw new Exception("Expected Exception, got: " + result);
                });
                throw new Exception("Expected AmqpIOException");
            }
            catch (RabbitIOException)
            {
            }

            template.ConvertAndSend(route, "message");
            Assert.True(latch.Wait(TimeSpan.FromSeconds(10)));
            var result = template.ReceiveAndConvert <string>(route);

            Assert.Equal("message", result);
            result = template.ReceiveAndConvert <string>(route);
            Assert.Null(result);
            admin.DeleteQueue(CF_INTEGRATION_TEST_QUEUE);
        }
Exemple #4
0
        public void SurviveAReconnect()
        {
            var myQueue = new Config.Queue("my-queue");
            var cf      = new RC.ConnectionFactory
            {
                Uri = new Uri("amqp://localhost")
            };

            var ccf = new CachingConnectionFactory(cf)
            {
                ChannelCacheSize       = 2,
                ChannelCheckoutTimeout = 2000
            };
            var admin = new RabbitAdmin(ccf);

            admin.DeclareQueue(myQueue);
            var template = new RabbitTemplate(ccf);

            CheckIt(template, 0, myQueue.ActualName);

            var i = 1;

            while (i < 45)
            {
                // While in this loop, stop and start the broker
                // The CCF should reconnect and the receives in
                // Checkit should stop throwing exceptions
                // The available permits should always be == 2.
                Thread.Sleep(2000);
                CheckIt(template, i++, myQueue.ActualName);
                var values = ccf._checkoutPermits.Values.GetEnumerator();
                values.MoveNext();
                var availablePermits = values.Current.CurrentCount;
                output.WriteLine("Permits after test: " + availablePermits);
                Assert.Equal(2, availablePermits);
            }
        }
        public void TestMultiEntities()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build());
            serviceCollection.AddRabbitServices();
            serviceCollection.AddRabbitAdmin();
            var e1 = new Config.DirectExchange("e1", false, true);

            serviceCollection.AddRabbitExchange(e1);
            var q1 = new Config.Queue("q1", false, false, true);

            serviceCollection.AddRabbitQueue(q1);
            var binding = BindingBuilder.Bind(q1).To(e1).With("k1");

            serviceCollection.AddRabbitBinding(binding);
            var es = new Declarables(
                "es",
                new DirectExchange("e2", false, true),
                new DirectExchange("e3", false, true));

            serviceCollection.AddSingleton(es);
            var qs = new Declarables(
                "qs",
                new Config.Queue("q2", false, false, true),
                new Config.Queue("q3", false, false, true));

            serviceCollection.AddSingleton(qs);
            var bs = new Declarables(
                "qs",
                new Binding("b1", "q2", DestinationType.QUEUE, "e2", "k2", null),
                new Binding("b2", "q3", DestinationType.QUEUE, "e3", "k3", null));

            serviceCollection.AddSingleton(bs);
            var ds = new Declarables(
                "ds",
                new DirectExchange("e4", false, true),
                new Queue("q4", false, false, true),
                new Binding("b3", "q4", DestinationType.QUEUE, "e4", "k4", null));

            serviceCollection.AddSingleton(ds);
            var provider = serviceCollection.BuildServiceProvider();
            var admin    = provider.GetRabbitAdmin() as RabbitAdmin;
            var template = admin.RabbitTemplate;

            template.ConvertAndSend("e1", "k1", "foo");
            template.ConvertAndSend("e2", "k2", "bar");
            template.ConvertAndSend("e3", "k3", "baz");
            template.ConvertAndSend("e4", "k4", "qux");
            Assert.Equal("foo", template.ReceiveAndConvert <string>("q1"));
            Assert.Equal("bar", template.ReceiveAndConvert <string>("q2"));
            Assert.Equal("baz", template.ReceiveAndConvert <string>("q3"));
            Assert.Equal("qux", template.ReceiveAndConvert <string>("q4"));
            admin.DeleteQueue("q1");
            admin.DeleteQueue("q2");
            admin.DeleteQueue("q3");
            admin.DeleteQueue("q4");
            admin.DeleteExchange("e1");
            admin.DeleteExchange("e2");
            admin.DeleteExchange("e3");
            admin.DeleteExchange("e4");

            var ctx = provider.GetService <IApplicationContext>();
            var mixedDeclarables = ctx.GetService <Declarables>("ds");

            Assert.NotNull(mixedDeclarables);
            var queues = mixedDeclarables.GetDeclarablesByType <IQueue>();

            Assert.Single(queues);
            Assert.Equal("q4", queues.Single().QueueName);
            var exchanges = mixedDeclarables.GetDeclarablesByType <IExchange>();

            Assert.Single(exchanges);
            Assert.Equal("e4", exchanges.Single().ExchangeName);
            var bindings = mixedDeclarables.GetDeclarablesByType <IBinding>();

            Assert.Single(bindings);
            Assert.Equal("q4", bindings.Single().Destination);
        }