public void TestSmoke() { void progressEventConsumer(ProgressEvent progressEvent) { Allocation allocation = progressEvent.GetAllocation(); long units = progressEvent.GetUnits(); UpdateCompletionMap(allocation, units); } EventHandlers eventHandlers = MakeEventHandlers(progressEventConsumer); eventHandlers.Dispatch(new ProgressEvent(child1Child, 50)); Assert.AreEqual(1, allocationCompletionMap.Count); Assert.AreEqual(50, allocationCompletionMap[child1Child]); eventHandlers.Dispatch(new ProgressEvent(child1Child, 50)); Assert.AreEqual(3, allocationCompletionMap.Count); Assert.AreEqual(100, allocationCompletionMap[child1Child]); Assert.AreEqual(1, allocationCompletionMap[child1]); Assert.AreEqual(1, allocationCompletionMap[root]); eventHandlers.Dispatch(new ProgressEvent(child2, 200)); Assert.AreEqual(4, allocationCompletionMap.Count); Assert.AreEqual(100, allocationCompletionMap[child1Child]); Assert.AreEqual(1, allocationCompletionMap[child1]); Assert.AreEqual(200, allocationCompletionMap[child2]); Assert.AreEqual(2, allocationCompletionMap[root]); }
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 void TestType() { // Used to test whether or not progress event was consumed bool[] called = new bool[] { false }; void buildImageConsumer(ProgressEvent _) { called[0] = true; } EventHandlers eventHandlers = MakeEventHandlers(buildImageConsumer); eventHandlers.Dispatch(new ProgressEvent(child1, 50)); Assert.IsTrue(called[0]); }
public void TestAccumulateProgress() { void progressEventConsumer(ProgressEvent progressEvent) { double fractionOfRoot = progressEvent.GetAllocation().GetFractionOfRoot(); long units = progressEvent.GetUnits(); progress += units * fractionOfRoot; } EventHandlers eventHandlers = MakeEventHandlers(progressEventConsumer); eventHandlers.Dispatch(new ProgressEvent(child1Child, 50)); Assert.AreEqual(1.0 / 2 / 100 * 50, progress, DOUBLE_ERROR_MARGIN); eventHandlers.Dispatch(new ProgressEvent(child1Child, 50)); Assert.AreEqual(1.0 / 2, progress, DOUBLE_ERROR_MARGIN); eventHandlers.Dispatch(new ProgressEvent(child2, 10)); Assert.AreEqual((1.0 / 2) + (1.0 / 2 / 200 * 10), progress, DOUBLE_ERROR_MARGIN); eventHandlers.Dispatch(new ProgressEvent(child2, 190)); Assert.AreEqual(1.0, progress, DOUBLE_ERROR_MARGIN); }
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"); }
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); } }
public void TestFactories() { eventHandlers.Dispatch(Error("error")); eventHandlers.Dispatch(Lifecycle("lifecycle")); eventHandlers.Dispatch(LogEvent.Progress("progress")); eventHandlers.Dispatch(Warn("warn")); eventHandlers.Dispatch(Info("info")); eventHandlers.Dispatch(Debug("debug")); VerifyNextLogEvent(Level.Error, "error"); VerifyNextLogEvent(Level.Lifecycle, "lifecycle"); VerifyNextLogEvent(Level.Progress, "progress"); VerifyNextLogEvent(Level.Warn, "warn"); VerifyNextLogEvent(Level.Info, "info"); VerifyNextLogEvent(Level.Debug, "debug"); Assert.IsTrue(receivedLogEvents.Count == 0); }