WorkerThreadPoolTest.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:6k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.threading;
  2. import junit.framework.TestCase;
  3. import net.javacoding.jspider.core.SpiderContext;
  4. import net.javacoding.jspider.core.task.DispatcherTask;
  5. import net.javacoding.jspider.core.util.config.ConfigurationFactory;
  6. import net.javacoding.jspider.mockobjects.*;
  7. import net.javacoding.jspider.mockobjects.util.Counter;
  8. /**
  9.  * Unit tests for the WorkerThreadPool class.
  10.  *
  11.  * $Id: WorkerThreadPoolTest.java,v 1.11 2003/04/03 15:57:24 vanrogu Exp $
  12.  *
  13.  * @author  G黱ther Van Roey
  14.  */
  15. public class WorkerThreadPoolTest extends TestCase {
  16.     /** How many threads are in the pools used during these tests. */
  17.     public final int POOL_SIZE = 5;
  18.     /**
  19.      * Public constructor giving a name to the test.
  20.      */
  21.     public WorkerThreadPoolTest ( ) {
  22.         super ( "workerThreadTest ");
  23.         // make sure we're using the 'unittest' configuration
  24.         ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
  25.     }
  26.     /**
  27.      * JUnit's overridden setUp method
  28.      * @throws Exception in case something fails during setup
  29.      */
  30.     protected void setUp() throws Exception {
  31.     }
  32.     /**
  33.      * JUnit's overridden tearDown method
  34.      * @throws Exception in case something fails during tearDown
  35.      */
  36.     protected void tearDown() throws Exception {
  37.     }
  38.     /**
  39.      * Test the instantiation and the stopping of all worker threads that
  40.      * are grouped in a pool.
  41.      */
  42.     public void testWorkerThreadPoolInstantiation ( ) {
  43.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  44.         pool.stopAll ( );
  45.     }
  46.     /**
  47.      * Tests whether the pool adheres to the size specifications passed.
  48.      */
  49.     public void testWorkerThreadPoolSize ( ) {
  50.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  51.         assertEquals(pool.getSize(), POOL_SIZE);
  52.         pool.stopAll();
  53.     }
  54.     /**
  55.      * Tests whether the occupation of a non-assigned thread pool equals zero.
  56.      */
  57.     public void testWorkerThreadOccupation ( ) {
  58.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  59.         assertEquals("Created fresh pool, occupation is not 0", 0, pool.getOccupation());
  60.         pool.stopAll();
  61.     }
  62.     /**
  63.      * Tests the dispatcher thread functionality.
  64.      * Dispatch some tasks and see if all threads die smoothly in time and the
  65.      * occupation of the pool is 0% afterwards.
  66.      */
  67.     public void testDispatcherThread ( ) throws Exception {
  68.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  69.         SpiderContext context = new SimpleSpiderContext();
  70.         DispatcherTask dispatcherTask = new WaitTaskDispatcherTask ( pool, context, 10, 100 );
  71.           synchronized ( dispatcherTask ) {
  72.               try {
  73.                   pool.assignGroupTask(dispatcherTask);
  74.                   // This SHOULDN'T take 10 seconds, even on the slowest machine!
  75.                   // After 10 seconds, we can assume something is broke, and our
  76.                   // threads hang.
  77.                   dispatcherTask.wait(10000);
  78.               } catch (InterruptedException e) {
  79.                   Thread.currentThread().interrupt();
  80.               }
  81.           }
  82.         pool.stopAll();
  83.         assertEquals("Finished jobs, threadPool occupation is not 0%", 0, pool.getOccupation());
  84.     }
  85.     /**
  86.      * Test method that tests the raw Thread Pool (without dispatcher).
  87.      * Small set.
  88.      */
  89.     public void testThreadedCountingNonDispatchedSmall ( ) {
  90.         doTestThreadedCountingNonDispatched ( 10 );
  91.     }
  92.     /**
  93.      * Test method that tests the raw Thread Pool (without dispatcher).
  94.      * large set.
  95.      */
  96.     public void testThreadedCountingNonDispatchedLarge ( ) {
  97.         doTestThreadedCountingNonDispatched ( 50000 );
  98.     }
  99.     /**
  100.      * Test method that tests the raw Thread Pool (without dispatcher).
  101.      * @param number number of tasks to be carried out
  102.      */
  103.     public void doTestThreadedCountingNonDispatched ( int number ) {
  104.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  105.         Counter counter = new Counter();
  106.         for ( int i = 0; i < number; i++ ) {
  107.             pool.assign(new CountTask(counter));
  108.         }
  109.         pool.stopAll();
  110.         assertEquals("Counter jobs finished, counter value is not " + number, number, counter.getValue());
  111.     }
  112.     /**
  113.      * Uses the ThreadPool (with dispatcher thread).
  114.      * Small test set.
  115.      */
  116.     public void testThreadedCountingDispatchedSmall ( ) throws Exception {
  117.         doTestThreadedCountingDispatched(10);
  118.     }
  119.     /**
  120.      * Uses the ThreadPool (with dispatcher thread).
  121.      * Large test set.
  122.      */
  123.     public void testThreadedCountingDispatchedLarge ( ) throws Exception {
  124.         doTestThreadedCountingDispatched(50000);
  125.     }
  126.     /**
  127.      * Test method that tests the Thread Pool (with dispatcher).
  128.      * @param number number of tasks to be carried out
  129.      */
  130.     public void doTestThreadedCountingDispatched ( int number ) throws Exception {
  131.         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
  132.         SpiderContext context = new SimpleSpiderContext();
  133.         Counter counter = new Counter();
  134.         DispatcherTask dispatcherTask = new CountTaskDispatcherTask ( context, pool, counter, number );
  135.          synchronized ( dispatcherTask ) {
  136.               pool.assignGroupTask(dispatcherTask);
  137.               try {
  138.                   dispatcherTask.wait();
  139.               } catch (InterruptedException e) {
  140.                   Thread.currentThread().interrupt();
  141.               }
  142.           }
  143.         pool.stopAll();
  144.         assertEquals("Counter jobs finished, counter value is not " + number, number, counter.getValue());
  145.     }
  146. }