예제 #1
0
        public void TestServiceStacking()
        {
            var serviceConfiguration = ServiceConfiguration.ConfigurationModule
                                       .Set(ServiceConfiguration.Services, GenericType <TestService> .Class)
                                       .Build();

            var serviceInjector = TangFactory.GetTang().NewInjector(serviceConfiguration);
            var contextConfig   = GetContextEventHandlerContextConfiguration();

            using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty()))
            {
                var childContextConfiguration = ContextConfiguration.ConfigurationModule
                                                .Set(ContextConfiguration.Identifier, "Context2")
                                                .Build();

                var childServiceConfiguration = ServiceConfiguration.ConfigurationModule
                                                .Set(ServiceConfiguration.Services, GenericType <SecondTestService> .Class)
                                                .Build();

                using (var childContextRuntime = contextRuntime.SpawnChildContext(childContextConfiguration, childServiceConfiguration))
                {
                    // Check that parent service injector does not contain instances of SecondTestService
                    Assert.False(contextRuntime.Services.Value.OfType <SecondTestService>().Any());
                    Assert.True(childContextRuntime.Services.Value.OfType <TestService>().Count() == 1);
                    Assert.True(childContextRuntime.Services.Value.OfType <SecondTestService>().Count() == 1);
                    Assert.True(childContextRuntime.Services.Value.Count() == 2);
                    Assert.True(ReferenceEquals(
                                    childContextRuntime.ContextInjector.GetInstance <TestService>(),
                                    contextRuntime.Services.Value.OfType <TestService>().Single()));
                }
            }
        }
예제 #2
0
        public void TestBaseServiceWithContextStacking()
        {
            var serviceConfiguration = ServiceConfiguration.ConfigurationModule
                                       .Set(ServiceConfiguration.Services, GenericType <TestService> .Class)
                                       .Build();

            var serviceInjector = TangFactory.GetTang().NewInjector(serviceConfiguration);
            var contextConfig   = GetContextEventHandlerContextConfiguration();

            using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty()))
            {
                var childContextConfiguration = ContextConfiguration.ConfigurationModule
                                                .Set(ContextConfiguration.Identifier, "Context2")
                                                .Build();

                using (var childContextRuntime = contextRuntime.SpawnChildContext(childContextConfiguration))
                {
                    var servicesFromInjector = serviceInjector.GetNamedInstance <ServicesSet, ISet <object> >();

                    // Check that parent service injector does not contain instances of SecondTestService
                    Assert.False(servicesFromInjector.OfType <SecondTestService>().Any());

                    var contextTestService = childContextRuntime.ContextInjector.GetInstance <TestService>();
                    Assert.True(ReferenceEquals(contextTestService, serviceInjector.GetInstance <TestService>()));
                }
            }
        }
예제 #3
0
        public void TestUnableToSpawnChildWhileTaskIsRunning()
        {
            var serviceInjector = TangFactory.GetTang().NewInjector();
            var contextConfig   = GetSimpleContextConfiguration();

            using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty()))
            {
                var taskConfig = TaskConfiguration.ConfigurationModule
                                 .Set(TaskConfiguration.Task, GenericType <TestTask> .Class)
                                 .Set(TaskConfiguration.Identifier, "ID")
                                 .Build();

                Thread taskThread = null;
                try
                {
                    var hbMgr = Substitute.For <IHeartBeatManager>();
                    contextRuntime.ContextInjector.BindVolatileInstance(GenericType <IHeartBeatManager> .Class, hbMgr);
                    taskThread = contextRuntime.StartTaskOnNewThread(taskConfig);

                    Assert.True(contextRuntime.TaskRuntime.IsPresent());
                    Assert.True(contextRuntime.GetTaskStatus().IsPresent());

                    // wait for the task to start
                    var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask;
                    testTask.StartEvent.Wait();
                    Assert.Equal(State.RUNNING, contextRuntime.GetTaskStatus().Value.state);

                    var childContextConfiguration = GetSimpleContextConfiguration();

                    Assert.Throws <InvalidOperationException>(
                        () => contextRuntime.SpawnChildContext(childContextConfiguration));
                }
                finally
                {
                    var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask;
                    if (testTask == null)
                    {
                        throw new Exception();
                    }

                    testTask.CountDownEvent.Signal();
                    testTask.DisposedEvent.Wait();

                    if (taskThread != null)
                    {
                        taskThread.Join();
                    }
                }
            }
        }
예제 #4
0
        public void TestContextStackingParentContext()
        {
            var serviceInjector = TangFactory.GetTang().NewInjector();
            var contextConfig   = GetSimpleContextConfiguration();

            using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty()))
            {
                var childContextConfiguration = GetSimpleContextConfiguration();
                using (var childContextRuntime = contextRuntime.SpawnChildContext(childContextConfiguration))
                {
                    Assert.False(contextRuntime.ParentContext.IsPresent());
                    Assert.True(childContextRuntime.ParentContext.IsPresent());
                    Assert.True(ReferenceEquals(contextRuntime, childContextRuntime.ParentContext.Value));
                }
            }
        }
예제 #5
0
        public void TestContextStackingDoesNotGetSameInstance()
        {
            var serviceInjector = TangFactory.GetTang().NewInjector();
            var contextConfig   = GetContextEventHandlerContextConfiguration();

            using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty()))
            {
                var childContextConfiguration = GetContextEventHandlerContextConfiguration();
                using (var childContextRuntime = contextRuntime.SpawnChildContext(childContextConfiguration))
                {
                    Assert.False(ReferenceEquals(
                                     contextRuntime.ContextInjector.GetInstance <TestContextEventHandler>(),
                                     childContextRuntime.ContextInjector.GetInstance <TestContextEventHandler>()));
                }
            }
        }