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 OneHandler_OneEvent_OneCatchAll() { var bus = new DefaultEventBus(); var logStream = new List <LogEvent>(); var someStream = new List <SomeEvent>(); 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 1 catch-all result"); 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!"); var catchAllHandler = new CatchAllHandler(logStream, someStream); bus.RegisterCatchAllHandler(catchAllHandler); var ev2 = new SomeEvent(100); var res2 = bus.Publish(ev2); Assert.IsTrue(someStream.Count == 1, "There must be one some event in stream"); Assert.IsTrue(someStream[0].Number == ev2.Number, "some event number wrong"); Assert.IsNotNull(res2, "Event handler was not found"); Assert.IsNotNull(res2.CatchAllResults, "Catch-all handlers must not be null"); Assert.IsTrue(res2.CatchAllResults.Length == 1, "There must be 1 catch-all result"); Assert.IsTrue(res2.StartedAt <= res2.FinishedAt, "StartedAt > FinishedAt!"); Assert.IsTrue(res2.Duration.Ticks >= 0, "Duration is negative"); Assert.IsNotNull(res2.HandlerResults, "Handler result is null"); Assert.IsTrue(res2.HandlerResults.Length == 0, "Handler result is not added"); Assert.IsTrue(res2.CatchAllResults[0].Handler.Equals(catchAllHandler), "Catch-all Handler in result is not equal to the actual handler!"); bus.Dispose(); }