KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > threading > WorkerThreadPoolTest


1 package net.javacoding.jspider.core.threading;
2
3 import junit.framework.TestCase;
4 import net.javacoding.jspider.core.SpiderContext;
5 import net.javacoding.jspider.core.task.DispatcherTask;
6 import net.javacoding.jspider.core.util.config.ConfigurationFactory;
7 import net.javacoding.jspider.mockobjects.*;
8 import net.javacoding.jspider.mockobjects.util.Counter;
9
10
11
12 /**
13  * Unit tests for the WorkerThreadPool class.
14  *
15  * $Id: WorkerThreadPoolTest.java,v 1.11 2003/04/03 15:57:24 vanrogu Exp $
16  *
17  * @author Günther Van Roey
18  */

19 public class WorkerThreadPoolTest extends TestCase {
20
21     /** How many threads are in the pools used during these tests. */
22     public final int POOL_SIZE = 5;
23
24     /**
25      * Public constructor giving a name to the test.
26      */

27     public WorkerThreadPoolTest ( ) {
28         super ( "workerThreadTest ");
29         // make sure we're using the 'unittest' configuration
30
ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
31     }
32
33     /**
34      * JUnit's overridden setUp method
35      * @throws Exception in case something fails during setup
36      */

37     protected void setUp() throws Exception JavaDoc {
38     }
39
40     /**
41      * JUnit's overridden tearDown method
42      * @throws Exception in case something fails during tearDown
43      */

44     protected void tearDown() throws Exception JavaDoc {
45     }
46
47     /**
48      * Test the instantiation and the stopping of all worker threads that
49      * are grouped in a pool.
50      */

51     public void testWorkerThreadPoolInstantiation ( ) {
52         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
53         pool.stopAll ( );
54     }
55
56     /**
57      * Tests whether the pool adheres to the size specifications passed.
58      */

59     public void testWorkerThreadPoolSize ( ) {
60         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
61         assertEquals(pool.getSize(), POOL_SIZE);
62         pool.stopAll();
63     }
64
65     /**
66      * Tests whether the occupation of a non-assigned thread pool equals zero.
67      */

68     public void testWorkerThreadOccupation ( ) {
69         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
70         assertEquals("Created fresh pool, occupation is not 0", 0, pool.getOccupation());
71         pool.stopAll();
72     }
73
74     /**
75      * Tests the dispatcher thread functionality.
76      * Dispatch some tasks and see if all threads die smoothly in time and the
77      * occupation of the pool is 0% afterwards.
78      */

79     public void testDispatcherThread ( ) throws Exception JavaDoc {
80         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
81         SpiderContext context = new SimpleSpiderContext();
82         DispatcherTask dispatcherTask = new WaitTaskDispatcherTask ( pool, context, 10, 100 );
83           synchronized ( dispatcherTask ) {
84               try {
85                   pool.assignGroupTask(dispatcherTask);
86                   // This SHOULDN'T take 10 seconds, even on the slowest machine!
87
// After 10 seconds, we can assume something is broke, and our
88
// threads hang.
89
dispatcherTask.wait(10000);
90               } catch (InterruptedException JavaDoc e) {
91                   Thread.currentThread().interrupt();
92               }
93           }
94         pool.stopAll();
95         assertEquals("Finished jobs, threadPool occupation is not 0%", 0, pool.getOccupation());
96     }
97
98     /**
99      * Test method that tests the raw Thread Pool (without dispatcher).
100      * Small set.
101      */

102     public void testThreadedCountingNonDispatchedSmall ( ) {
103         doTestThreadedCountingNonDispatched ( 10 );
104     }
105
106     /**
107      * Test method that tests the raw Thread Pool (without dispatcher).
108      * large set.
109      */

110     public void testThreadedCountingNonDispatchedLarge ( ) {
111         doTestThreadedCountingNonDispatched ( 50000 );
112     }
113
114     /**
115      * Test method that tests the raw Thread Pool (without dispatcher).
116      * @param number number of tasks to be carried out
117      */

118     public void doTestThreadedCountingNonDispatched ( int number ) {
119         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
120         Counter counter = new Counter();
121         for ( int i = 0; i < number; i++ ) {
122             pool.assign(new CountTask(counter));
123         }
124         pool.stopAll();
125         assertEquals("Counter jobs finished, counter value is not " + number, number, counter.getValue());
126     }
127
128     /**
129      * Uses the ThreadPool (with dispatcher thread).
130      * Small test set.
131      */

132     public void testThreadedCountingDispatchedSmall ( ) throws Exception JavaDoc {
133         doTestThreadedCountingDispatched(10);
134     }
135
136     /**
137      * Uses the ThreadPool (with dispatcher thread).
138      * Large test set.
139      */

140     public void testThreadedCountingDispatchedLarge ( ) throws Exception JavaDoc {
141         doTestThreadedCountingDispatched(50000);
142     }
143
144     /**
145      * Test method that tests the Thread Pool (with dispatcher).
146      * @param number number of tasks to be carried out
147      */

148     public void doTestThreadedCountingDispatched ( int number ) throws Exception JavaDoc {
149         WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE );
150         SpiderContext context = new SimpleSpiderContext();
151         Counter counter = new Counter();
152         DispatcherTask dispatcherTask = new CountTaskDispatcherTask ( context, pool, counter, number );
153          synchronized ( dispatcherTask ) {
154               pool.assignGroupTask(dispatcherTask);
155               try {
156                   dispatcherTask.wait();
157               } catch (InterruptedException JavaDoc e) {
158                   Thread.currentThread().interrupt();
159               }
160           }
161         pool.stopAll();
162         assertEquals("Counter jobs finished, counter value is not " + number, number, counter.getValue());
163     }
164
165 }
Popular Tags