コード例 #1
0
        public void WaitAll_SingleThread_MultipleTasks()
        {
            var workerPool = new ParallelTaskWorkerPool(1);
            var task1      = new BusyTask();
            var task2      = new BusyTask();

            workerPool.Enqueue(task1);
            workerPool.Enqueue(task2);
            workerPool.Start();

            Assert.IsFalse(workerPool.WaitAll(10),
                           "Threads should not have exited, 2 tasks are in progress");

            Assert.AreEqual(BusyTaskState.Executing, task1.State);
            Assert.AreEqual(BusyTaskState.Queued, task2.State);

            task1.MarkTaskAsCompleted();

            Assert.IsFalse(workerPool.WaitAll(10),
                           "Threads should not have exited, 1 task is in progress");

            Assert.AreEqual(BusyTaskState.Completed, task1.State);
            Assert.AreEqual(BusyTaskState.Executing, task2.State);

            task2.MarkTaskAsCompleted();

            Assert.IsTrue(workerPool.WaitAll(100),
                          "Threads should have exited, all work is complete");

            Assert.AreEqual(BusyTaskState.Completed, task1.State);
            Assert.AreEqual(BusyTaskState.Completed, task2.State);
        }
コード例 #2
0
        public void WaitAll_TwoThreads_MultipleTasks()
        {
            var workerPool = new ParallelTaskWorkerPool(2);
            var task1      = new BusyTask();
            var task2      = new BusyTask();

            workerPool.Enqueue(task1);
            workerPool.Enqueue(task2);
            workerPool.Start();

            Assert.That(workerPool.WaitAll(10), Is.False, "Threads should not have exited, 2 tasks are in progress");

            Assert.That(task1.State, Is.EqualTo(BusyTaskState.Executing));
            Assert.That(task2.State, Is.EqualTo(BusyTaskState.Executing));

            task1.MarkTaskAsCompleted();

            Assert.That(workerPool.WaitAll(10), Is.False, "Threads should not have exited, 1 task is in progress");

            Assert.That(task1.State, Is.EqualTo(BusyTaskState.Completed));
            Assert.That(task2.State, Is.EqualTo(BusyTaskState.Executing));

            task2.MarkTaskAsCompleted();

            Assert.That(workerPool.WaitAll(100), Is.True, "Threads should have exited, all work is complete");

            Assert.That(task1.State, Is.EqualTo(BusyTaskState.Completed));
            Assert.That(task2.State, Is.EqualTo(BusyTaskState.Completed));
        }
コード例 #3
0
        public void WaitAll_SingleThread_MultipleTasks()
        {
            var workerPool = new ParallelTaskWorkerPool(1);
            var task1 = new BusyTask();
            var task2 = new BusyTask();
            workerPool.Enqueue(task1);
            workerPool.Enqueue(task2);
            workerPool.Start();

            Assert.IsFalse(workerPool.WaitAll(10),
                "Threads should not have exited, 2 tasks are in progress");

            Assert.AreEqual(BusyTaskState.Executing, task1.State);
            Assert.AreEqual(BusyTaskState.Queued, task2.State);

            task1.MarkTaskAsCompleted();

            Assert.IsFalse(workerPool.WaitAll(10),
                "Threads should not have exited, 1 task is in progress");

            Assert.AreEqual(BusyTaskState.Completed, task1.State);
            Assert.AreEqual(BusyTaskState.Executing, task2.State);

            task2.MarkTaskAsCompleted();

            Assert.IsTrue(workerPool.WaitAll(100),
                "Threads should have exited, all work is complete");

            Assert.AreEqual(BusyTaskState.Completed, task1.State);
            Assert.AreEqual(BusyTaskState.Completed, task2.State);
        }
コード例 #4
0
            public void AddWithTaskWorks()
            {
                // prepare
                var task   = new BusyTask();
                var target = new BusyTaskCollection();

                // execute
                target.Add(task);

                // varify
                Assert.AreEqual(task, target[0]);
            }
コード例 #5
0
            public void IndexerFailsWithWrongKey()
            {
                // prepare
                string   key    = "aaaa";
                var      target = new BusyTaskCollection();
                BusyTask result = null;

                // execute
                result = target[key];

                // verify
                Assert.IsNull(result);
            }
コード例 #6
0
        public void WaitAll_SingleTask()
        {
            var workerPool = new ParallelTaskWorkerPool(1);
            var task       = new BusyTask();

            workerPool.Enqueue(task);
            workerPool.Start();

            Assert.That(workerPool.WaitAll(10), Is.False, "Threads should not have exited, work is in progress");

            task.MarkTaskAsCompleted();

            Assert.That(workerPool.WaitAll(100), Is.True, "Threads should have exited, all work is complete");
        }
コード例 #7
0
        public void WaitAll_SingleTask()
        {
            var workerPool = new ParallelTaskWorkerPool(1);
            var task = new BusyTask();
            workerPool.Enqueue(task);
            workerPool.Start();

            Assert.IsFalse(workerPool.WaitAll(10),
                "Threads should not have exited, work is in progress");

            task.MarkTaskAsCompleted();

            Assert.IsTrue(workerPool.WaitAll(100),
                "Threads should have exited, all work is complete");
        }
コード例 #8
0
            public void IndexerWorks()
            {
                // prepare
                string key    = "aaaa";
                var    task   = new BusyTask(key);
                var    target = new BusyTaskCollection();

                target.Add(task);
                BusyTask result = null;

                // execute
                result = target[key];

                // verify
                Assert.AreSame(task, result);
            }
コード例 #9
0
            public void UpdateWorks()
            {
                // prepare
                string key     = "aaaa";
                string message = "Hello world";
                var    task    = new BusyTask(key);
                var    target  = new BusyTaskCollection();

                target.Add(task);

                // execute
                target.Update(key, message, true, BusyTaskType.Error);

                // verify
                Assert.AreEqual(message, task.Message);
                Assert.IsTrue(task.IsProcessing);
                Assert.AreEqual(BusyTaskType.Error, task.Type);
            }
コード例 #10
0
        /// <summary>
        /// Initialize a task.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isGlobal"></param>
        protected BusyTask CreateTask(string key, bool isGlobal)
        {
            var task = Tasks[key];

            if (task != null)
            {
                throw new ArgumentException("a task with this key already exists", "key");
            }
            else
            {
                task = new BusyTask
                {
                    IsGlobal = isGlobal,
                    Key      = key,
                };
                this.Tasks.Add(task);
                return(task);
            }
        }
コード例 #11
0
        /// <summary>
        /// Update a task status.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isProcessing"></param>
        /// <param name="type"></param>
        /// <param name="message"></param>
        protected void UpdateTask(string key, string message = null, bool isProcessing = false, BusyTaskType type = BusyTaskType.Default)
        {
            var task = Tasks[key];

            if (task != null)
            {
                Tasks.Update(key, message, isProcessing, type);
            }
            else
            {
                task = new BusyTask
                {
                    Key          = key,
                    IsGlobal     = false,
                    IsProcessing = isProcessing,
                    Message      = message,
                };
                Tasks.Add(task);
            }
        }