public void TestReceive()
        {
            var template = provider.GetRabbitTemplate();
            var foo      = MakeAFoo();
            var pp       = new TestPostProcessor();

            template.ConvertAndSend(TEST_QUEUE2, foo, pp);
            var result = template.ReceiveAndConvert <Foo <Bar <Baz, Qux> > >(10000);

            Assert.NotNull(result);
        }
        public void TestReceiveNoWait()
        {
            var template = provider.GetRabbitTemplate();
            var foo      = MakeAFoo();
            var pp       = new TestPostProcessor();

            template.ConvertAndSend(TEST_QUEUE2, foo, pp);
            var result = template.ReceiveAndConvert <Foo <Bar <Baz, Qux> > >();

            var n = 0;

            while (n++ < 100 && foo == null)
            {
                Thread.Sleep(100);
                result = template.ReceiveAndConvert <Foo <Bar <Baz, Qux> > >();
            }

            Assert.NotNull(result);
        }
        public async Task TestDeclareDelayedExchange()
        {
            provider = services.BuildServiceProvider();
            var rabbitAdmin = provider.GetRabbitAdmin();
            var exchange    = new DirectExchange("test.delayed.exchange")
            {
                IsDelayed = true
            };
            var queue        = new Queue(Guid.NewGuid().ToString(), true, false, false);
            var exchangeName = exchange.ExchangeName;
            var binding      = new Binding("baz", queue.QueueName, DestinationType.QUEUE, exchangeName, queue.QueueName, null);

            try
            {
                rabbitAdmin.DeclareExchange(exchange);
            }
            catch (RabbitIOException e)
            {
                if (RabbitUtils.IsExchangeDeclarationFailure(e))
                {
                    var inner = e.InnerException;
                    if (inner.Message.Contains("exchange type 'x-delayed-message'"))
                    {
                        return; // Broker doesn't support?
                    }
                }

                throw;
            }

            rabbitAdmin.DeclareQueue(queue);
            rabbitAdmin.DeclareBinding(binding);
            var cf       = provider.GetRabbitConnectionFactory();
            var context  = provider.GetApplicationContext();
            var pp       = new TestPostProcessor();
            var template = new RabbitTemplate(cf)
            {
                ReceiveTimeout = 10000
            };

            template.ConvertAndSend(exchangeName, queue.QueueName, "foo", pp);
            var headers = RabbitHeaderAccessor.GetMutableAccessor(new MessageHeaders());

            headers.Delay = 500;
            var send = MessageBuilder.WithPayload(Encoding.UTF8.GetBytes("foo")).SetHeaders(headers).Build();

            template.Send(exchangeName, queue.QueueName, send);
            var t1       = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var received = template.Receive(queue.QueueName);

            Assert.NotNull(received);
            var delay = received.Headers.ReceivedDelay();

            Assert.NotNull(delay);
            Assert.Equal(500, delay.Value);
            received = template.Receive(queue.QueueName);
            Assert.NotNull(received);
            delay = received.Headers.ReceivedDelay();
            Assert.NotNull(delay);
            Assert.Equal(1000, delay.Value);
            var t2  = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var dif = t2 - t1;

            Assert.InRange(dif, 950, 1250);
            var config = await GetExchange(exchangeName);

            Assert.Equal("direct", config.GetValue <string>("x-delayed-type"));
            Assert.Equal("x-delayed-message", config.GetValue <string>("type"));
        }