public void WhenPublishingEvent_ThenInvokesCompatibleHandler() { var handler = new Mock<IEventHandler<TestEvent>>(); var e = new ManualResetEventSlim(); handler.Setup(x => x.Handle(It.IsAny<TestEvent>())) .Callback(() => e.Set()); var bus = new MemoryEventBus(handler.Object); bus.Publish(new TestEvent()); e.Wait(3000); handler.Verify(x => x.Handle(It.IsAny<TestEvent>())); }
public void WhenPublishingMultipleEvents_ThenInvokesCompatibleHandlerMultipleTimes() { var handler = new Mock<IEventHandler<TestEvent>>(); var e = new ManualResetEventSlim(); var called = 0; handler.Setup(x => x.Handle(It.IsAny<TestEvent>())) .Callback(() => { if (Interlocked.Increment(ref called) == 4) e.Set(); }); var bus = new MemoryEventBus(handler.Object); bus.Publish(new[] { new TestEvent(), new TestEvent(), new TestEvent(), new TestEvent() }); e.Wait(10000); Assert.Equal(4, called); }
public void WhenPublishingEvent_ThenDoesNotInvokeIncompatibleHandler() { var compatibleHandler = new Mock<IEventHandler<TestEvent>>(); var incompatibleHandler = new Mock<IEventHandler<FooEvent>>(); var e = new ManualResetEventSlim(); compatibleHandler.Setup(x => x.Handle(It.IsAny<TestEvent>())) .Callback(() => e.Set()); var bus = new MemoryEventBus(incompatibleHandler.Object, compatibleHandler.Object); bus.Publish(new TestEvent()); e.Wait(3000); incompatibleHandler.Verify(x => x.Handle(It.IsAny<FooEvent>()), Times.Never()); }