public void when_gets_transient_error_on_receive_then_retries()
        {
            var sender = new TopicSender(this.settings, this.topic);
            Data data = new Data { Id = Guid.NewGuid(), Title = "Foo" };
            Data received = null;
            using (var receiver = new TestableSubscriptionReceiver(this.settings, this.topic, this.subscription, new Incremental(3, TimeSpan.Zero, TimeSpan.Zero), new Incremental(0, TimeSpan.Zero, TimeSpan.Zero)))
            {
                var attempt = 0;
                var currentDelegate = receiver.DoReceiveMessageDelegate;
                receiver.DoReceiveMessageDelegate =
                    () =>
                    {
                        if (attempt++ < 1) { throw new TimeoutException(); }
                        return currentDelegate();
                    };

                var signal = new ManualResetEventSlim();

                receiver.MessageReceived += (o, e) =>
                {
                    received = e.Message.GetBody<Data>();
                    signal.Set();
                };

                receiver.Start();

                sender.SendAsync(() => new BrokeredMessage(data));

                Assert.True(signal.Wait(TimeSpan.FromSeconds(10)), "Test timed out");
            }

            Assert.NotNull(received);
            Assert.Equal(data.Id, received.Id);
            Assert.Equal(data.Title, received.Title);
        }
        public void when_sending_message_then_can_receive_it()
        {
            var sender = new TopicSender(this.Settings, this.Topic);
            Data data = new Data { Id = Guid.NewGuid(), Title = "Foo" };
            Data received = null;
            using (var receiver = new SubscriptionReceiver(this.Settings, this.Topic, this.Subscription))
            {
                var signal = new ManualResetEventSlim();

                receiver.Start(
                    m =>
                    {
                        received = m.GetBody<Data>();
                        signal.Set();
                        return MessageReleaseAction.CompleteMessage;
                    });

                sender.SendAsync(() => new BrokeredMessage(data));

                signal.Wait();
            }

            Assert.NotNull(received);
            Assert.Equal(data.Id, received.Id);
            Assert.Equal(data.Title, received.Title);
        }
        public void when_gets_transient_error_several_times_on_receive_then_retries_until_failure()
        {
            var attempt = 0;
            var sender = new TopicSender(this.settings, this.topic);
            Data data = new Data { Id = Guid.NewGuid(), Title = "Foo" };
            Data received = null;
            using (var receiver = new TestableSubscriptionReceiver(this.settings, this.topic, this.subscription, new Incremental(3, TimeSpan.Zero, TimeSpan.Zero), new Incremental(0, TimeSpan.Zero, TimeSpan.Zero)))
            {
                var signal = new ManualResetEventSlim();

                receiver.DoReceiveMessageDelegate =
                    () =>
                    {
                        if (attempt++ == 3) { signal.Set(); }
                        throw new TimeoutException();
                    };

                receiver.MessageReceived += (o, e) =>
                {
                    received = e.Message.GetBody<Data>();
                };

                receiver.Start();

                sender.SendAsync(() => new BrokeredMessage(data));

                Assert.True(signal.Wait(TimeSpan.FromSeconds(10)), "Test timed out");
                Thread.Sleep(TimeSpan.FromMilliseconds(500));
            }

            Thread.Sleep(TimeSpan.FromSeconds(1));
            Assert.Null(received);
        }