public void MultipleHandlers_OneEvent() { var bus = new DefaultEventBus(); var logStream1 = new List <LogEvent>(); var logStream2 = new List <LogEvent>(); var handler1 = new LogEventHandler1(logStream1); var handler2 = new LogEventHandler2(logStream2); bus.Subscribe <LogEvent, LogEventHandler1>(handler1); bus.Subscribe <LogEvent, LogEventHandler2>(handler2); var ev = new LogEvent("log"); var res = bus.Publish(ev); Assert.IsTrue(logStream1.Count == 1, "There must be one log item in stream1"); Assert.IsTrue(logStream1[0].Text.Equals(ev.Text, StringComparison.Ordinal), "Log item's text aren't equal in stream1"); Assert.IsTrue(logStream2.Count == 1, "There must be one log item in stream2"); Assert.IsTrue(logStream2[0].Text.Equals(ev.Text, StringComparison.Ordinal), "Log item's text aren't equal in stream2"); Assert.IsNotNull(res, "Event handler was not found"); Assert.IsNotNull(res.CatchAllResults, "Catch-all handlers must not be null"); Assert.IsTrue(res.CatchAllResults.Length == 0, "There must be no catch-all results"); Assert.IsTrue(res.StartedAt <= res.FinishedAt, "StartedAt > FinishedAt!"); Assert.IsTrue(res.Duration.Ticks >= 0, "Duration is negative"); Assert.IsNotNull(res.HandlerResults, "Handler result is null"); Assert.IsTrue(res.HandlerResults.Length == 2, "Handler result is not added"); Assert.IsTrue(res.HandlerResults.Any(a => a.Handler.Equals(handler1)), "handler1 does not exist in result"); Assert.IsTrue(res.HandlerResults.Any(a => a.Handler.Equals(handler2)), "handler2 does not exist in result"); bus.Dispose(); }
public void Should_Receive_Events_Even_When_Inbound_Chain_Is_Null() { IRegistration registration = null; // create an envelope with content var env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") }; // mock up an envelope bus var envBusMock = new Mock <IEnvelopeBus>(); envBusMock.Setup(envBus => envBus.Register(It.IsAny <IRegistration>())) .Callback <IRegistration>(r => { registration = r; }); // mock up an IEventHandler var handlerMock = new Mock <IEventHandler>(); // the event bus we're testing - explicity set inbound chain to null DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, null, null); //wire up handler bus.Subscribe(handlerMock.Object); //Simulate received event. registration.Handle(env); // make sure the handler's Handle method was called handlerMock.Verify(handler => handler.Handle(It.IsAny <object>(), It.IsAny <IDictionary <string, string> >()), Times.Once()); }
public void Should_Receive_Events_Even_When_Inbound_Chain_Is_Null() { IRegistration registration = null; // create an envelope with content var env = new Envelope() {Payload = Encoding.UTF8.GetBytes("Test")}; // mock up an envelope bus var envBusMock = new Mock<IEnvelopeBus>(); envBusMock.Setup(envBus => envBus.Register(It.IsAny<IRegistration>())) .Callback<IRegistration>(r => { registration = r; }); // mock up an IEventHandler var handlerMock = new Mock<IEventHandler>(); // the event bus we're testing - explicity set inbound chain to null DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, null, null); //wire up handler bus.Subscribe(handlerMock.Object); //Simulate received event. registration.Handle(env); // make sure the handler's Handle method was called handlerMock.Verify(handler => handler.Handle(It.IsAny<object>(), It.IsAny<IDictionary<string, string>>()), Times.Once()); }
public void Showcase_WithCatchAll() { // 1- create an event bus var bus = new DefaultEventBus(); // 2- subscribe to SimpleMessage event via PrintMessageRaw event handler bus.Subscribe <SimpleMessage, PrintMessageRaw>(new PrintMessageRaw()); // 3- subscribe to SimpleMessage event via PrintMessagePretty event handler bus.Subscribe <SimpleMessage, PrintMessagePretty>(new PrintMessagePretty()); // 4- register a catch-all event handler bus.RegisterCatchAllHandler(new CatchAllMessages()); // 5- create the event var message = new SimpleMessage("a simple message"); // 6- publish the event bus.Publish(message); }
public void Showcase() { // 1- create an event bus var bus = new DefaultEventBus(); // 2- create services var userService = new UserService(bus); var resumeService = new ResumeService(); var storeService = new StoreService(); // 3- subscribe bus.Subscribe <MaritalStatusChanged, ResumeService.MaritalStatusChangedHandler>( new ResumeService.MaritalStatusChangedHandler(resumeService)); bus.Subscribe <MaritalStatusChanged, StoreService.MaritalStatusChangedHandler>( new StoreService.MaritalStatusChangedHandler(storeService)); // 4- someone got married userService.GotMarried("1"); }
public void Showcase_WithoutCatchAll() { // 1- create an event bus var bus = new DefaultEventBus(); // 2- subscribe to SimpleMessage event via PrintMessageRaw event handler bus.Subscribe <SimpleMessage, PrintMessageRaw>(new PrintMessageRaw()); // 3- subscribe to SimpleMessage event via PrintMessagePretty event handler var x = new PrintMessagePretty(); bus.Subscribe <SimpleMessage, PrintMessagePretty>(x); // 4- remember subscribing to a message with the same handler instance, has no effect! bus.Subscribe <SimpleMessage, PrintMessagePretty>(x); // 5- create the event var message = new SimpleMessage("a simple message"); // 6- publish the event bus.Publish(message); }
public void Subscribe_Unsubscribe() { var bus = new DefaultEventBus(); var handler = new LogEventHandler1(null); bus.Subscribe <LogEvent, LogEventHandler1>(handler); var handlers = bus.GetHandlers <LogEvent>().ToArray(); Assert.IsTrue(bus.GetCatchAllHandlers().Count() == 0, "No catch-all"); Assert.IsTrue(handlers.Length == 1, "one handler"); Assert.IsTrue(handlers[0].Equals(handler), "types ok"); bus.Unsubscribe <LogEvent, LogEventHandler1>(handler); handlers = bus.GetHandlers <LogEvent>().ToArray(); Assert.IsTrue(bus.GetCatchAllHandlers().Count() == 0, "No catch-all again"); Assert.IsTrue(handlers.Length == 0, "no handler"); }
public void OneHandler_OneEvent() { var bus = new DefaultEventBus(); var logStream = new List <LogEvent>(); var handler = new LogEventHandler1(logStream); bus.Subscribe <LogEvent, LogEventHandler1>(handler); var ev = new LogEvent("log-1"); var res = bus.Publish(ev); Assert.IsTrue(logStream.Count == 1, "There must be one log item in stream"); Assert.IsTrue(logStream[0].Text.Equals(ev.Text, StringComparison.Ordinal), "Log item's text aren't equal"); Assert.IsNotNull(res, "Event handler was not found"); Assert.IsNotNull(res.CatchAllResults, "Catch-all handlers must not be null"); Assert.IsTrue(res.CatchAllResults.Length == 0, "There must be no catch-all results"); Assert.IsTrue(res.StartedAt <= res.FinishedAt, "StartedAt > FinishedAt!"); Assert.IsTrue(res.Duration.Ticks >= 0, "Duration is negative"); Assert.IsNotNull(res.HandlerResults, "Handler result is null"); Assert.IsTrue(res.HandlerResults.Length == 1, "Handler result is not added"); Assert.IsTrue(res.HandlerResults[0].Handler.Equals(handler), "Handler in result is not equal to the actual handler!"); bus.Dispose(); }