Beispiel #1
0
        public void TestDispatch()
        {
            IList <string> emissions = new List <string>();

            EventHandlers eventHandlers =
                EventHandlers.CreateBuilder()
                .Add <TestFibEvent2>(_ => emissions.Add("handled 2 first"))
                .Add <TestFibEvent2>(_ => emissions.Add("handled 2 second"))

                .Add <TestFibEvent3>(_ => emissions.Add("handled 3"))

                .Add <IFibEvent>(_ => emissions.Add("handled generic"))

                .Build();

            TestFibEvent2 testFibEvent2 = new TestFibEvent2();
            TestFibEvent3 testFibEvent3 = new TestFibEvent3();

            eventHandlers.Dispatch(testFibEvent2);
            eventHandlers.Dispatch(testFibEvent3);

            CollectionAssert.AreEqual(
                new[]
            {
                "handled 2 first",
                "handled 2 second",
                "handled generic",
                "handled 3",
                "handled generic"
            },
                emissions);
        }
        public async Task TestAcceptAsync()
        {
            using (DoubleAccumulator maxProgress = new DoubleAccumulator(0))
                using (ProgressEventHandler progressEventHandler =
                           new ProgressEventHandler(update => maxProgress.Accumulate(update.GetProgress())))
                {
                    EventHandlers eventHandlers =
                        EventHandlers.CreateBuilder().Add <ProgressEvent>(progressEventHandler.Accept).Build();

                    // Adds root, child1, and child1Child.
                    await MultithreadedExecutor.InvokeAsync(() => eventHandlers.Dispatch(new ProgressEvent(root, 0L)))
                    .ConfigureAwait(false);

                    await MultithreadedExecutor.InvokeAsync(() => eventHandlers.Dispatch(new ProgressEvent(child1, 0L)))
                    .ConfigureAwait(false);

                    await MultithreadedExecutor
                    .InvokeAsync(() => eventHandlers.Dispatch(new ProgressEvent(child1Child, 0L)))
                    .ConfigureAwait(false);

                    Assert.AreEqual(0.0, maxProgress.Get(), DOUBLE_ERROR_MARGIN);

                    // Adds 50 to child1Child and 100 to child2.
                    List <Action> callables = new List <Action>(150);
                    callables.AddRange(
                        Enumerable.Repeat((Action)(() => eventHandlers.Dispatch(new ProgressEvent(child1Child, 1L))), 50));
                    callables.AddRange(
                        Enumerable.Repeat((Action)(() => eventHandlers.Dispatch(new ProgressEvent(child2, 1L))), 100));

                    await MultithreadedExecutor.InvokeAllAsync(callables).ConfigureAwait(false);

                    Assert.AreEqual(
                        (1.0 / 2 / 100 * 50) + (1.0 / 2 / 200 * 100), maxProgress.Get(), DOUBLE_ERROR_MARGIN);

                    // 0 progress doesn't do anything.
                    await MultithreadedExecutor
                    .InvokeAllAsync(Enumerable.Repeat((Action)(() =>
                                                               eventHandlers.Dispatch(new ProgressEvent(child1, 0L))), 100))
                    .ConfigureAwait(false);

                    Assert.AreEqual(
                        (1.0 / 2 / 100 * 50) + (1.0 / 2 / 200 * 100), maxProgress.Get(), DOUBLE_ERROR_MARGIN);

                    // Adds 50 to child1Child and 100 to child2 to finish it up.
                    await MultithreadedExecutor.InvokeAllAsync(callables).ConfigureAwait(false);

                    Assert.AreEqual(1.0, maxProgress.Get(), DOUBLE_ERROR_MARGIN);
                }
        }
Beispiel #3
0
        public void TestLogging()
        {
            EventHandlers eventHandlers =
                EventHandlers.CreateBuilder().Add <TimerEvent>(timerEventQueue.Enqueue).Build();

            Mock.Get(mockClock).Setup(m => m.GetCurrentInstant()).Returns(Instant.FromUnixTimeSeconds(0));

            using (TimerEventDispatcher parentTimerEventDispatcher =
                       new TimerEventDispatcher(eventHandlers, "description", mockClock, null))
            {
                Mock.Get(mockClock).Setup(m => m.GetCurrentInstant()).Returns(Instant.FromUnixTimeSeconds(0) + Duration.FromMilliseconds(1));

                parentTimerEventDispatcher.Lap();
                Mock.Get(mockClock).Setup(m => m.GetCurrentInstant()).Returns((Instant.FromUnixTimeSeconds(0) + Duration.FromMilliseconds(1)).PlusNanoseconds(1));

                using (TimerEventDispatcher ignored =
                           parentTimerEventDispatcher.SubTimer("child description"))
                {
                    Mock.Get(mockClock).Setup(m => m.GetCurrentInstant()).Returns(Instant.FromUnixTimeSeconds(0) + Duration.FromMilliseconds(2));

                    // Laps on close.
                }
            }

            TimerEvent timerEvent = GetNextTimerEvent();

            VerifyStartState(timerEvent);
            VerifyDescription(timerEvent, "description");

            timerEvent = GetNextTimerEvent();
            VerifyStateFirstLap(timerEvent, State.LAP);
            VerifyDescription(timerEvent, "description");

            timerEvent = GetNextTimerEvent();
            VerifyStartState(timerEvent);
            VerifyDescription(timerEvent, "child description");

            timerEvent = GetNextTimerEvent();
            VerifyStateFirstLap(timerEvent, State.FINISHED);
            VerifyDescription(timerEvent, "child description");

            timerEvent = GetNextTimerEvent();
            VerifyStateNotFirstLap(timerEvent, State.FINISHED);
            VerifyDescription(timerEvent, "description");

            Assert.IsTrue(timerEventQueue.Count == 0);
        }
Beispiel #4
0
        public void TestAdd()
        {
            ITestFibEvent1 mockTestFibEvent1 = Mock.Of <ITestFibEvent1>();

            Mock.Get(mockTestFibEvent1).Setup(m => m.GetPayload()).Returns("payload");
            TestFibEvent2 testFibEvent2 = new TestFibEvent2();

            int           counter       = 0;
            EventHandlers eventHandlers =
                EventHandlers.CreateBuilder()
                .Add <ITestFibEvent1>(
                    testFibEvent1 => Assert.AreEqual("payload", testFibEvent1.GetPayload()))
                .Add <TestFibEvent2>(e => e.SayHello("Fib"))
                .Add <IFibEvent>(_ => counter++)
                .Build();

            eventHandlers.Dispatch(mockTestFibEvent1);
            eventHandlers.Dispatch(testFibEvent2);

            Assert.AreEqual(2, counter);
            Mock.Get(mockTestFibEvent1).Verify(m => m.GetPayload());
            Mock.Get(mockTestFibEvent1).VerifyNoOtherCalls();
            testFibEvent2.AssertMessageCorrect("Fib");
        }
Beispiel #5
0
 public LogEventTest()
 {
     eventHandlers =
         EventHandlers.CreateBuilder().Add <LogEvent>(receivedLogEvents.Enqueue).Build();
 }
Beispiel #6
0
 private static EventHandlers MakeEventHandlers(Action <ProgressEvent> progressEventConsumer)
 {
     return(EventHandlers.CreateBuilder().Add(progressEventConsumer).Build());
 }