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 19 public class WorkerThreadPoolTest extends TestCase { 20 21 22 public final int POOL_SIZE = 5; 23 24 27 public WorkerThreadPoolTest ( ) { 28 super ( "workerThreadTest "); 29 ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST); 31 } 32 33 37 protected void setUp() throws Exception { 38 } 39 40 44 protected void tearDown() throws Exception { 45 } 46 47 51 public void testWorkerThreadPoolInstantiation ( ) { 52 WorkerThreadPool pool = new WorkerThreadPool ( "testPool", "testThread", POOL_SIZE ); 53 pool.stopAll ( ); 54 } 55 56 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 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 79 public void testDispatcherThread ( ) throws Exception { 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 dispatcherTask.wait(10000); 90 } catch (InterruptedException 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 102 public void testThreadedCountingNonDispatchedSmall ( ) { 103 doTestThreadedCountingNonDispatched ( 10 ); 104 } 105 106 110 public void testThreadedCountingNonDispatchedLarge ( ) { 111 doTestThreadedCountingNonDispatched ( 50000 ); 112 } 113 114 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 132 public void testThreadedCountingDispatchedSmall ( ) throws Exception { 133 doTestThreadedCountingDispatched(10); 134 } 135 136 140 public void testThreadedCountingDispatchedLarge ( ) throws Exception { 141 doTestThreadedCountingDispatched(50000); 142 } 143 144 148 public void doTestThreadedCountingDispatched ( int number ) throws Exception { 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 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 |