public void ReceiverWithExceptionLeavesCommandInQueueToBeRedelivered()
        {
            using (var sender = new TestSender())
            {
                sender.PublishCommand(3);

                using (var wait = new ManualResetEvent(false))
                {
                    var service = Substitute.For <IReceive <int> >();
                    service.When(_ => _.Execute(Arg.Any <int>())).Do(_ =>
                    {
                        wait.Set();
                        throw new NotImplementedException();
                    });

                    var builder = new ContainerBuilder();
                    builder.RegisterInstance(service);
                    using (var container = builder.Build())
                        using (var listener = sender.Listener())
                        {
                            listener.SubscribeCommands <int>(container);
                            Assert.True(wait.WaitOne(TimeSpan.FromSeconds(5)));

                            service.Received(1).Execute(3);
                        }
                }

                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Equal(3, service.Next());
                    }
            }
        }
        public void ListenerAndSenderKeepCommandQueues()
        {
            var connection = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "******",
                Password = "******"
            };
            var ns = "test";

            using (var sender = new Sender(connection, ns))
            {
                sender.PublishCommand(6);
                using (var service = new BlockingReceiver <int>())
                    using (var listener = new Listener(connection, ns))
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Equal(6, service.Next());
                    }

                sender.PublishCommand(3);
                Thread.Sleep(TimeSpan.FromSeconds(5));

                using (var service = new BlockingReceiver <int>())
                    using (var listener = new Listener(connection, ns))
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Equal(3, service.Next());
                    }
            }
        }
        public void TestListenerAndSenderRemovesCommandQueues()
        {
            var timeout = TimeSpan.FromSeconds(1);

            using (var sender = new TestSender(timeout))
            {
                sender.PublishCommand(6);
                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Equal(6, service.Next());
                    }


                sender.PublishCommand(3);
                Thread.Sleep(timeout);

                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Throws <TimeoutException>(() => service.Next(TimeSpan.FromSeconds(1)));
                    }
            }
        }
        public void ChannelBaseUsesPrivateQueuesPerNamespace()
        {
            using (var sender1 = new TestSender())
                using (var sender2 = new TestSender())
                {
                    sender1.PublishCommand(3);
                    sender2.PublishCommand(4);

                    using (var listener = sender1.Listener())
                        using (var service = new BlockingReceiver <int>())
                        {
                            service.SubscribeToCommand(listener);
                            Assert.Equal(3, service.Next());
                            Assert.Throws <TimeoutException>(() => service.Next(TimeSpan.FromSeconds(1)));
                        }

                    using (var listener = sender2.Listener())
                        using (var service = new BlockingReceiver <int>())
                        {
                            service.SubscribeToCommand(listener);
                            Assert.Equal(4, service.Next());
                            Assert.Throws <TimeoutException>(() => service.Next(TimeSpan.FromSeconds(1)));
                        }
                }
        }
        public void CommandsAreDeliveredAlsoWhenPublishedBeforeListenerSubscribed()
        {
            using (var sender = new TestSender())
            {
                sender.PublishCommand(3);

                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        var result = service.Next();

                        Assert.Equal(3, result);
                    }
            }
        }
예제 #6
0
        public void IntegrationTestOnPostRequest()
        {
            using (var service = new BlockingReceiver <CreatePerson>())
                using (var listener = new Listener(new ConnectionFactory(), "mvc-demo"))
                    using (var server = StartTestServer())
                        using (var client = server.CreateClient())
                        {
                            service.SubscribeToCommand(listener);

                            var result = client.PostAsync("People/Create", new StringContent(JsonConvert.SerializeObject(new { FirstName = "first", LastName = "last" }), Encoding.UTF8, "application/json")).Result;
                            Assert.Equal(HttpStatusCode.Redirect, result.StatusCode);

                            var command = service.Next();
                            Assert.Equal("first", command.FirstName);
                            Assert.Equal("last", command.LastName);
                        }
        }
        public void ReceivedCommandIsNotDeliveredTwice()
        {
            using (var sender = new TestSender())
            {
                sender.PublishCommand(3);

                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Equal(3, service.Next());
                    }

                using (var service = new BlockingReceiver <int>())
                    using (var listener = sender.Listener())
                    {
                        service.SubscribeToCommand(listener);
                        Assert.Throws <TimeoutException>(() => service.Next(TimeSpan.FromSeconds(1)));
                    }
            }
        }
        public void ListenerRaisesEventsOnReceivingCommands()
        {
            // Arrange
            using (var service = new BlockingReceiver <int>())
                using (var listener = new TestListener())
                    using (var sender = listener.Sender())
                    {
                        var messages = new List <ReceivedEventArgs>();
                        listener.Received += (o, e) => messages.Add(e);

                        service.SubscribeToCommand(listener);

                        // Act
                        sender.PublishCommand(3);
                        service.Next();

                        // Assert
                        var message = messages.Single();
                        Assert.Equal(service.GetType(), message.HandledBy);
                        Assert.Equal("Int32", message.Topic);
                        Assert.Equal("3", message.Message);
                    }
        }