private async Task <Dictionary <TestStages, List <Observer> > > RunLifecycle(Dictionary <TestStages, int> observerCountByStage, TestStages?failOnStart, TestStages?failOnStop) { // setup lifecycle observers var observersByStage = new Dictionary <TestStages, List <Observer> >(); var lifecycle = new TestLifecycleSubject(null); foreach (KeyValuePair <TestStages, int> kvp in observerCountByStage) { List <Observer> observers = Enumerable .Range(0, kvp.Value) .Select(i => new Observer(failOnStart.HasValue && kvp.Key == failOnStart, failOnStop.HasValue && kvp.Key == failOnStop)) .ToList(); observersByStage[kvp.Key] = observers; observers.ForEach(o => lifecycle.Subscribe((int)kvp.Key, o)); } // run lifecycle if (failOnStart.HasValue) { await Assert.ThrowsAsync <Exception>(() => lifecycle.OnStart()); } else { await lifecycle.OnStart(); } await lifecycle.OnStop(); // return results return(observersByStage); }
public async Task MultiStageObserverLifecycleTest() { var lifecycle = new TestLifecycleSubject(null); var multiStageObserver = new MultiStageObserver(); multiStageObserver.Participate(lifecycle); await lifecycle.OnStart(); await lifecycle.OnStop(); Assert.Equal(4, multiStageObserver.Started.Count); Assert.Equal(4, multiStageObserver.Stopped.Count); Assert.True(multiStageObserver.Started.Values.All(o => o)); Assert.True(multiStageObserver.Stopped.Values.All(o => o)); }