Пример #1
0
        public async Task SmartLifecycleGroupShutdown()
        {
            var stoppedBeans = new ConcurrentQueue <ILifecycle>();

            var bean1 = TestSmartLifecycleBean.ForShutdownTests(1, 300, stoppedBeans);
            var bean2 = TestSmartLifecycleBean.ForShutdownTests(3, 100, stoppedBeans);
            var bean3 = TestSmartLifecycleBean.ForShutdownTests(1, 600, stoppedBeans);
            var bean4 = TestSmartLifecycleBean.ForShutdownTests(2, 400, stoppedBeans);
            var bean5 = TestSmartLifecycleBean.ForShutdownTests(2, 700, stoppedBeans);
            var bean6 = TestSmartLifecycleBean.ForShutdownTests(int.MaxValue, 200, stoppedBeans);
            var bean7 = TestSmartLifecycleBean.ForShutdownTests(3, 200, stoppedBeans);

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

            await processor.Stop();

            var stopped = stoppedBeans.ToArray();

            Assert.Equal(int.MaxValue, GetPhase(stopped[0]));
            Assert.Equal(3, GetPhase(stopped[1]));
            Assert.Equal(3, GetPhase(stopped[2]));
            Assert.Equal(2, GetPhase(stopped[3]));
            Assert.Equal(2, GetPhase(stopped[4]));
            Assert.Equal(1, GetPhase(stopped[5]));
            Assert.Equal(1, GetPhase(stopped[6]));
        }
Пример #2
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]));
        }
Пример #3
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]));
        }
Пример #4
0
        public async Task SingleSmartLifecycleShutdown()
        {
            var stoppedBeans = new ConcurrentQueue <ILifecycle>();
            var bean         = TestSmartLifecycleBean.ForShutdownTests(99, 300, stoppedBeans);

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

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

            var stopped = stoppedBeans.ToArray();

            Assert.Same(bean, stopped[0]);
        }
Пример #5
0
        public async Task SingleSmartLifecycleAutoStartup()
        {
            var startedBeans = new ConcurrentQueue <ILifecycle>();
            var bean         = TestSmartLifecycleBean.ForStartupTests(1, startedBeans);

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

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

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

            Assert.False(bean.IsRunning);
            Assert.Single(startedBeans);
        }
        public async Task SingleSmartLifecycleWithoutAutoStartup()
        {
            var startedBeans = new ConcurrentQueue <ILifecycle>();
            var bean         = TestSmartLifecycleBean.ForStartupTests(1, startedBeans);

            bean.IsAutoStartup = false;
            var processor = new DefaultLifecycleProcessor(new List <ILifecycle>()
            {
                bean
            });

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

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

            Assert.True(bean.IsRunning);
            Assert.Single(startedBeans);
            await processor.Stop();
        }
Пример #7
0
        public async Task SmartLifecycleGroupStartup()
        {
            var startedBeans = new ConcurrentQueue <ILifecycle>();
            var beanMin      = TestSmartLifecycleBean.ForStartupTests(int.MinValue, startedBeans);
            var bean1        = TestSmartLifecycleBean.ForStartupTests(1, startedBeans);
            var bean2        = TestSmartLifecycleBean.ForStartupTests(2, startedBeans);
            var bean3        = TestSmartLifecycleBean.ForStartupTests(3, startedBeans);
            var beanMax      = TestSmartLifecycleBean.ForStartupTests(int.MaxValue, startedBeans);
            var processor    = new DefaultLifecycleProcessor(CreateApplicationContext(new List <ILifecycle>()
            {
                bean3, beanMin, bean2, beanMax, bean1
            }, new List <ISmartLifecycle>()));

            Assert.False(beanMin.IsRunning);
            Assert.False(bean1.IsRunning);
            Assert.False(bean2.IsRunning);
            Assert.False(bean3.IsRunning);
            Assert.False(beanMax.IsRunning);

            await processor.OnRefresh();

            Assert.True(beanMin.IsRunning);
            Assert.True(bean1.IsRunning);
            Assert.True(bean2.IsRunning);
            Assert.True(bean3.IsRunning);
            Assert.True(beanMax.IsRunning);

            await processor.Stop();

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

            Assert.Equal(int.MinValue, GetPhase(started[0]));
            Assert.Equal(1, GetPhase(started[1]));
            Assert.Equal(2, GetPhase(started[2]));
            Assert.Equal(3, GetPhase(started[3]));
            Assert.Equal(int.MaxValue, GetPhase(started[4]));
        }
        public async Task SingleLifecycleShutdown()
        {
            var        stoppedBeans = new ConcurrentQueue <ILifecycle>();
            ILifecycle bean         = new TestLifecycleBean(null, stoppedBeans);

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

            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]);
        }