예제 #1
0
        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 LifecycleObservable <TestStages>(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(kvp.Key, o));
            }

            // run lifecycle
            if (failOnStart.HasValue)
            {
                await Assert.ThrowsAsync <OrleansLifecycleCanceledException>(() => lifecycle.OnStart());
            }
            else
            {
                await lifecycle.OnStart();
            }
            await lifecycle.OnStop();

            // return results
            return(observersByStage);
        }
예제 #2
0
        public async Task MultiStageObserverLifecycleTest()
        {
            var lifecycle          = new LifecycleObservable <TestStages>(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));
        }