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 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(); 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); Assert.Throws <InvalidOperationException>(() => contextRuntime.StartTaskOnNewThread(taskConfig)); } finally { var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask; if (testTask == null) { throw new Exception(); } testTask.CountDownEvent.Signal(); if (taskThread != null) { taskThread.Join(); } } } }