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()); }
static void Main(string[] args) { // 构造事件总线 IEventBus eventBus = new DefaultEventBus(); // 发布消息 eventBus.Publish(new ConsoleStarted(1)); Console.ReadLine(); }
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 ItShouldHandleEventOnce() { IEventBus eventBus = new DefaultEventBus(); var handlerMock = new TestDomainEventHandler(); eventBus.Register(handlerMock); eventBus.Raise(new TestEvent()); Thread.Sleep(5000); Assert.Equal(handlerMock.HandleTimes, 1); }
public void Should_Send_Events_Even_When_Outbound_Chain_Is_Null() { // create an event var ev = new {Name = "test"}; // mock an envelope bus var envBusMock = new Mock<IEnvelopeBus>(); DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, null, null); bus.Publish(ev); // make sure that the envelope bus was given an envelope envBusMock.Verify(envBus => envBus.Send(It.IsAny<Envelope>()), Times.Once()); }
public void Register_Unregister() { var bus = new DefaultEventBus(); var handler = new CatchAllHandler(null, null); bus.RegisterCatchAllHandler(handler); var catchAllHandlers = bus.GetCatchAllHandlers().ToArray(); Assert.IsTrue(catchAllHandlers.Length == 1, "One catch-all"); Assert.IsTrue(catchAllHandlers[0].Equals(handler), "Types ok"); bus.UnregisterCatchAllHandler(handler); catchAllHandlers = bus.GetCatchAllHandlers().ToArray(); Assert.IsTrue(catchAllHandlers.Length == 0, "No catch-all"); }
public void Should_Throw_An_Exception_When_Payload_Is_Empty() { // create an empty event var ev = new {Name = "test"}; // mock up an envelope bus var envBusMock = new Mock<IEnvelopeBus>(); // create the event bus we're testing DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, new List<IMessageProcessor>(), new List<IMessageProcessor>()); // send the event through the bus bus.Publish(ev); // make sure that the envelope bus was never given an envelope envBusMock.Verify(envBus => envBus.Send(It.IsAny<Envelope>()), Times.Never()); }
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 Should_Send_Events_Even_When_Outbound_Chain_Is_Null() { // create an event var ev = new { Name = "test" }; // mock an envelope bus var envBusMock = new Mock <IEnvelopeBus>(); DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, null, null); bus.Publish(ev); // make sure that the envelope bus was given an envelope envBusMock.Verify(envBus => envBus.Send(It.IsAny <Envelope>()), Times.Once()); }
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(); }
public void Should_Receive_Events_Even_When_Inbound_Chain_Is_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>(); // 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); bus.InboundChain = null; // simulate an incoming event bus.InterceptEvent(handlerMock.Object, 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 Should_Throw_An_Exception_When_Payload_Is_Empty() { // create an empty event var ev = new { Name = "test" }; // mock up an envelope bus var envBusMock = new Mock <IEnvelopeBus>(); // create the event bus we're testing DefaultEventBus bus = new DefaultEventBus(envBusMock.Object, new List <IMessageProcessor>(), new List <IMessageProcessor>()); // send the event through the bus bus.Publish(ev); // make sure that the envelope bus was never given an envelope envBusMock.Verify(envBus => envBus.Send(It.IsAny <Envelope>()), Times.Never()); }
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); }
private DomainEnvironment() { ImmediateEventBus = new DefaultEventBus(); PostCommitEventBus = new DefaultEventBus(); }