Exemplo n.º 1
0
        public void MoreThenThreadsTest()
        {
            int FuncOne() => 1;
            int FuncTwo() => 2;
            int FuncThree() => 3;

            var pool      = new MyThreadPool(2);
            var taskOne   = pool.AddTask(FuncOne);
            var taskTwo   = pool.AddTask(FuncTwo);
            var taskThree = pool.AddTask(FuncThree);

            Assert.Equal(1, taskOne.Result);
            Assert.Equal(2, taskTwo.Result);
            Assert.Equal(3, taskThree.Result);
        }
Exemplo n.º 2
0
        public void ExceptionTest()
        {
            int del()
            {
                int b = 0;
                int a = 5 / b;

                return(a);
            }

            var pool = new MyThreadPool(5);
            var task = pool.AddTask(del);

            Assert.Throws <AggregateException>(() => task.Result);

            try
            {
                var result = task.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    Assert.ThrowsAsync <DivideByZeroException>(() => throw ex);
                }
            }
        }
Exemplo n.º 3
0
        public void MyThreadPoolShouldWorkWithDifferentTypes()
        {
            const int numOfThreads = 4;
            var       threadPool   = new MyThreadPool(numOfThreads);

            int firstVariable = 1;
            var firstTask     = threadPool.AddTask(() => firstVariable + firstVariable);

            string secondVariable = "1";
            var    secondTask     = threadPool.AddTask(() => secondVariable + secondVariable);

            const int    firstTrueResult  = 2;
            const string secondTrueResult = "11";

            Assert.AreEqual(firstTrueResult, firstTask.Result);
            Assert.AreEqual(secondTrueResult, secondTask.Result);
        }
Exemplo n.º 4
0
        public void MyThreadPoolShouldWorkWithIncorrectFunction()
        {
            const int numOfThreads = 4;
            var       threadPool   = new MyThreadPool(numOfThreads);

            int variable = 2;
            var result   = threadPool.AddTask(() => variable / 0).Result;
        }
Exemplo n.º 5
0
        public void OneTaskTest()
        {
            int SimpleFunction() => 21;

            var pool     = new MyThreadPool(5);
            var nullTask = pool.AddTask(SimpleFunction);

            Assert.Equal(21, nullTask.Result);
        }
Exemplo n.º 6
0
        public void NullFunctionTest()
        {
            bool?NullFunction() => null;

            var pool     = new MyThreadPool(5);
            var nullTask = pool.AddTask(NullFunction);

            Assert.Null(nullTask.Result);
        }
Exemplo n.º 7
0
        public void IsCompletedTest()
        {
            int SleepFunction()
            {
                Thread.Sleep(500);
                return(25);
            }

            var pool     = new MyThreadPool(5);
            var nullTask = pool.AddTask(SleepFunction);

            Assert.False(nullTask.IsCompleted);
        }
Exemplo n.º 8
0
        public void CancellationTest()
        {
            int BasicFunc() => 10;

            var pool = new MyThreadPool(4);
            var task = pool.AddTask(BasicFunc);

            pool.Shutdown();
            Thread.Sleep(50);

            Assert.Equal(10, task.Result);
            Assert.Equal(0, pool.ThreadsCount());
        }
Exemplo n.º 9
0
        public void CheckMethodContinueWith()
        {
            const int numOfThreads = 4;
            var       threadPool   = new MyThreadPool(numOfThreads);

            int variable   = 1;
            var firstTask  = threadPool.AddTask(() => variable + variable);
            var secondTask = firstTask.ContinueWith((int var) => Math.Sqrt(var));

            double trueResult = Math.Sqrt(variable + variable);

            Assert.AreEqual(trueResult, secondTask.Result);
        }
Exemplo n.º 10
0
        public void ContinueWithTest()
        {
            int BasicFunc() => 10;

            int Add(int a)
            {
                return(a + 10);
            }

            var pool    = new MyThreadPool(4);
            var taskOne = pool.AddTask(BasicFunc);
            var taskTwo = taskOne.ContinueWith(Add);

            Assert.Equal(10, taskOne.Result);
            Assert.Equal(20, taskTwo.Result);
        }
Exemplo n.º 11
0
        public void CheckCountOfActiveThreads()
        {
            const int numOfThreads = 100;
            var       threadPool   = new MyThreadPool(numOfThreads);

            Assert.AreEqual(numOfThreads, threadPool.GetCountOfActiveThreads());

            const int numOfTasks = 100;

            for (int i = 0; i < numOfTasks; i++)
            {
                threadPool.AddTask(() => 1);
            }

            Assert.AreEqual(numOfThreads, threadPool.GetCountOfActiveThreads());

            threadPool.Shutdown();
            Assert.AreEqual(0, threadPool.GetCountOfActiveThreads());
        }
Exemplo n.º 12
0
        public void MyThreadPoolShouldSolveTasks()
        {
            const int numOfThreads = 4;
            const int numOfTasks   = 16;

            var threadPool = new MyThreadPool(numOfThreads);
            var tasks      = new IMyTask <int> [numOfTasks];

            int variable   = 2;
            int trueResult = variable * variable;

            for (int i = 0; i < numOfTasks; i++)
            {
                tasks[i] = threadPool.AddTask(() => variable * variable);
            }

            for (int i = 0; i < numOfTasks; i++)
            {
                Assert.AreEqual(trueResult, tasks[i].Result);
                Assert.IsTrue(tasks[i].IsCompleted);
            }
        }
Exemplo n.º 13
0
        public void SingleTaskTest()
        {
            var testTask = testPool.AddTask(() => 5);

            Assert.AreEqual(5, testTask.Result);
        }