Exemplo n.º 1
0
        public void TopologyExchangeConfig(LinkExchangeType exchangeType, bool durable, bool autoDelete,
                                           string alternateExhange, bool delayed)
        {
            using (var rabbitConneciton = TestsOptions.GetConnection())
            {
                var rabbitModel = rabbitConneciton.CreateModel();

                var exchangeName = TestsOptions.TestExchangeName;

                using (var link = TestsOptions.GetLinkBuilder().Build())
                {
                    Assert.ThrowsAny <Exception>(() => { rabbitModel.ExchangeDeclarePassive(exchangeName); });

                    rabbitModel = rabbitConneciton.CreateModel();

                    link.Topology
                    .Handler(async cfg =>
                    {
                        var e1 =
                            await
                            cfg.ExchangeDeclare(exchangeName, exchangeType, durable, autoDelete,
                                                alternateExhange,
                                                delayed);
                        var e2 =
                            await
                            cfg.ExchangeDeclare(exchangeName + "-second", exchangeType, durable, autoDelete,
                                                alternateExhange, delayed);

                        await cfg.ExchangeDeclarePassive(exchangeName);

                        await cfg.ExchangeDeclareDefault();

                        await cfg.Bind(e2, e1);
                        await cfg.Bind(e2, e1, "test");
                    })
                    .WaitAsync()
                    .GetAwaiter()
                    .GetResult();

                    rabbitModel.ExchangeDeclarePassive(exchangeName);
                    rabbitModel.ExchangeDeclarePassive(exchangeName + "-second");

                    link.Topology
                    .Handler(async cfg =>
                    {
                        var e1 = await cfg.ExchangeDeclarePassive(exchangeName);
                        var e2 = await cfg.ExchangeDeclarePassive(exchangeName + "-second");

                        await cfg.ExchangeDelete(e1);
                        await cfg.ExchangeDelete(e2);
                    })
                    .WaitAsync()
                    .GetAwaiter()
                    .GetResult();

                    Assert.ThrowsAny <Exception>(() => { rabbitModel.ExchangeDeclarePassive(exchangeName); });
                }
            }
        }
Exemplo n.º 2
0
        public void PublishTest()
        {
            var exchangeName = TestsOptions.TestExchangeName;
            var queueName    = TestsOptions.TestQueueName;

            using (var link = TestsOptions.GetLinkBuilder().Build())
            {
                try
                {
                    using (var producer = link.Producer
                                          .Exchange(async cfg =>
                    {
                        var ex = await cfg.ExchangeDeclare(exchangeName, LinkExchangeType.Fanout, autoDelete: true);
                        var q = await cfg.QueueDeclare(queueName, false, false, true,
                                                       expires: TimeSpan.FromMinutes(1));

                        await cfg.Bind(q, ex);

                        return(ex);
                    })
                                          .ConfirmsMode(false)
                                          .Build()
                           )
                    {
                        producer.PublishAsync(new LinkPublishMessage <byte[]>(new byte[0]))
                        .GetAwaiter()
                        .GetResult();
                    }
                }
                finally
                {
                    link.Topology
                    .Handler(async cfg =>
                    {
                        try
                        {
                            var ex = await cfg.ExchangeDeclarePassive(exchangeName);

                            var q = await cfg.QueueDeclarePassive(queueName);

                            await cfg.ExchangeDelete(ex);
                            await cfg.QueueDelete(q);
                        }
                        catch
                        {
                            // No-op
                        }
                    })
                    .WaitAsync()
                    .GetAwaiter()
                    .GetResult();
                }
            }
        }