9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
import java.security.*; |
15 |
|
|
289 |
|
* threadFail with message "should throw exception" |
290 |
|
*/ |
291 |
|
public void threadShouldThrow() { |
292 |
< |
try { |
293 |
< |
threadFailed = true; |
293 |
< |
fail("should throw exception"); |
294 |
< |
} catch (AssertionFailedError e) { |
295 |
< |
e.printStackTrace(); |
296 |
< |
throw e; |
297 |
< |
} |
292 |
> |
threadFailed = true; |
293 |
> |
fail("should throw exception"); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
316 |
|
public void joinPool(ExecutorService exec) { |
317 |
|
try { |
318 |
|
exec.shutdown(); |
319 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
319 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
320 |
|
} catch (SecurityException ok) { |
321 |
|
// Allowed in case test doesn't have privs |
322 |
|
} catch (InterruptedException ie) { |
339 |
|
fail("Unexpected exception"); |
340 |
|
} |
341 |
|
|
342 |
+ |
/** |
343 |
+ |
* fail with message "Unexpected exception", with argument |
344 |
+ |
*/ |
345 |
+ |
public void unexpectedException(Throwable ex) { |
346 |
+ |
ex.printStackTrace(); |
347 |
+ |
fail("Unexpected exception: " + ex); |
348 |
+ |
} |
349 |
+ |
|
350 |
|
|
351 |
|
/** |
352 |
|
* The number of elements to place in collections, arrays, etc. |
398 |
|
|
399 |
|
// Some convenient Runnable classes |
400 |
|
|
401 |
+ |
abstract class CheckedRunnable implements Runnable { |
402 |
+ |
abstract void realRun() throws Throwable; |
403 |
+ |
|
404 |
+ |
public final void run() { |
405 |
+ |
try { |
406 |
+ |
realRun(); |
407 |
+ |
} catch (Throwable t) { |
408 |
+ |
threadUnexpectedException(t); |
409 |
+ |
} |
410 |
+ |
} |
411 |
+ |
} |
412 |
+ |
|
413 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
414 |
+ |
abstract void realRun() throws Throwable; |
415 |
+ |
|
416 |
+ |
public final void run() { |
417 |
+ |
try { |
418 |
+ |
realRun(); |
419 |
+ |
threadShouldThrow(); |
420 |
+ |
} catch (InterruptedException success) { |
421 |
+ |
} catch (Throwable t) { |
422 |
+ |
threadUnexpectedException(t); |
423 |
+ |
} |
424 |
+ |
} |
425 |
+ |
} |
426 |
+ |
|
427 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
428 |
+ |
abstract T realCall() throws Throwable; |
429 |
+ |
|
430 |
+ |
public final T call() { |
431 |
+ |
try { |
432 |
+ |
return realCall(); |
433 |
+ |
} catch (Throwable t) { |
434 |
+ |
threadUnexpectedException(t); |
435 |
+ |
return null; |
436 |
+ |
} |
437 |
+ |
} |
438 |
+ |
} |
439 |
+ |
|
440 |
|
static class NoOpRunnable implements Runnable { |
441 |
|
public void run() {} |
442 |
|
} |
459 |
|
public Integer call() { return one; } |
460 |
|
} |
461 |
|
|
462 |
< |
class ShortRunnable implements Runnable { |
463 |
< |
public void run() { |
464 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
423 |
< |
} |
424 |
< |
catch (Exception e) { |
425 |
< |
threadUnexpectedException(e); |
426 |
< |
} |
462 |
> |
class ShortRunnable extends CheckedRunnable { |
463 |
> |
void realRun() throws Throwable { |
464 |
> |
Thread.sleep(SHORT_DELAY_MS); |
465 |
|
} |
466 |
|
} |
467 |
|
|
468 |
< |
class ShortInterruptedRunnable implements Runnable { |
469 |
< |
public void run() { |
470 |
< |
try { |
433 |
< |
Thread.sleep(SHORT_DELAY_MS); |
434 |
< |
threadShouldThrow(); |
435 |
< |
} |
436 |
< |
catch (InterruptedException success) { |
437 |
< |
} |
468 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
469 |
> |
void realRun() throws InterruptedException { |
470 |
> |
Thread.sleep(SHORT_DELAY_MS); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
< |
class SmallRunnable implements Runnable { |
475 |
< |
public void run() { |
476 |
< |
try { |
444 |
< |
Thread.sleep(SMALL_DELAY_MS); |
445 |
< |
} |
446 |
< |
catch (Exception e) { |
447 |
< |
threadUnexpectedException(e); |
448 |
< |
} |
474 |
> |
class SmallRunnable extends CheckedRunnable { |
475 |
> |
void realRun() throws Throwable { |
476 |
> |
Thread.sleep(SMALL_DELAY_MS); |
477 |
|
} |
478 |
|
} |
479 |
|
|
480 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
481 |
< |
public void run() { |
480 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
481 |
> |
void realRun() { |
482 |
|
try { |
483 |
|
Thread.sleep(SMALL_DELAY_MS); |
484 |
|
} |
485 |
< |
catch (Exception e) { |
485 |
> |
catch (InterruptedException ok) { |
486 |
|
} |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
< |
class SmallCallable implements Callable { |
491 |
< |
public Object call() { |
492 |
< |
try { |
465 |
< |
Thread.sleep(SMALL_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch (Exception e) { |
468 |
< |
threadUnexpectedException(e); |
469 |
< |
} |
490 |
> |
class SmallCallable extends CheckedCallable { |
491 |
> |
Object realCall() throws Throwable { |
492 |
> |
Thread.sleep(SMALL_DELAY_MS); |
493 |
|
return Boolean.TRUE; |
494 |
|
} |
495 |
|
} |
496 |
|
|
497 |
< |
class SmallInterruptedRunnable implements Runnable { |
498 |
< |
public void run() { |
499 |
< |
try { |
477 |
< |
Thread.sleep(SMALL_DELAY_MS); |
478 |
< |
threadShouldThrow(); |
479 |
< |
} |
480 |
< |
catch (InterruptedException success) { |
481 |
< |
} |
497 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
498 |
> |
void realRun() throws InterruptedException { |
499 |
> |
Thread.sleep(SMALL_DELAY_MS); |
500 |
|
} |
501 |
|
} |
502 |
|
|
503 |
< |
|
504 |
< |
class MediumRunnable implements Runnable { |
505 |
< |
public void run() { |
488 |
< |
try { |
489 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
490 |
< |
} |
491 |
< |
catch (Exception e) { |
492 |
< |
threadUnexpectedException(e); |
493 |
< |
} |
503 |
> |
class MediumRunnable extends CheckedRunnable { |
504 |
> |
void realRun() throws Throwable { |
505 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
506 |
|
} |
507 |
|
} |
508 |
|
|
509 |
< |
class MediumInterruptedRunnable implements Runnable { |
510 |
< |
public void run() { |
511 |
< |
try { |
500 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
501 |
< |
threadShouldThrow(); |
502 |
< |
} |
503 |
< |
catch (InterruptedException success) { |
504 |
< |
} |
509 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
510 |
> |
void realRun() throws InterruptedException { |
511 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
516 |
< |
public void run() { |
515 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
516 |
> |
void realRun() { |
517 |
|
try { |
518 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
519 |
|
} |
520 |
< |
catch (InterruptedException success) { |
520 |
> |
catch (InterruptedException ok) { |
521 |
|
} |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
526 |
< |
public void run() { |
525 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
526 |
> |
void realRun() { |
527 |
|
try { |
528 |
|
Thread.sleep(LONG_DELAY_MS); |
529 |
|
} |
530 |
< |
catch (InterruptedException success) { |
530 |
> |
catch (InterruptedException ok) { |
531 |
|
} |
532 |
|
} |
533 |
|
} |
598 |
|
* For use as RejectedExecutionHandler in constructors |
599 |
|
*/ |
600 |
|
static class NoOpREHandler implements RejectedExecutionHandler { |
601 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
601 |
> |
public void rejectedExecution(Runnable r, |
602 |
> |
ThreadPoolExecutor executor) {} |
603 |
|
} |
604 |
|
|
597 |
– |
|
605 |
|
} |