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_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)))
            {
                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_message_receivedthen_calls_process_message()
        {
            var waiter = new ManualResetEventSlim();
            var sender = new TopicSender(this.Settings, this.Topic);
            var processor = new FakeProcessor(
                waiter,
                new SubscriptionReceiver(this.Settings, this.Topic, this.Subscription),
                new JsonTextSerializer());

            processor.Start();

            var stream = new MemoryStream();
            new JsonTextSerializer().Serialize(new StreamWriter(stream), "Foo");
            stream.Position = 0;
            sender.SendAsync(() => new BrokeredMessage(stream, true));

            waiter.Wait(5000);

            Assert.NotNull(processor.Payload);
        }
        public void when_processing_throws_then_sends_message_to_dead_letter()
        {
            var failCount = 0;
            var waiter = new ManualResetEventSlim();
            var sender = new TopicSender(this.Settings, this.Topic);
            var processor = new Mock<MessageProcessor>(
                new SubscriptionReceiver(this.Settings, this.Topic, this.Subscription), new JsonTextSerializer()) { CallBase = true };

            processor.Protected()
                .Setup("ProcessMessage", ItExpr.IsAny<string>(), ItExpr.IsAny<object>(), ItExpr.IsAny<string>(), ItExpr.IsAny<string>())
                .Callback(() =>
                {
                    failCount++;
                    if (failCount == 5)
                        waiter.Set();

                    throw new ArgumentException();
                });

            processor.Object.Start();

            var stream = new MemoryStream();
            new JsonTextSerializer().Serialize(new StreamWriter(stream), "Foo");
            stream.Position = 0;
            sender.SendAsync(() => new BrokeredMessage(stream, true));

            waiter.Wait(5000);

            var deadReceiver = this.Settings.CreateMessageReceiver(this.Topic, this.Subscription);

            var deadMessage = deadReceiver.Receive(TimeSpan.FromSeconds(5));

            processor.Object.Dispose();

            Assert.NotNull(deadMessage);
            var data = new JsonTextSerializer().Deserialize(new StreamReader(deadMessage.GetBody<Stream>()));

            Assert.Equal("Foo", (string)data);
        }
        public void when_message_fails_to_deserialize_then_dead_letters_message()
        {
            var waiter = new ManualResetEventSlim();
            var sender = new TopicSender(this.Settings, this.Topic);
            var processor = new FakeProcessor(
                waiter,
                new SubscriptionReceiver(this.Settings, this.Topic, this.Subscription),
                new JsonTextSerializer());

            processor.Start();

            var data = new JsonTextSerializer().Serialize(new Data());
            data = data.Replace(typeof(Data).FullName, "Some.TypeName.Cannot.Resolve");
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
            stream.Position = 0;

            sender.SendAsync(() => new BrokeredMessage(stream, true));

            waiter.Wait(5000);

            var deadReceiver = this.Settings.CreateMessageReceiver(this.Topic, this.Subscription);
            var deadMessage = deadReceiver.Receive(TimeSpan.FromSeconds(5));

            processor.Dispose();

            Assert.NotNull(deadMessage);
            var payload = new StreamReader(deadMessage.GetBody<Stream>()).ReadToEnd();

            Assert.Contains("Some.TypeName.Cannot.Resolve", payload);
        }
        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);
        }