Beispiel #1
0
        public async Task MixedShutdown()
        {
            var        stoppedBeans = new ConcurrentQueue <ILifecycle>();
            ILifecycle bean1        = TestLifecycleBean.ForShutdownTests(stoppedBeans);
            ILifecycle bean2        = TestSmartLifecycleBean.ForShutdownTests(500, 200, stoppedBeans);
            ILifecycle bean3        = TestSmartLifecycleBean.ForShutdownTests(int.MaxValue, 100, stoppedBeans);
            ILifecycle bean4        = TestLifecycleBean.ForShutdownTests(stoppedBeans);
            ILifecycle bean5        = TestSmartLifecycleBean.ForShutdownTests(1, 200, stoppedBeans);
            ILifecycle bean6        = TestSmartLifecycleBean.ForShutdownTests(-1, 100, stoppedBeans);
            ILifecycle bean7        = TestSmartLifecycleBean.ForShutdownTests(int.MinValue, 300, stoppedBeans);

            var processor = new DefaultLifecycleProcessor(CreateApplicationContext(new List <ILifecycle>()
            {
                bean1, bean2, bean3, bean4, bean5, bean6, bean7
            }, new List <ISmartLifecycle>()));
            await processor.OnRefresh();

            Assert.True(bean2.IsRunning);
            Assert.True(bean3.IsRunning);
            Assert.True(bean5.IsRunning);
            Assert.True(bean6.IsRunning);
            Assert.True(bean7.IsRunning);
            Assert.False(bean1.IsRunning);
            Assert.False(bean4.IsRunning);

            await bean1.Start();

            await bean4.Start();

            Assert.True(bean1.IsRunning);
            Assert.True(bean4.IsRunning);

            await processor.Stop();

            Assert.False(bean2.IsRunning);
            Assert.False(bean3.IsRunning);
            Assert.False(bean5.IsRunning);
            Assert.False(bean6.IsRunning);
            Assert.False(bean7.IsRunning);
            Assert.False(bean1.IsRunning);
            Assert.False(bean4.IsRunning);

            var stopped = stoppedBeans.ToArray();

            Assert.Equal(7, stopped.Length);
            Assert.Equal(int.MaxValue, GetPhase(stopped[0]));
            Assert.Equal(500, GetPhase(stopped[1]));
            Assert.Equal(1, GetPhase(stopped[2]));
            Assert.Equal(0, GetPhase(stopped[3]));
            Assert.Equal(0, GetPhase(stopped[4]));
            Assert.Equal(-1, GetPhase(stopped[5]));
            Assert.Equal(int.MinValue, GetPhase(stopped[6]));
        }
Beispiel #2
0
        public async Task RefreshThenStopAndRestartWithMixedBeans()
        {
            var startedBeans = new ConcurrentQueue <ILifecycle>();
            var simpleBean1  = TestLifecycleBean.ForStartupTests(startedBeans);
            var simpleBean2  = TestLifecycleBean.ForStartupTests(startedBeans);
            var smartBean1   = TestSmartLifecycleBean.ForStartupTests(5, startedBeans);
            var smartBean2   = TestSmartLifecycleBean.ForStartupTests(-3, startedBeans);
            var processor    = new DefaultLifecycleProcessor(CreateApplicationContext(new List <ILifecycle>()
            {
                simpleBean1, simpleBean2, smartBean1, smartBean2
            }, new List <ISmartLifecycle>()));

            Assert.False(simpleBean1.IsRunning);
            Assert.False(simpleBean2.IsRunning);
            Assert.False(smartBean1.IsRunning);
            Assert.False(smartBean2.IsRunning);

            await processor.OnRefresh();

            Assert.False(simpleBean1.IsRunning);
            Assert.False(simpleBean2.IsRunning);
            Assert.True(smartBean1.IsRunning);
            Assert.True(smartBean2.IsRunning);

            Assert.Equal(2, startedBeans.Count);
            var started = startedBeans.ToArray();

            Assert.Equal(-3, GetPhase(started[0]));
            Assert.Equal(5, GetPhase(started[1]));

            await processor.Stop();

            Assert.False(simpleBean1.IsRunning);
            Assert.False(simpleBean2.IsRunning);
            Assert.False(smartBean1.IsRunning);
            Assert.False(smartBean2.IsRunning);

            await processor.Start();

            Assert.True(simpleBean1.IsRunning);
            Assert.True(simpleBean2.IsRunning);
            Assert.True(smartBean1.IsRunning);
            Assert.True(smartBean2.IsRunning);

            Assert.Equal(6, startedBeans.Count);
            started = startedBeans.ToArray();
            Assert.Equal(-3, GetPhase(started[2]));
            Assert.Equal(0, GetPhase(started[3]));
            Assert.Equal(0, GetPhase(started[4]));
            Assert.Equal(5, GetPhase(started[5]));
        }
Beispiel #3
0
        public async Task SingleLifecycleShutdown()
        {
            var        stoppedBeans = new ConcurrentQueue <ILifecycle>();
            ILifecycle bean         = new TestLifecycleBean(null, stoppedBeans);

            var processor = new DefaultLifecycleProcessor(CreateApplicationContext(new List <ILifecycle>()
            {
                bean
            }, new List <ISmartLifecycle>()));

            Assert.False(bean.IsRunning);
            await processor.OnRefresh();

            Assert.False(bean.IsRunning);
            await processor.Start();

            Assert.True(bean.IsRunning);
            await processor.Stop();

            var stopped = stoppedBeans.ToArray();

            Assert.False(bean.IsRunning);
            Assert.Same(bean, stopped[0]);
        }