1 22 package org.jboss.test.util.test; 23 24 import java.util.Arrays ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.ArrayList ; 28 29 import org.jboss.util.threadpool.BasicThreadPool; 30 import org.jboss.util.threadpool.ThreadPoolFullException; 31 import org.jboss.util.threadpool.BlockingMode; 32 import org.jboss.logging.Logger; 33 import junit.framework.TestCase; 34 35 43 public class ThreadPoolRunnableUnitTestCase extends TestCase 44 { 45 private static Logger log = Logger.getLogger(ThreadPoolRunnableUnitTestCase.class); 46 47 48 static final int BASIC = 0; 49 50 51 static final int HOLD_START = 1; 52 53 54 HashSet startedRunnables = new HashSet (); 55 56 57 HashSet startedReleases = new HashSet (); 58 59 60 HashSet finishedRunnables = new HashSet (); 61 62 63 HashMap threadNames = new HashMap (); 64 65 70 public ThreadPoolRunnableUnitTestCase(String name) 71 { 72 super(name); 73 } 74 75 78 public void testBasic() throws Exception 79 { 80 BasicThreadPool pool = new BasicThreadPool(); 81 try 82 { 83 pool.run(new TestRunnable(BASIC, "test")); 84 waitFinished(1); 85 HashSet expected = makeExpected(new Object [] {"test"}); 86 assertEquals(expected, finishedRunnables); 87 } 88 finally 89 { 90 pool.stop(true); 91 } 92 } 93 94 97 public void testMultipleBasic() throws Exception 98 { 99 BasicThreadPool pool = new BasicThreadPool(); 100 try 101 { 102 pool.run(new TestRunnable(BASIC, "test1")); 103 pool.run(new TestRunnable(BASIC, "test2")); 104 pool.run(new TestRunnable(BASIC, "test3")); 105 waitFinished(3); 106 HashSet expected = makeExpected(new Object [] {"test1", "test2", "test3"}); 107 assertEquals(expected, finishedRunnables); 108 } 109 finally 110 { 111 pool.stop(true); 112 } 113 } 114 115 118 public void testSimplePooling() throws Exception 119 { 120 BasicThreadPool pool = new BasicThreadPool(); 121 pool.setMaximumPoolSize(1); 122 try 123 { 124 pool.run(new TestRunnable(BASIC, "test1")); 125 waitFinished(1); 126 pool.run(new TestRunnable(BASIC, "test2")); 127 waitFinished(2); 128 assertEquals(threadNames.get("test1"), threadNames.get("test2")); 129 } 130 finally 131 { 132 pool.stop(true); 133 } 134 } 135 136 139 public void testMultiplePooling() throws Exception 140 { 141 BasicThreadPool pool = new BasicThreadPool(); 142 try 143 { 144 pool.run(new TestRunnable(HOLD_START, "test1")); 145 waitStarted(1); 146 pool.run(new TestRunnable(BASIC, "test2")); 147 waitFinished(1); 148 releaseStarted("test1"); 149 waitFinished(2); 150 assertTrue("Shouldn't run on the same thread", threadNames.get("test1").equals(threadNames.get("test2")) == false); 151 } 152 finally 153 { 154 pool.stop(true); 155 } 156 } 157 158 161 public void testMaximumPool() throws Exception 162 { 163 BasicThreadPool pool = new BasicThreadPool(); 164 pool.setMaximumPoolSize(1); 165 try 166 { 167 pool.run(new TestRunnable(HOLD_START, "test1")); 168 waitStarted(1); 169 pool.run(new TestRunnable(BASIC, "test2")); 170 Thread.sleep(1000); 171 assertEquals(0, finishedRunnables.size()); 172 releaseStarted("test1"); 173 waitFinished(2); 174 assertEquals(makeExpected(new Object [] {"test1", "test2"}), finishedRunnables); 175 } 176 finally 177 { 178 pool.stop(true); 179 } 180 } 181 182 185 public void testMaximumQueue() throws Exception 186 { 187 BasicThreadPool pool = new BasicThreadPool(); 188 pool.setMaximumQueueSize(1); 189 pool.setMaximumPoolSize(1); 190 try 191 { 192 pool.run(new TestRunnable(HOLD_START, "test1")); 193 waitStarted(1); 194 pool.run(new TestRunnable(BASIC, "test2")); 195 196 boolean caught = false; 197 try 198 { 199 pool.run(new TestRunnable(BASIC, "test3")); 200 } 201 catch (ThreadPoolFullException expected) 202 { 203 caught = true; 204 } 205 assertTrue("Expected ThreadPoolFullException", caught); 206 207 releaseStarted("test1"); 208 waitFinished(2); 209 assertEquals(makeExpected(new Object [] {"test1", "test2"}), finishedRunnables); 210 } 211 finally 212 { 213 pool.stop(true); 214 } 215 } 216 217 220 public void testRunnableTimeout() throws Exception 221 { 222 BasicThreadPool pool = new BasicThreadPool(); 223 pool.setMaximumQueueSize(1); 224 pool.setMaximumPoolSize(1); 225 try 226 { 227 TestRunnable test = new TestRunnable(HOLD_START, "test1", 12*1000); 228 pool.run(test, 0, 10*1000); 229 waitStarted(1); 230 releaseStarted("test1"); 231 waitFinished(1); 232 assertEquals(makeExpected(new Object [] {"test1"}), finishedRunnables); 233 } 234 finally 235 { 236 pool.stop(true); 237 } 238 } 239 240 243 public void testRunnableTimeoutWithSpinLoop() throws Exception 244 { 245 BasicThreadPool pool = new BasicThreadPool(); 246 pool.setMaximumQueueSize(1); 247 pool.setMaximumPoolSize(1); 248 try 249 { 250 TestRunnable test = new TestRunnable(HOLD_START, "test1", Long.MAX_VALUE); 251 pool.run(test, 0, 10*1000); 252 waitStarted(1); 253 releaseStarted("test1"); 254 Thread.sleep(11*1000); 255 pool.run(new TestRunnable(BASIC, "test2")); 257 waitStarted(1); 258 releaseStarted("test2"); 259 waitFinished(1); 260 assertEquals(makeExpected(new Object [] {"test2"}), finishedRunnables); 261 } 262 finally 263 { 264 pool.stop(true); 265 } 266 } 267 268 271 public void testRunnableTimeoutWithSpinLoop2() throws Exception 272 { 273 BasicThreadPool pool = new BasicThreadPool(); 274 pool.setMaximumQueueSize(1); 275 pool.setMaximumPoolSize(1); 276 pool.setBlockingMode(BlockingMode.RUN); 277 try 278 { 279 TestRunnable test = new TestRunnable(BASIC, "testx", Long.MAX_VALUE); 280 pool.run(test, 0, 1*1000); 281 ArrayList tmp = new ArrayList (); 283 for(int n = 0; n < 10; n ++) 284 { 285 String name = "test"+n; 286 pool.run(new TestRunnable(BASIC, name)); 287 tmp.add(name); 288 } 289 Thread.sleep(3000); 290 assertEquals(makeExpected(tmp.toArray()), finishedRunnables); 291 } 292 finally 293 { 294 pool.stop(true); 295 } 296 } 297 298 304 public synchronized void saveRunnableThreadName(String data, String name) 305 { 306 threadNames.put(data, name); 307 } 308 309 312 public synchronized void waitStarted(int target) 313 throws InterruptedException 314 { 315 log.info("waitStarted, target="+target); 316 while (startedRunnables.size() < target) 317 wait(); 318 } 319 320 325 public synchronized void releaseStarted(String data) 326 { 327 log.info("releaseStarted, data="+data); 328 startedReleases.add(data); 329 notifyAll(); 330 } 331 332 335 public synchronized void waitForReleaseStarted(String data) 336 { 337 try 338 { 339 log.info("waitForReleaseStarted, data="+data); 340 while (startedReleases.contains(data) == false) 341 wait(); 342 } 343 catch (InterruptedException ignored) 344 { 345 } 346 } 347 348 351 public synchronized void notifyStarted(String data) 352 { 353 log.info("notifyStarted, data="+data); 354 startedRunnables.add(data); 355 notifyAll(); 356 } 357 358 361 public synchronized void clearStarted() 362 { 363 log.info("clearStarted"); 364 startedRunnables.clear(); 365 } 366 367 370 public synchronized void waitFinished(int target) 371 throws InterruptedException 372 { 373 log.info("waitFinished, target="+target); 374 while (finishedRunnables.size() < target) 375 wait(); 376 } 377 378 381 public synchronized void notifyFinished(String data) 382 { 383 log.info("notifyFinished, data="+data); 384 finishedRunnables.add(data); 385 notifyAll(); 386 } 387 388 391 public synchronized void clearFinished() 392 { 393 log.info("clearFinished"); 394 finishedRunnables.clear(); 395 } 396 397 403 public HashSet makeExpected(Object [] expected) 404 { 405 return new HashSet (Arrays.asList(expected)); 406 } 407 408 411 public class TestRunnable implements Runnable 412 { 413 414 private int test; 415 416 private String data; 417 private long runSleepTime; 418 419 425 public TestRunnable(int test, String data) 426 { 427 this(test, data, 0); 428 } 429 public TestRunnable(int test, String data, long runSleepTime) 430 { 431 this.test = test; 432 this.data = data; 433 this.runSleepTime = runSleepTime; 434 } 435 436 439 public void run() 440 { 441 log.info("Begin run"); 442 saveThreadName(); 443 started(); 444 if( runSleepTime > 0 ) 445 { 446 log.info("Begin spin loop"); 447 if( runSleepTime == Long.MAX_VALUE ) 448 { 449 while( true ) 450 ; 451 } 452 else 453 { 454 log.info("Begin sleep"); 455 try 456 { 457 Thread.sleep(runSleepTime); 458 } 459 catch(InterruptedException e) 460 { 461 } 462 } 463 } 464 finished(); 465 log.info("End run"); 466 } 467 468 471 public void saveThreadName() 472 { 473 saveRunnableThreadName(data, Thread.currentThread().getName()); 474 } 475 476 479 public void started() 480 { 481 notifyStarted(data); 482 if (test == HOLD_START) 483 waitForReleaseStarted(data); 484 } 485 486 489 public void finished() 490 { 491 notifyFinished(data); 492 } 493 } 494 } 495 | Popular Tags |