Пример #1
0
        public void CreateThreadsTest()
        {
            var target = CreateFixedThreadPool(4, false);
            var heavyTask = new TaskMock(Wait);

            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Wait();
            Assert.AreEqual(1, target.DeferedThreadCount);

            target.Stop();
            AssertIsProperlyStopped(target);
        }
Пример #2
0
        public void StopTest_MultiplyWaiters()
        {
            var target = CreateFixedThreadPool(2, false);
            var heavyTask = new TaskMock(Wait);
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Medium));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));

            Exception deferedException = null;
            var waitersCounter = new CountdownEvent(4);

            for (var i = 0; i < 4; i++)
            {
                new Thread(() =>
                {
                    target.Stop();
                    try
                    { // each thread should see properly stopped pool.
                        AssertIsProperlyStopped(target);
                    }
                    catch (Exception e)
                    {
                        deferedException = e;
                    }
                    waitersCounter.Signal();
                }).Start();
            }

            target.Stop();
            AssertIsProperlyStopped(target);

            waitersCounter.Wait(); // if thread pool works properly, will never blocked here

            if (deferedException != null)
            {
                throw deferedException;
            }
        }
Пример #3
0
        public void ReuseThreadsTest()
        {
            var target = CreateFixedThreadPool(5, false);
            var heavyTask = new TaskMock(Wait);

            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Wait();
            Assert.AreEqual(2, target.DeferedThreadCount);
            Wait(); // Allowing threads to finish.

            // Now will attemp to reuse it and create one new
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Wait();
            Assert.AreEqual(1, target.DeferedThreadCount);

            target.Stop();
            AssertIsProperlyStopped(target);
        }