public void TestMethodOneTask()
        {
            ThreadPool.ThreadPool threadPool = new ThreadPool.ThreadPool(new Random(), 4);
            MyTask <String>       task       = new MyTask <String>(() => "Hello");

            threadPool.Enqueue(task);
            threadPool.Start();
            Thread.Sleep(1000);
            threadPool.Dispose();
            Assert.AreEqual("Hello", task.Result());
        }
        public void TestMethodManyPools()
        {
            ThreadPool.ThreadPool threadPool = new ThreadPool.ThreadPool(new Random(), 20);
            var task1 = new MyTask <int>(() => 2 + 2);
            var task2 = task1.ContinueWith(y => y + 1);
            var task3 = task2.ContinueWith(y => y * 2);

            threadPool.Enqueue(task1);
            threadPool.Enqueue(task2);
            threadPool.Enqueue(task3);
            threadPool.Start();
            Assert.AreEqual(20, threadPool.ThreadAmount());
            Thread.Sleep(1000);
            threadPool.Dispose();
            Assert.AreEqual(0, threadPool.ThreadAmount());
        }
        public void TestMethodContinueWith()
        {
            ThreadPool.ThreadPool threadPool = new ThreadPool.ThreadPool(new Random(), 4);
            var task1 = new MyTask <int>(() => 2 + 2);
            var task2 = task1.ContinueWith(y => y + 1);
            var task3 = task2.ContinueWith(y => y * 2);

            threadPool.Enqueue(task1);
            threadPool.Enqueue(task2);
            threadPool.Enqueue(task3);
            threadPool.Start();
            Thread.Sleep(1000);
            threadPool.Dispose();
            Assert.AreEqual(4, task1.Result());
            Assert.AreEqual(5, task2.Result());
            Assert.AreEqual(10, task3.Result());
        }
        public void TestMethodSomeTask()
        {
            ThreadPool.ThreadPool threadPool = new ThreadPool.ThreadPool(new Random(), 4);
            MyTask <int>          task1      = new MyTask <int>(() => 2 + 2);
            MyTask <int>          task2      = new MyTask <int>(() => { int x = 0; return(1 / x); });
            MyTask <String>       task3      = new MyTask <String>(() => "Task3");

            threadPool.Enqueue(task1);
            threadPool.Enqueue(task2);
            threadPool.Enqueue(task3);
            threadPool.Start();
            Thread.Sleep(1000);
            threadPool.Dispose();
            Assert.AreEqual(4, task1.Result());
            Assert.ThrowsException <AggregateException>(() => task2.Execute());
            Assert.AreEqual("Task3", task3.Result());
        }
        public void TasksConsistencyTest(int threadCount,int taskCount,Func<Action> taskPayloadProvider)
        {
            var pool = new ThreadPool.ThreadPool(threadCount);
            var random = new Random();
            int completedTasksCount = 0;

            for (int i = 0; i < taskCount;i++)
            {
                pool.Execute(new Task(()=>{
                                              completedTasksCount++;
                                              taskPayloadProvider.Invoke().Invoke();
                                          }),
                             (Priority) random.Next(0, 2));
            }

            pool.Stop();

            Assert.AreEqual(taskCount,completedTasksCount);
        }
Beispiel #6
0
        public void TasksConsistencyTest(int threadCount, int taskCount, Func <Action> taskPayloadProvider)
        {
            var pool   = new ThreadPool.ThreadPool(threadCount);
            var random = new Random();
            int completedTasksCount = 0;

            for (int i = 0; i < taskCount; i++)
            {
                pool.Execute(new Task(() => {
                    completedTasksCount++;
                    taskPayloadProvider.Invoke().Invoke();
                }),
                             (Priority)random.Next(0, 2));
            }

            pool.Stop();

            Assert.AreEqual(taskCount, completedTasksCount);
        }
        public void TasksPriorityTest()
        {
            var pool = new ThreadPool.ThreadPool(2);
            var random = new Random();
            var priorityChecker = new PrioritySequenceChecker();

            for (int i = 0; i < 100; i++)
            {
                var priority = (Priority)random.Next(0, 2);
                priorityChecker.BuildSequence(priority);

                var task = new Task(() =>
                                        {
                                            Thread.Sleep(random.Next(10,20));
                                            Assert.AreEqual(true, priorityChecker.CheckSequence(priority));
                                        });

                pool.Execute(task, priority);
            }

            pool.Stop();
        }
Beispiel #8
0
        public void TasksPriorityTest()
        {
            var pool            = new ThreadPool.ThreadPool(2);
            var random          = new Random();
            var priorityChecker = new PrioritySequenceChecker();

            for (int i = 0; i < 100; i++)
            {
                var priority = (Priority)random.Next(0, 2);
                priorityChecker.BuildSequence(priority);

                var task = new Task(() =>
                {
                    Thread.Sleep(random.Next(10, 20));
                    Assert.AreEqual(true, priorityChecker.CheckSequence(priority));
                });

                pool.Execute(task, priority);
            }

            pool.Stop();
        }
Beispiel #9
0
 private void Init(int capacity)
 {
     Debug.WriteLine($"\tinit threadpool with cap: {capacity}");
     _pool = new ThreadPool.ThreadPool(capacity);
 }