예제 #1
0
        public void ListenerAndSenderShouldConnectWithSpecifiedCredentials()
        {
            // Arrange
            var input = new Person {
                FirstName = "Test", LastName = "Man"
            };
            var connection = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "******",
                Password = "******"
            };

            using (var listener = new Listener(connection, "demo2"))
                using (var service = new BlockingReceiver <Person>())
                {
                    var builder = new ContainerBuilder();
                    builder
                    .RegisterInstance <IReceive <Person> >(service);

                    using (var container = builder.Build())
                    {
                        listener.SubscribeEvents <Person>(container);
                        // Act
                        using (var sender = new Sender(connection, "demo2"))
                        {
                            sender.PublishEvent(input);
                        }

                        // Assert
                        service.Next();
                    }
                }
        }
예제 #2
0
        public void TestListenerProvidesSpecificSender()
        {
            using (var listener1 = new TestListener())
                using (var listener2 = new TestListener())
                    using (var service1 = new BlockingReceiver <int>())
                        using (var service2 = new BlockingReceiver <int>())
                        {
                            service1.SubscribeToEvents(listener1);
                            service2.SubscribeToEvents(listener2);

                            using (var sender = listener1.Sender())
                            {
                                sender.PublishEvent(3);
                            }

                            using (var sender = listener2.Sender())
                            {
                                sender.PublishEvent(4);
                            }


                            Assert.Equal(3, service1.Next());
                            Assert.Equal(4, service2.Next());
                        }
        }
        public void FrontEndServiceShouldRespondToPersonCreatedEvents()
        {
            // Arrange
            using (var context = new FrontEndContext(
                       new DbContextOptionsBuilder <FrontEndContext>().UseSqlite(@"Filename=.\test.db").Options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var builder = new ContainerBuilder();
                builder
                .RegisterType <FrontEndService>()
                .As <IReceive <PersonCreated> >();
                builder
                .RegisterInstance <IFrontEndContext>(context).ExternallyOwned();

                using (var waiter = new BlockingReceiver <PersonCreated>())
                    using (var container = builder.Build())
                        using (var sender = new TestSender())
                            using (var listener = sender.Listener())
                            {
                                listener.SubscribeEvents <PersonCreated>(container);
                                waiter.SubscribeToEvents(listener);

                                // Act
                                sender.PublishEvent(new PersonCreated {
                                });
                                waiter.Next();
                            }

                // Assert
                Assert.True(context.People.Any());
            }
        }
예제 #4
0
        public void SendAndReceiveShouldNotDependOnClrTypes()
        {
            // Arrange
            var input = new ef_demo.Person {
                FirstName = "Test", LastName = "Man"
            };

            using (var listener = new TestListener())
                using (var service = new BlockingReceiver <Person>())
                {
                    service.SubscribeToEvents(listener);

                    // Act
                    using (var sender = listener.Sender())
                    {
                        sender.PublishEvent(input);
                    }

                    // Assert
                    var result = service.Next();

                    Assert.Equal(input.FirstName, result.FirstName);
                    Assert.Equal(input.LastName, result.LastName);
                }
        }
예제 #5
0
 public void TimeoutWhenMessageDoesNotArrive()
 {
     using (var service = new BlockingReceiver <int>())
     {
         Assert.Throws <TimeoutException>(() => 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 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 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)));
                        }
                }
        }
예제 #10
0
        public void ListenerDisposesDependenciesResolvedToCreateInstancesOnExecute()
        {
            // Arrange
            var dependency = Substitute.For <IDependency, IDisposable>();

            var builder = new ContainerBuilder();

            builder
            .Register(c => dependency);
            builder
            .RegisterReceiverFor <ReceiverWithDependency, int>();

            using (var listener = new TestListener())
                using (var sender = listener.Sender())
                    using (var container = builder.Build())
                        using (var waiter = new BlockingReceiver <int>())
                        {
                            listener.SubscribeEvents <int>(container);
                            dependency.ClearReceivedCalls();

                            waiter.SubscribeToEvents(listener);

                            // Act
                            sender.PublishEvent(4);
                            waiter.Next();
                        }

            // Assert
            ((IDisposable)dependency).Received(1).Dispose();
        }
예제 #11
0
        public void PersonCreatedService_receives_new_Person_and_Saves_to_Database()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <DeelnemerContext>()
                          .UseSqlServer(@"Server=.\SQLEXPRESS;Database=JeroenDonaldDuckDeelnemers;Trusted_Connection=true")
                          .Options;



            using (var context = new DeelnemerContext(options))
                using (var tx = context.Database.BeginTransaction())
                    using (var listener = new TestListener())
                    {
                        context.Database.EnsureCreated();

                        var service = new BlockingReceiver <PersonCreated>();
                        service.SubscribeToEvents(listener);

                        //Act
                        service.Execute(new PersonCreated()
                        {
                            FirstName = "Klaas-Teake"
                        });


                        context.SaveChanges();

                        //Assert
                        var result = context.Deelnemers.Where(p => p.FirstName == "Klaas-Teake").First();
                        Assert.Equal("Klaas-Teake", result.FirstName);
                    }
        }
        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);
                    }
            }
        }
예제 #13
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);
                        }
        }
예제 #14
0
        public void ListenerReceivesTwoMessagesOfDifferentType()
        {
            using (var listener = new TestListener())
                using (var service1 = new BlockingReceiver <Person>())
                    using (var service2 = new BlockingReceiver <string>())
                    {
                        service1.SubscribeToEvents(listener);
                        service2.SubscribeToEvents(listener);

                        using (var sender = listener.Sender())
                        {
                            sender.PublishEvent(new Person {
                                FirstName = "first"
                            });
                            sender.PublishEvent("just simple text");

                            service1.Next();
                            service2.Next();
                        }
                    }
        }
        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)));
                    }
            }
        }
예제 #16
0
        public void PublishedMessageShouldBeDeliveredToSubscribedReceiversFromBothListeners()
        {
            // Arrange
            using (var sender = new TestSender())
                using (var listener1 = sender.Listener())
                    using (var listener2 = sender.Listener())
                        using (var service1 = new BlockingReceiver <Person>())
                            using (var service2 = new BlockingReceiver <Person>())
                            {
                                service1.SubscribeToEvents(listener1);
                                service2.SubscribeToEvents(listener2);

                                // Act
                                sender.PublishEvent(new Person {
                                    FirstName = "first"
                                });

                                // Assert
                                service1.Next();
                                service2.Next();
                            }
        }
예제 #17
0
        public void WaitForNextValue()
        {
            var items = Enumerable.Range(0, 10).ToList();

            using (var service = new BlockingReceiver <int>())
            {
                var task = Task.Run(() =>
                {
                    foreach (var item in items)
                    {
                        service.Execute(item);
                        Thread.Sleep(100);
                    }
                });

                foreach (var item in items)
                {
                    var result = service.Next();
                    Assert.Equal(item, result);
                }
            }
        }
예제 #18
0
        public void SendAndReceiveShouldDependOnClassName()
        {
            // Arrange
            var input = new Person {
                FirstName = "Test", LastName = "Man"
            };

            using (var listener = new TestListener())
                using (var wait = new ManualResetEvent(false))
                    using (var service = new BlockingReceiver <SomethingUnrelated>())
                    {
                        service.SubscribeToEvents(listener);

                        // Act
                        using (var sender = listener.Sender())
                        {
                            sender.PublishEvent(input);
                        }

                        // Assert
                        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);
                    }
        }
예제 #20
0
        public void PublishedMessageShouldBeReceivedBySubscribedReceiver()
        {
            // Arrange
            using (var listener = new TestListener())
                using (var receiver = new BlockingReceiver <Person>())
                {
                    receiver.SubscribeToEvents(listener);

                    // Act
                    var input = new Person {
                        FirstName = "Test", LastName = "Man"
                    };
                    using (var sender = listener.Sender())
                    {
                        sender.PublishEvent(input);
                    }

                    // Assert
                    var result = receiver.Next();
                    Assert.Equal(input.FirstName, result.FirstName);
                    Assert.Equal(input.LastName, result.LastName);
                }
        }