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