コード例 #1
0
        public void TestWorkQueueLengthBusyCountAndWorkCancellation()
        {
            DoWorkHandler workHandler = new DoWorkHandler(delegate() { Thread.Sleep(300); });

            Console.Out.WriteLine("----=<TestWorkItemsProcessed>=----");
            List <IWork>        workList = new List <IWork>();
            ThreadPoolStartInfo tpsi     = new ThreadPoolStartInfo(1, 1, 10);

            _pool = new ThreadPool(tpsi);
            SetupLogging();

            _pool.Add(workHandler);
            Thread.Sleep(10);
            Assert.AreEqual(1, _pool.BusyThreadCount);
            for (int i = 0; i < 10; i++)
            {
                workList.Add(_pool.Add(workHandler));
            }
            Assert.AreEqual(10, _pool.QueueLength);

            foreach (IWork work in workList)
            {
                work.State = WorkState.CANCELED;
            }
            _pool.MinimumThreads = _pool.MaximumThreads = 10;
            _pool.MinimumThreads = 0;

            while (_pool.BusyThreadCount > 0)
            {
                Thread.Sleep(100);
            }

            foreach (IWork work in workList)
            {
                Assert.AreEqual(WorkState.CANCELED, work.State);
            }
            Assert.AreEqual(0, _pool.QueueLength);
            Assert.AreEqual(0, _pool.BusyThreadCount);
        }
コード例 #2
0
 public void TestMaxThreads()
 {
     Console.Out.WriteLine("----=<TestMaxThreads>=----");
     for (int i = 0; i < 10; i++)
     {
         _pool.Add(new DoWorkHandler(delegate() { Thread.Sleep(300); }));
         Thread.Sleep(10);
     }
     // System.Threading.Thread.Sleep(600);
     Assert.AreEqual(5, _pool.BusyThreadCount, "Maximum number of 5 threads exceeded");
     _pool.Stop();
 }
コード例 #3
0
        public void TestDropBackToMinThreads()
        {
            Console.Out.WriteLine("----=<TestDropBackToMinThreads>=----");
            ThreadPoolStartInfo tpsi = new ThreadPoolStartInfo(2, 10, 100);

            _pool = new ThreadPool(tpsi);
            SetupLogging();
            for (int i = 0; i < 10; i++)
            {
                _pool.Add(new DoWorkHandler(delegate() { Thread.Sleep(300); }), "TestDropBackToMinThreads" + i);
                Thread.Sleep(10);
            }
            Thread.Sleep(600);
            Assert.AreEqual(0, _pool.BusyThreadCount);
            Assert.AreEqual(10, _pool.WorkItemsProcessed);
            Assert.AreEqual(2, _pool.ThreadCount, "Pool did not drop back down to 2 threads");
            _pool.Stop();
        }
コード例 #4
0
    public void TestWorkQueueLengthBusyCountAndWorkCancellation()
    {
      DoWorkHandler workHandler = new DoWorkHandler(delegate() { Thread.Sleep(300); });
      Console.Out.WriteLine("----=<TestWorkItemsProcessed>=----");
      List<IWork> workList = new List<IWork>();
      ThreadPoolStartInfo tpsi = new ThreadPoolStartInfo(1, 1, 10);
      _pool = new ThreadPool(tpsi);
      SetupLogging();

      _pool.Add(workHandler);
      Thread.Sleep(10);
      Assert.AreEqual(1, _pool.BusyThreadCount);
      for (int i = 0; i < 10; i++)
      {
        workList.Add(_pool.Add(workHandler));
      }
      Assert.AreEqual(10, _pool.QueueLength);

      foreach (IWork work in workList)
      {
        work.State = WorkState.CANCELED;
      }
      _pool.MinimumThreads = _pool.MaximumThreads = 10;
      _pool.MinimumThreads = 0;

      while (_pool.BusyThreadCount > 0)
      {
        Thread.Sleep(100);
      }

      foreach (IWork work in workList)
      {
        Assert.AreEqual(WorkState.CANCELED, work.State);
      }
      Assert.AreEqual(0, _pool.QueueLength);
      Assert.AreEqual(0, _pool.BusyThreadCount);
    }
コード例 #5
0
 public void TestDropBackToMinThreads()
 {
   Console.Out.WriteLine("----=<TestDropBackToMinThreads>=----");
   ThreadPoolStartInfo tpsi = new ThreadPoolStartInfo(2, 10, 100);
   _pool = new ThreadPool(tpsi);
   SetupLogging();
   for (int i = 0; i < 10; i++)
   {
     _pool.Add(new DoWorkHandler(delegate() { Thread.Sleep(300); }), "TestDropBackToMinThreads" + i);
     Thread.Sleep(10);
   }
   Thread.Sleep(600);
   Assert.AreEqual(0, _pool.BusyThreadCount);
   Assert.AreEqual(10, _pool.WorkItemsProcessed);
   Assert.AreEqual(2, _pool.ThreadCount, "Pool did not drop back down to 2 threads");
   _pool.Stop();
 }