public void ShutdownedThreadPoolTakeNewTasksTest()
 {
     for (int i = 0; i < 100; ++i)
     {
         threadPool.Submit(() => 2);
     }
     threadPool.Shutdown();
     Assert.Throws <ApplicationException>(() => threadPool.Submit(() => 2));
 }
Exemple #2
0
        public void ShutdownTestOnContinuedTask()
        {
            var preTask = pool.Enqueue(() => 10);

            pool.Shutdown();
            Thread.Sleep(1000);
            IMyTask <int> temp;

            Assert.Throws <InvalidOperationException>(() => temp = preTask.ContinueWith(x => x + 10));
        }
Exemple #3
0
        public void SubmitWithShutdownTest()
        {
            for (var i = 0; i < numberOfThreads; ++i)
            {
                threads[i] = new Thread(() =>
                {
                    try
                    {
                        manualResetEvent.WaitOne();
                        results.Enqueue(threadPool.Submit(() => 10).Result);
                    }
                    catch (InvalidOperationException) { }
                });

                threads[i].Start();
            }

            var threadWithShutdown = new Thread(() =>
            {
                manualResetEvent.WaitOne();
                threadPool.Shutdown();
            });

            threadWithShutdown.Start();

            manualResetEvent.Set();

            foreach (var thread in threads)
            {
                if (!thread.Join(100))
                {
                    Assert.Fail("Deadlock");
                }
            }
        }
        public void ContinueWithShutdownTest(int threadCount)
        {
            var pool       = new MyThreadPool(threadCount);
            var tasks      = new IMyTask <int> [threadCount + 10];
            var resetEvent = new ManualResetEvent(false);

            tasks[0] = pool.QueueTask(() =>
            {
                resetEvent.WaitOne();
                return(1);
            });

            for (var i = 1; i < tasks.Length; ++i)
            {
                tasks[i] = tasks[i - 1].ContinueWith((j) =>
                {
                    return(++j);
                });
            }

            pool.Shutdown();
            resetEvent.Set();

            for (var i = 1; i < tasks.Length; ++i)
            {
                Assert.Throws <ThreadPoolShutdownException>(() => _ = tasks[i].Result);
                Assert.IsFalse(tasks[i].IsCompleted);
            }

            while (pool.ActiveThreadCount != 0)
            {
                ;
            }
        }
Exemple #5
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());
        }
Exemple #6
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());
        }
        public void ShutdownTest(int threadCount)
        {
            var pool       = new MyThreadPool(threadCount);
            var tasks      = new List <IMyTask <int> >();
            var resetEvent = new ManualResetEvent(false);
            var counter    = 0;

            for (var i = 0; i < threadCount + 10; ++i)
            {
                tasks.Add(pool.QueueTask(() =>
                {
                    resetEvent.WaitOne();
                    Interlocked.Increment(ref counter);
                    return(0);
                }));
            }

            Thread.Sleep(1000);
            pool.Shutdown();
            resetEvent.Set();
            Thread.Sleep(1000);
            Assert.AreEqual(threadCount, counter);

            for (var i = 0; i < threadCount; ++i)
            {
                Assert.AreEqual(0, tasks[i].Result);
                Assert.IsTrue(tasks[i].IsCompleted);
            }

            for (var i = threadCount; i < threadCount + 10; ++i)
            {
                Assert.Throws <ThreadPoolShutdownException>(() => _ = tasks[i].Result);
                Assert.IsFalse(tasks[i].IsCompleted);
            }

            Assert.Throws <ThreadPoolShutdownException>(() => pool.QueueTask(() => 0));
            Assert.Throws <ThreadPoolShutdownException>(() => tasks[0].ContinueWith((i) => i * 2));

            while (pool.ActiveThreadCount != 0)
            {
                ;
            }
        }
 public void SubmitAfterShutdownTest()
 {
     threadPool.Shutdown();
     Assert.Throws <InvalidOperationException>(() => threadPool.Submit(() => 0));
 }
 public void ShutdownWorksTest()
 {
     testPool.Shutdown();
     Assert.Throws <InvalidOperationException>(() => { testPool.AddTask(() => "zdarova"); }, "ThreadPool was shut down!");
 }
Exemple #10
0
        public void NumberOfMaximumThreadsIsRight()
        {
            var set = new HashSet <string>();

            for (var i = 0; i < 10; ++i)
            {
                threadPool.QueueTask(() =>
                {
                    set.Add(Thread.CurrentThread.Name);
                    Thread.Sleep(1000);
                    return(5);
                });
            }

            Thread.Sleep(2000);
            threadPool.Shutdown();

            Assert.IsTrue(set.Count >= threadPool.NumberOfThreads);
        }