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 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 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 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 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_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); }