Пример #1
0
        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);
                contextRuntime.StartTask(taskConfig);
                var testTask = contextRuntime.TaskRuntime.Value.Task as TestTask;
                if (testTask == null)
                {
                    throw new Exception();
                }

                testTask.CountDownEvent.Signal();
                testTask.StopEvent.Wait();
                Assert.False(contextRuntime.GetTaskStatus().IsPresent());
                testTask.DisposedEvent.Wait();

                contextRuntime.StartTask(taskConfig);
                Assert.Equal(contextRuntime.GetTaskStatus().Value.state, State.RUNNING);

                var secondTestTask = contextRuntime.TaskRuntime.Value.Task as TestTask;
                if (secondTestTask == null)
                {
                    throw new Exception();
                }

                Assert.False(ReferenceEquals(testTask, secondTestTask));

                secondTestTask.CountDownEvent.Signal();
                secondTestTask.StopEvent.Wait();
                Assert.False(contextRuntime.GetTaskStatus().IsPresent());
                secondTestTask.DisposedEvent.Wait();
            }
        }
Пример #2
0
        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();
                }
            }
        }