示例#1
0
        public void TestAbortOneRunningTask()
        {
            DummyTaskParameters params1 = new DummyTaskParameters();

            params1.IsBackgroundTask  = true;
            params1.Name              = "TestRunOneTask";
            params1.RunInParallel     = false;
            params1.UseSeparateThread = true;
            params1.WaitForAbort      = true;

            DummyTask task1 = new DummyTask(params1);

            // Create a task manager for the context.
            TaskManager taskManager = new TaskManager("Test");

            taskManager.Enqueue(task1);

            // Give it a second to start running the task then abort.
            Thread.Sleep(1000);
            taskManager.AbortCurrentTask(true);

            taskManager.WaitForTaskCompletion(task1, s_TaskTimeout);

            Assert.AreEqual(1, task1.ExecutionCount);
            Assert.AreNotEqual(null, task1.LastException);
            Assert.AreEqual(true, task1.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, task1.CurrentTaskState.Aborted);
            Assert.AreEqual(true, task1.CurrentTaskState.ExternalAbortRequested);
            taskManager.Dispose();
        }
示例#2
0
        public void TestTaskNotStarted()
        {
            DummyTaskParameters testParams = new DummyTaskParameters();

            testParams.Name              = "DummyTask";
            testParams.RunInParallel     = true;
            testParams.IsBackgroundTask  = true;
            testParams.UseSeparateThread = true;

            DummyTask testTask = new DummyTask(testParams);

            // Check the initialisation data has been stored ok.
            Assert.AreEqual(testParams.Name, testTask.Name);
            Assert.AreEqual(testParams.RunInParallel, testTask.InitializationData.RunInParallel);
            Assert.AreEqual(testParams.IsBackgroundTask, testTask.InitializationData.IsBackgroundTask);
            Assert.AreEqual(testParams.UseSeparateThread, testTask.InitializationData.UseSeparateThread);

            // Check the task state.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskStarted);

            // Check other properties initial state.
            Assert.AreEqual(null, testTask.LastException);
        }
示例#3
0
        public void TestTaskCompleteWithException()
        {
            // Force the task to return an exception.
            DummyTaskParameters testParams = new DummyTaskParameters();

            testParams.Name               = "DummyTask";
            testParams.RunInParallel      = true;
            testParams.IsBackgroundTask   = true;
            testParams.UseSeparateThread  = true;
            testParams.SetLastException   = true;
            testParams.LastExceptionToSet = new ArgumentException("hello");

            DummyTask testTask = new DummyTask(testParams);

            // Check the initialisation data has been stored ok.
            Assert.AreEqual(testParams.Name, testTask.Name);
            Assert.AreEqual(testParams.RunInParallel, testTask.InitializationData.RunInParallel);
            Assert.AreEqual(testParams.IsBackgroundTask, testTask.InitializationData.IsBackgroundTask);
            Assert.AreEqual(testParams.UseSeparateThread, testTask.InitializationData.UseSeparateThread);

            // Check the task state.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskStarted);

            // Check other properties initial state.
            Assert.AreEqual(null, testTask.LastException);



            testTask.Run();
            testTask.TaskStartedNotificationEvent.Set();
            testTask.Join(60000); // Shouldn't take more than 60 seconds to complete.

            Assert.AreEqual(testParams.LastExceptionToSet, testTask.LastException);
            Assert.AreEqual(true, testTask.EntryPointExecuted);

            // Check the task state.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskStarted);
        }
示例#4
0
        public void TestRunTwoConcurrentTasks()
        {
            DummyTaskParameters params1 = new DummyTaskParameters();

            params1.IsBackgroundTask  = true;
            params1.Name              = "DummyTask1";
            params1.RunInParallel     = true;
            params1.UseSeparateThread = true;
            params1.WaitForAbort      = true;

            DummyTask task1 = new DummyTask(params1);

            DummyTaskParameters params2 = new DummyTaskParameters();

            params2.IsBackgroundTask  = true;
            params2.Name              = "DummyTask2";
            params2.RunInParallel     = true;
            params2.UseSeparateThread = true;
            params2.TimeToWait        = 1000; // Wait one second.

            DummyTask task2 = new DummyTask(params2);

            // Create a task manager for the context.
            TaskManager taskManager = new TaskManager("Test");

            taskManager.RunConcurrentTask(task1);
            taskManager.RunConcurrentTask(task2);
            taskManager.WaitForTaskCompletion(task2, s_TaskTimeout);

            // Task 2 should complete first - then abort task1.
            Assert.AreEqual(1, task2.ExecutionCount);
            Assert.AreEqual(1, task1.ExecutionCount);
            Assert.AreEqual(null, task2.LastException);
            Assert.AreEqual(true, task2.CurrentTaskState.TaskCompleted);


            Assert.AreEqual(false, task1.CurrentTaskState.TaskCompleted);
            taskManager.AbortAllConcurrentTasks();
            taskManager.WaitForTaskCompletion(task1, s_TaskTimeout);

            Assert.AreEqual(null, task2.LastException);
            Assert.AreNotEqual(null, task1.LastException);
            Assert.AreEqual(true, task1.CurrentTaskState.TaskCompleted);

            taskManager.Dispose();
        }
示例#5
0
        public void TestRunOneTask()
        {
            DummyTaskParameters params1 = new DummyTaskParameters();

            params1.IsBackgroundTask  = true;
            params1.Name              = "TestRunOneTask";
            params1.RunInParallel     = false;
            params1.UseSeparateThread = true;

            DummyTask task1 = new DummyTask(params1);

            // Create a task manager for the context.
            TaskManager taskManager = new TaskManager("Test");

            taskManager.Enqueue(task1);
            taskManager.WaitForTaskCompletion(task1, s_TaskTimeout);

            Assert.AreEqual(1, task1.ExecutionCount);
            Assert.AreEqual(null, task1.LastException);
            taskManager.Dispose();
        }
示例#6
0
        public void TestRunSameTaskManyTimes()
        {
            int numTasks = 1000;

            TaskManager taskManager = new TaskManager("Test");

            DummyTaskParameters params1 = new DummyTaskParameters();

            params1.IsBackgroundTask  = false;
            params1.Name              = "TestRunOneTask1";
            params1.RunInParallel     = false;
            params1.UseSeparateThread = true;


            DummyTask [] dummyTasks = new DummyTask [numTasks];

            // Queue all the tasks.
            for (int i = 0; i < numTasks; i++)
            {
                dummyTasks[i] = new DummyTask(params1);
                taskManager.Enqueue(dummyTasks[i]);
            }

            // Wait for the last one to complete.
            taskManager.WaitForTaskCompletion(dummyTasks[numTasks - 1], s_TaskTimeout * numTasks);


            // Check all the tasks completed ok.
            for (int i = 0; i < numTasks; i++)
            {
                Assert.AreEqual(1, dummyTasks[i].ExecutionCount);
                Assert.AreEqual(null, dummyTasks[i].LastException);
                Assert.AreEqual(true, dummyTasks[i].CurrentTaskState.TaskCompleted);
            }
            taskManager.Dispose();
        }
示例#7
0
        public void TestTaskSuspendAbort()
        {
            DummyTaskParameters testParams = new DummyTaskParameters();

            testParams.Name              = "DummyTask";
            testParams.RunInParallel     = true;
            testParams.IsBackgroundTask  = true;
            testParams.UseSeparateThread = true;
            // Force the task to return an exception.
            testParams.WaitForSuspend = true;

            DummyTask testTask = new DummyTask(testParams);

            // Check the initialisation data has been stored ok.
            Assert.AreEqual(testParams.Name, testTask.Name);
            Assert.AreEqual(testParams.RunInParallel, testTask.InitializationData.RunInParallel);
            Assert.AreEqual(testParams.IsBackgroundTask, testTask.InitializationData.IsBackgroundTask);
            Assert.AreEqual(testParams.UseSeparateThread, testTask.InitializationData.UseSeparateThread);

            // Check the task state.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskStarted);

            // Check other properties initial state.
            Assert.AreEqual(null, testTask.LastException);


            // Also set the state of the task to suspended before it starts so that it
            // is guaranteed to see SuspendRequested when calling CheckSuspend.
            testTask.Suspend();
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(true, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskStarted);

            // Run the task - the task will suspend and hang waiting for a resume.
            testTask.Run();
            testTask.TaskStartedNotificationEvent.Set();

            // Wait for the task to suspend (or until we time out).
            int totalTimeWaited = 0;
            int timeoutValue    = 100;
            int maxTimeToWait   = 10000;

            while (!testTask.CurrentTaskState.Suspended)
            {
                Thread.Sleep(timeoutValue);
                totalTimeWaited += timeoutValue;
                if (totalTimeWaited > maxTimeToWait)
                {
                    Assert.AreEqual(true, totalTimeWaited < maxTimeToWait);
                }
            }

            // Should be suspended.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(true, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskStarted);


            testTask.StopExternal();

            testTask.Join(60000); // Shouldn't take more than 60 seconds to complete.

            Assert.AreNotEqual(null, testTask.LastException);

            // Check the task state.
            Assert.AreEqual(true, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(true, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(true, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskStarted);
        }
示例#8
0
        public void TestTaskInternalAbort()
        {
            DummyTaskParameters testParams = new DummyTaskParameters();

            testParams.Name              = "DummyTask";
            testParams.RunInParallel     = true;
            testParams.IsBackgroundTask  = true;
            testParams.UseSeparateThread = true;
            testParams.WaitForAbort      = true;


            DummyTask testTask = new DummyTask(testParams);

            // Check the initialisation data has been stored ok.
            Assert.AreEqual(testParams.Name, testTask.Name);
            Assert.AreEqual(testParams.RunInParallel, testTask.InitializationData.RunInParallel);
            Assert.AreEqual(testParams.IsBackgroundTask, testTask.InitializationData.IsBackgroundTask);
            Assert.AreEqual(testParams.UseSeparateThread, testTask.InitializationData.UseSeparateThread);

            // Check the task state.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskStarted);

            // Check other properties initial state.
            Assert.AreEqual(null, testTask.LastException);

            // Force the task to return an exception.
            testTask.Run();
            testTask.TaskStartedNotificationEvent.Set();

            // Wait for a second and then Abort the task.
            Thread.Sleep(1000);

            // Shouldn't be stopped yet.
            Assert.AreEqual(false, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(false, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskStarted);

            testTask.ForceCallToStopInternal();

            testTask.Join(60000); // Shouldn't take more than 60 seconds to complete.

            Assert.AreNotEqual(null, testTask.LastException);

            // Check the task state.
            Assert.AreEqual(true, testTask.CurrentTaskState.Aborted);
            Assert.AreEqual(true, testTask.CurrentTaskState.AbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.ExternalAbortRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.InternalAbortRequested);
            Assert.AreEqual(false, testTask.CurrentTaskState.Suspended);
            Assert.AreEqual(false, testTask.CurrentTaskState.SuspendRequested);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskCompleted);
            Assert.AreEqual(true, testTask.CurrentTaskState.TaskStarted);
        }