Пример #1
0
        public void SimpleTwoServicesWithDependency()
        {
            logger.Clear();
            var manager = ServiceManager.Create();

            // REGISTER
            IService service;
            var      serviceObject1 = new TestService1(out service);

            manager.Manager.Register(service);
            Assert.AreEqual(1, manager.Manager.ListServices().Count());
            var serviceObject2 = new TestService2(out service);

            manager.Manager.Register(service);
            Assert.AreEqual(2, manager.Manager.ListServices().Count());

            var services = manager.Manager.ListServices().ToArray();

            Assert.AreEqual(2, services.Length);

            var deps = manager.Manager.ListUnregisteredDependencies().ToArray();

            Assert.AreEqual(0, deps.Length);

            // START
            manager.StartServices(new TaskContextMock(logger));
            Assert.AreEqual(ServiceManager.ServiceManagerState.Started, manager.Manager.State);
            var walker = new MiniLoggerWalker(logger.First);

            walker.ExpectNext("Initializing service TestService1 - Progress: 0");
            walker.ExpectNext("Initializing service TestService2 - Progress: 1");
            walker.ExpectNext("Starting service TestService1 - Progress: 2");
            walker.ExpectNext("Start TestService1");
            walker.ExpectNext("Starting service TestService2 - Progress: 3");
            walker.ExpectNext("Start TestService2");
            walker.ExpectNext("Started all services - Progress: 4");
            walker.ExpectEnd();

            // ACCESS
            object serviceFromManager = manager.Manager.Get <TestService1>();

            Assert.IsNotNull(serviceFromManager);
            Assert.AreSame(serviceFromManager, serviceObject1);

            serviceFromManager = manager.Manager.Get <TestService2>();
            Assert.IsNotNull(serviceFromManager);
            Assert.AreSame(serviceFromManager, serviceObject2);

            // STOP
            manager.StopServices(new TaskContextMock(logger));
            Assert.AreEqual(ServiceManager.ServiceManagerState.Stopped, manager.Manager.State);
            walker.ContinueAgain();
            walker.ExpectNext("Stopping service TestService2 - Progress: 0");
            walker.ExpectNext("Stop TestService2");
            walker.ExpectNext("Stopping service TestService1 - Progress: 1");
            walker.ExpectNext("Stop TestService1");
            walker.ExpectNext("Stopped all services - Progress: 2");
            walker.ExpectEnd();
        }
Пример #2
0
        public void OneServiceWithUnknownDependency()
        {
            logger.Clear();
            IService service;
            var      serviceObject = new TestService2(out service);

            // REGISTER
            var manager = ServiceManager.Create();

            manager.Manager.Register(service);

            try
            {
                var serviceFromManager = manager.Manager.Get <TestService1>();
                Assert.Fail("Expected exception");
            }
            catch { }

            var deps = manager.Manager.ListUnregisteredDependencies().ToArray();

            Assert.AreEqual(1, deps.Length);
            Assert.AreEqual(typeof(TestService1), deps[0]);

            try
            {
                manager.StartServices(new TaskContextMock(logger));
                Assert.Fail("Expected exception");
            }
            catch
            {
            }
            Assert.AreEqual(ServiceManager.ServiceManagerState.InitOrStartFailed, manager.Manager.State);
            var walker = new MiniLoggerWalker(logger.First);

            walker.ExpectNext("Failed starting services. Some dependencies are missing (TestService1). - Progress: 0");
            walker.ExpectEnd();
        }