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 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()); } }
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); } }
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(); }
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 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(); } }
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); } }
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 ListenerRaisesEventsOnReceivingMessages() { // Arrange using (var listener = new TestListener()) using (var sender = listener.Sender()) using (var service = new BlockingReceiver <int>()) { var messages = new List <ReceivedEventArgs>(); listener.Received += (o, e) => messages.Add(e); service.SubscribeToEvents(listener); // Act sender.PublishEvent(3); service.Next(); // Assert var message = messages.Single(); Assert.Equal(service.GetType(), message.HandledBy); Assert.Equal("Int32", message.Topic); Assert.Equal("3", message.Message); } }