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>())); } } }
public void TestServiceInstantiatedAndDisposed() { var serviceConfiguration = ServiceConfiguration.ConfigurationModule .Set(ServiceConfiguration.Services, GenericType <TestService> .Class) .Build(); var serviceInjector = TangFactory.GetTang().NewInjector(serviceConfiguration); var contextConfig = GetContextConfiguration(); TestService testService; using (var contextRuntime = new ContextRuntime(serviceInjector, contextConfig, Optional <ContextRuntime> .Empty())) { var servicesFromInjector = serviceInjector.GetNamedInstance <ServicesSet, ISet <object> >(); testService = servicesFromInjector.Single() as TestService; Assert.NotNull(testService); if (testService == null) { // Not possible return; } var testServiceFromInjector = serviceInjector.GetInstance <TestService>(); Assert.True(ReferenceEquals(testService, testServiceFromInjector)); var contextTestService = contextRuntime.ContextInjector.GetInstance <TestService>(); Assert.True(ReferenceEquals(contextTestService, testServiceFromInjector)); } Assert.True(testService.Disposed); }
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())); } } }
public void TestTwoSuccessiveTasksOnSameContext() { 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.OnTaskStop, GenericType <TestTask> .Class) .Set(TaskConfiguration.Identifier, "ID") .Build(); var hbMgr = Substitute.For <IHeartBeatManager>(); contextRuntime.ContextInjector.BindVolatileInstance(GenericType <IHeartBeatManager> .Class, hbMgr); var taskThread = contextRuntime.StartTaskOnNewThread(taskConfig); var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask; if (testTask == null) { throw new Exception(); } testTask.StartEvent.Wait(); testTask.CountDownEvent.Signal(); testTask.StopEvent.Wait(); taskThread.Join(); Assert.False(contextRuntime.GetTaskStatus().IsPresent()); taskThread = contextRuntime.StartTaskOnNewThread(taskConfig); var secondTestTask = contextRuntime.TaskRuntime.Value.Task as TestTask; if (secondTestTask == null) { throw new Exception(); } secondTestTask.StartEvent.Wait(); Assert.Equal(contextRuntime.GetTaskStatus().Value.state, State.RUNNING); Assert.False(ReferenceEquals(testTask, secondTestTask)); secondTestTask.CountDownEvent.Signal(); secondTestTask.StopEvent.Wait(); taskThread.Join(); Assert.False(contextRuntime.GetTaskStatus().IsPresent()); secondTestTask.DisposedEvent.Wait(); } }
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(); } } } }
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)); } } }
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>())); } } }
public void TestContextEvents() { const string hello = "Hello!"; var contextConfig = GetContextEventHandlerContextConfiguration(); var injector = TangFactory.GetTang().NewInjector(); var handler = new TestContextEventHandler(); injector.BindVolatileInstance(GenericType <TestContextEventHandler> .Class, handler); using (var contextRuntime = new ContextRuntime(injector, contextConfig, Optional <ContextRuntime> .Empty())) { contextRuntime.HandleContextMessage(Encoding.UTF8.GetBytes(hello)); } Assert.True(handler.Started, "Handler did not receive the start signal."); Assert.True(handler.Stopped, "Handler did not receive the stop signal."); Assert.Equal(Encoding.UTF8.GetString(handler.MessageReceived), hello); }
public async Task TestUnableToRunMultipleTasksAtTheSameTime() { 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(); try { var hbMgr = Substitute.For <IHeartBeatManager>(); contextRuntime.ContextInjector.BindVolatileInstance(GenericType <IHeartBeatManager> .Class, hbMgr); var t = contextRuntime.StartTask(taskConfig); Assert.True(contextRuntime.TaskRuntime.IsPresent()); Assert.True(contextRuntime.GetTaskStatus().IsPresent()); Assert.Equal(contextRuntime.GetTaskStatus().Value.state, State.RUNNING); await Assert.ThrowsAsync <InvalidOperationException>( () => contextRuntime.StartTask(taskConfig)); } finally { var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask; if (testTask == null) { throw new Exception(); } testTask.CountDownEvent.Signal(); testTask.DisposedEvent.Wait(); } } }