public void EstablishContext()
        {
            _service = new TestService();
            _service2 = new TestService2();

            _serviceCoordinator = new ServiceCoordinator(new ThreadPoolFiber(), x => { }, x => { }, x => { }, 1.Minutes());
            _serviceCoordinator.CreateService("test",
                                              n =>
                                              new ServiceController<TestService>("test", null,
                                                                                 AddressRegistry.GetOutboundCoordinatorChannel(),
                                                                                 x => x.Start(),
                                                                                 x => x.Stop(),
                                                                                 x => x.Pause(),
                                                                                 x => x.Continue(),
                                                                                 (x, c) => _service));
            _serviceCoordinator.CreateService("test2",
                                              n =>
                                              new ServiceController<TestService>("test2", null,
                                                                                 AddressRegistry.GetOutboundCoordinatorChannel(),
                                                                                 x => x.Start(),
                                                                                 x => x.Stop(),
                                                                                 x => x.Pause(),
                                                                                 x =>
                                                                                 x.Continue(),
                                                                                 (x, c) =>
                                                                                 _service2));
            _serviceCoordinator.Start();

            _service.Running.WaitUntilCompleted(10.Seconds());
            _service2.Running.WaitUntilCompleted(10.Seconds());
        }
예제 #2
0
    public void ServiceLocaterTest()
    {
        ServInjector.Clear();

        //Serviceを継承した場合は自動で登録される
        var serv1 = new TestService1();

        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv1);
        //DisposeでUnbindされる
        serv1.Dispose();
        Assert.IsNull(ServInjector.Resolve <ITestService>());
        //手動で登録してみる
        ServInjector.Bind <ITestService>(serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv1);
        //別のサービスをバインドする
        var serv2 = new TestService2();

        ServInjector.Bind <ITestService>(serv2);
        Assert.AreNotEqual(ServInjector.Resolve <ITestService>(), serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv2);
        //service1はバインドされていないので解放しても結果は同じ
        serv1.Dispose();
        Assert.AreNotEqual(ServInjector.Resolve <ITestService>(), serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv2);
        //手動で解放してみる
        ServInjector.Unbind <ITestService>(serv2);
        Assert.IsNull(ServInjector.Resolve <ITestService>());
    }
        public void Fire_OneMatchingFact_FiresOnceAndCallsDependency()
        {
            //Arrange
            var  service1       = new TestService1();
            bool service1Called = false;

            service1.ServiceCalled += (sender, args) => service1Called = true;

            var  service2       = new TestService2();
            bool service2Called = false;

            service2.ServiceCalled += (sender, args) => service2Called = true;

            Session.DependencyResolver = new TestDependencyResolver(service1, service2);

            var fact = new FactType {
                TestProperty = "Valid Value 1"
            };

            Session.Insert(fact);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.True(service1Called);
            Assert.True(service2Called);
        }
        public void EstablishContext()
        {
            _service = new TestService();
            _service2 = new TestService2();

            _serviceCoordinator = new ServiceCoordinator(new PoolFiber(), x => { }, x => { }, x => { }, 1.Minutes());
            _serviceCoordinator.CreateService("test",
                                              (inbox,coordinator) =>
                                              new LocalServiceController<TestService>("test", inbox, coordinator,
                                                                                 x => x.Start(),
                                                                                 x => x.Stop(),
                                                                                 x => x.Pause(),
                                                                                 x => x.Continue(),
                                                                                 (x, c) => _service));
            _serviceCoordinator.CreateService("test2",
                                              (inbox, coordinator) =>
                                              new LocalServiceController<TestService>("test2", inbox, coordinator,
                                                                                 x => x.Start(),
                                                                                 x => x.Stop(),
                                                                                 x => x.Pause(),
                                                                                 x =>
                                                                                 x.Continue(),
                                                                                 (x, c) =>
                                                                                 _service2));
            _serviceCoordinator.Start();

            _service.Running.WaitUntilCompleted(10.Seconds());
            _service2.Running.WaitUntilCompleted(10.Seconds());
        }
        public void Fire_OneMatchingFact_CanResolveDependencyFromContext()
        {
            //Arrange
            var service1 = new TestService1();
            var service2 = new TestService2();

            Session.DependencyResolver = new TestDependencyResolver(service1, service2);

            var fact = new FactType {
                TestProperty = "Valid Value 1"
            };

            Session.Insert(fact);

            ITestService1 resolvedService1 = null;

            GetRuleInstance <TestRule>().Action = ctx =>
            {
                resolvedService1 = ctx.Resolve <ITestService1>();
            };

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.Same(service1, resolvedService1);
        }
        public void EstablishContext()
        {
            _service  = new TestService();
            _service2 = new TestService2();

            _serviceCoordinator = new ServiceCoordinator(new PoolFiber(), x => { }, x => { }, x => { }, 1.Minutes());
            _serviceCoordinator.CreateService("test",
                                              (inbox, coordinator) =>
                                              new LocalServiceController <TestService>("test", inbox, coordinator,
                                                                                       x => x.Start(),
                                                                                       x => x.Stop(),
                                                                                       x => x.Pause(),
                                                                                       x => x.Continue(),
                                                                                       (x, c) => _service));
            _serviceCoordinator.CreateService("test2",
                                              (inbox, coordinator) =>
                                              new LocalServiceController <TestService>("test2", inbox, coordinator,
                                                                                       x => x.Start(),
                                                                                       x => x.Stop(),
                                                                                       x => x.Pause(),
                                                                                       x =>
                                                                                       x.Continue(),
                                                                                       (x, c) =>
                                                                                       _service2));
            _serviceCoordinator.Start();

            _service.Running.WaitUntilCompleted(10.Seconds());
            _service2.Running.WaitUntilCompleted(10.Seconds());
        }
        public void RemoveServices()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();
            TestService1      svc1_     = new TestService1();
            TestService2      svc2      = new TestService2();

            container.RegisterService(svc1);
            container.RegisterService(svc2);
            container.RegisterService("svc1_", svc1_);
            Assert.AreEqual(svc1, container.GetService <IService1>());
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());

            container.UnregisterService(svc1);
            Assert.AreEqual(svc1_, container.GetService <IService1>());
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());
            container.UnregisterService(svc1_);
            Assert.AreEqual(null, container.GetService <IService1>());
            Assert.AreEqual(null, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());
            container.UnregisterService(svc2);
            Assert.AreEqual(null, container.GetService <IService2>());
        }
예제 #8
0
        public void EstablishContext()
        {
            _service = new TestService();
            _service2 = new TestService2();

            _beforeStartingServicesInvoked = false;
            _afterStartingServicesInvoked = false;
            _afterStoppingServicesInvoked = false;

            _serviceCoordinator = new ServiceCoordinator(x => { _beforeStartingServicesInvoked = true; }, x => { _afterStartingServicesInvoked = true; }, x => { _afterStoppingServicesInvoked = true; });
            IList<Func<IServiceController>> services = new List<Func<IServiceController>>
                                                       {
                                                           () => new ServiceController<TestService>
                                                                 {
                                                                     Name = "test",
                                                                     BuildService = s => _service,
                                                                     StartAction = x => x.Start(),
                                                                     StopAction = x => x.Stop(),
                                                                     ContinueAction = x => x.Continue(),
                                                                     PauseAction = x => x.Pause()
                                                                 },
                                                           () => new ServiceController<TestService2>
                                                                 {
                                                                     Name = "test2",
                                                                     BuildService = s => _service2,
                                                                     StartAction = x => x.Start(),
                                                                     StopAction = x => x.Stop(),
                                                                     ContinueAction = x => x.Continue(),
                                                                     PauseAction = x => x.Pause()
                                                                 }
                                                       };

            _serviceCoordinator.RegisterServices(services);
        }
        public void EstablishContext()
        {
            _service = new TestService();
            _service2 = new TestService2();

            _serviceCoordinator = new ServiceCoordinator(x => { }, x => { }, x => { });
            IList<Func<IServiceController>> services = new List<Func<IServiceController>>
                                                       {
                                                           () =>
                                                           new ServiceController<TestService>("test")
                                                           {
                                                               BuildService = s=> _service,
                                                               StartAction = x => x.Start(),
                                                               StopAction = x => x.Stop(),
                                                               ContinueAction = x => x.Continue(),
                                                               PauseAction = x => x.Pause()
                                                           },
                                                           () =>
                                                           new ServiceController<TestService2>("test2")
                                                           {
                                                               BuildService = s=> _service2,
                                                               StartAction = x => x.Start(),
                                                               StopAction = x => x.Stop(),
                                                               ContinueAction = x => x.Continue(),
                                                               PauseAction = x => x.Pause()
                                                           }
                                                       };

            _serviceCoordinator.RegisterServices(services);
            _serviceCoordinator.Start();

            Thread.Sleep(2.Seconds());
        }
예제 #10
0
        private void ServiceLocator2_Button_Click(object sender, RoutedEventArgs e)
        {
            TestService2 service = ServiceLocator.Instance.GetService <TestService2>();

            if (service != null)
            {
                MessageBox.Show("Get TestService2");
            }
        }
        public void GetServiceByKey_PreferParent()
        {
            var parent   = new TestSupportServices();
            var child    = new TestSupportServices();
            var service2 = new TestService2();

            child.ServiceContainer.RegisterService("key", service2);
            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.PreferParents));
        }
        public void YieldToParentTest4()
        {
            var child         = new TestSupportServices();
            var service2Local = new TestService2();

            child.ServiceContainer.RegisterService("key", service2Local, true);

            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.PreferParents));
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.PreferLocal));
        }
예제 #13
0
 public void Method1(TestService1 service1, TestService2 service2)
 {
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     if (service2 is null)
     {
         throw new ArgumentNullException(nameof(service2));
     }
     service1.Success = true;
 }
 public void AddServicesWithoutKey() {
     ServiceContainer container = new ServiceContainer(null);
     TestService1 svc1 = new TestService1();
     TestService1 svc1_ = new TestService1();
     TestService2 svc2 = new TestService2();
     container.RegisterService(svc1);
     Assert.AreEqual(svc1, container.GetService<IService1>());
     container.RegisterService(svc2);
     Assert.AreEqual(svc1, container.GetService<IService1>());
     Assert.AreEqual(svc2, container.GetService<IService2>());
     container.RegisterService("svc1_", svc1_);
     Assert.AreEqual(svc1, container.GetService<IService1>());
     Assert.AreEqual(svc1_, container.GetService<IService1>("svc1_"));
 }
        public void GetServiceFromParentByKey_InvalidParent()
        {
            var child = new TestSupportServices();

            var service2 = new TestService2();

            child.ServiceContainer.RegisterService("key", service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>("key"));
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>("key2"));

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>("key"));
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>("key2"));
        }
        public void AddServicesWithoutKey_2()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();
            TestService1      svc1_     = new TestService1();
            TestService2      svc2      = new TestService2();

            container.RegisterService("svc1_", svc1_);
            container.RegisterService(svc1);
            container.RegisterService(svc2);
            Assert.AreEqual(svc1, container.GetService <IService1>());
            Assert.AreEqual(svc2, container.GetService <IService2>());
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
        }
        public void YieldToParentTest2()
        {
            var parent   = new TestSupportServices();
            var child    = new TestSupportServices();
            var service2 = new TestService2();

            parent.ServiceContainer.RegisterService(service2);
            child.ParentViewModel = parent;
            var service2Local = new TestService2();

            child.ServiceContainer.RegisterService(service2Local, true);

            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.PreferParents));
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.PreferLocal));
        }
        public void RegisterServiceWithoutNameTwice()
        {
            var viewModel = new TestSupportServices();

            viewModel.ServiceContainer.RegisterService(new TestService1());

            var service1_2 = new TestService1_2();

            viewModel.ServiceContainer.RegisterService(service1_2);
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService <IService1>());
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService <IService2>());

            var service2 = new TestService2();

            viewModel.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService <IService1>());
            Assert.AreEqual(service2, viewModel.ServiceContainer.GetService <IService2>());
        }
        public void Clear()
        {
            var parent = new TestSupportServices();
            var child  = new TestSupportServices();

            var service2 = new TestService2();

            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());

            child.ServiceContainer.Clear();
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>());

            child.ServiceContainer.RegisterService("key", service2);
            child.ServiceContainer.Clear();
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>("key"));
        }
        public void GetServiceFromParent() {
            var parent = new TestSupportServices();
            var child = new TestSupportServices();
            var service1 = new TestService1();
            child.ServiceContainer.RegisterService(service1);
            Assert.AreEqual(service1, child.ServiceContainer.GetService<IService1>());

            var service2 = new TestService2();
            parent.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>());

            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());

            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.LocalOnly));
            var service2Local = new TestService2();
            child.ServiceContainer.RegisterService(service2Local);
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.PreferParents));
        }
        public void GetServiceFromParentByKey()
        {
            var parent = new TestSupportServices();
            var child  = new TestSupportServices();

            var service2 = new TestService2();

            parent.ServiceContainer.RegisterService("key", service2);
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>("key"));

            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>("key"));

            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.LocalOnly));
            var service2Local = new TestService2();

            child.ServiceContainer.RegisterService("key", service2Local);
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.PreferParents));
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>("key", ServiceSearchMode.PreferLocal));
        }
        public void GetApplicationService()
        {
            var defaultServiceContainer = new DefaultServiceContainer2();
            var service2InApp           = new TestService2();

            defaultServiceContainer.Resources.Add("testService2", service2InApp);
            var service11 = new TestService1();
            var service12 = new TestService1();

            defaultServiceContainer.Resources.Add("testService11", service11);
            defaultServiceContainer.Resources.Add("testService12", service12);
            ServiceContainer.Default = defaultServiceContainer;
            var parent   = new TestSupportServices();
            var child    = new TestSupportServices();
            var service2 = new TestService2();

            child.ServiceContainer.RegisterService(service2);
            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.PreferParents));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.IsNotNull(child.ServiceContainer.GetService <IService1>());
            Assert.AreEqual(service11, child.ServiceContainer.GetService <IService1>("testService11"));
        }
        public void GetServiceFromParent_InvalidParent()
        {
            var child = new TestSupportServices();

            var service2 = new TestService2();

            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());

            var service1 = new TestService1();

            ServiceContainer.Default.RegisterService(service1);
            try {
                Assert.AreEqual(service1, child.ServiceContainer.GetService <IService1>());
            } finally {
                ServiceContainer.Default.Clear();
            }
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());
        }
        public void EstablishContext()
        {
            _service = new TestService();
            _service2 = new TestService2();

            _beforeStartingServicesInvoked = false;
            _afterStartingServicesInvoked = false;
            _afterStoppingServicesInvoked = false;

            _serviceCoordinator = new ServiceCoordinator(x => { _beforeStartingServicesInvoked = true; },
                                                         x => { _afterStartingServicesInvoked = true; },
                                                         x => { _afterStoppingServicesInvoked = true; },
                                                         10.Seconds());

            IList<Func<IServiceController>> services = new List<Func<IServiceController>>
                                                       {
                                                           () => new ServiceController<TestService>("test", WellknownAddresses.GetServiceCoordinatorProxy())
                                                                 {
                                                                     BuildService = s => _service,
                                                                     StartAction = x => x.Start(),
                                                                     StopAction = x => x.Stop(),
                                                                     ContinueAction = x => x.Continue(),
                                                                     PauseAction = x => x.Pause()
                                                                 },
                                                           () => new ServiceController<TestService2>("test2", WellknownAddresses.GetServiceCoordinatorProxy())
                                                                 {
                                                                     BuildService = s => _service2,
                                                                     StartAction = x => x.Start(),
                                                                     StopAction = x => x.Stop(),
                                                                     ContinueAction = x => x.Continue(),
                                                                     PauseAction = x => x.Pause()
                                                                 }
                                                       };

            _serviceCoordinator.RegisterServices(services);
        }
예제 #25
0
 public ComponentWithConstructorInjection(TestService1 property1, TestService2 property2)
 {
     Property1 = property1;
     Property2 = property2;
 }
        public void GetServiceFromParent_InvalidParent() {
            var child = new TestSupportServices();

            var service2 = new TestService2();
            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());

            var service1 = new TestService1();
            ServiceContainer.Default.RegisterService(service1);
            try {
                Assert.AreEqual(service1, child.ServiceContainer.GetService<IService1>());
            } finally {
                ServiceContainer.Default.Clear();
            }
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());
        }
        public void GetServiceFromParentByKey_InvalidParent() {
            var child = new TestSupportServices();

            var service2 = new TestService2();
            child.ServiceContainer.RegisterService("key", service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>("key"));
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>("key2"));

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>("key"));
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>("key2"));
        }
 public void GetServiceByKey_PreferParent() {
     var parent = new TestSupportServices();
     var child = new TestSupportServices();
     var service2 = new TestService2();
     child.ServiceContainer.RegisterService("key", service2);
     child.ParentViewModel = parent;
     Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>("key", ServiceSearchMode.PreferParents));
 }
        public void RegisterServiceWithoutNameTwice() {
            var viewModel = new TestSupportServices();
            viewModel.ServiceContainer.RegisterService(new TestService1());

            var service1_2 = new TestService1_2();
            viewModel.ServiceContainer.RegisterService(service1_2);
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService<IService1>());
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService<IService2>());

            var service2 = new TestService2();
            viewModel.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service1_2, viewModel.ServiceContainer.GetService<IService1>());
            Assert.AreEqual(service2, viewModel.ServiceContainer.GetService<IService2>());
        }
예제 #30
0
 public TestClass1(IServiceProvider serviceProvider, TestService1 service1, TestService2 service2)
 {
     if (serviceProvider is null)
     {
         throw new ArgumentNullException(nameof(serviceProvider));
     }
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     if (service2 is null)
     {
         throw new ArgumentNullException(nameof(service2));
     }
     service1.Success = true;
 }
 public TestDependencyResolver(TestService1 service1, TestService2 service2)
 {
     _service1 = service1;
     _service2 = service2;
 }
예제 #32
0
 public void Method2(TestService1 service1, TestService2 service2)
 {
     throw new InvalidOperationException("Invalid method called!");
 }
예제 #33
0
 public bool Method2(TestService1 service1, TestService2 service2, bool test)
 {
     throw new InvalidOperationException("Invalid method called!");
 }
예제 #34
0
 public TestClass1(TestService1 service1, TestService2 service2)
 {
     throw new InvalidOperationException("Invalid method called!");
 }
        public void Clear() {
            var parent = new TestSupportServices();
            var child = new TestSupportServices();

            var service2 = new TestService2();
            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());

            child.ServiceContainer.Clear();
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>());

            child.ServiceContainer.RegisterService("key", service2);
            child.ServiceContainer.Clear();
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>("key"));
        }