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; |
294 |
< |
fail("should throw exception"); |
295 |
< |
} catch (AssertionFailedError e) { |
296 |
< |
e.printStackTrace(); |
297 |
< |
throw e; |
298 |
< |
} |
292 |
> |
try { |
293 |
> |
threadFailed = true; |
294 |
> |
fail("should throw exception"); |
295 |
> |
} catch (AssertionFailedError e) { |
296 |
> |
e.printStackTrace(); |
297 |
> |
throw e; |
298 |
> |
} |
299 |
|
} |
300 |
|
|
301 |
|
/** |
321 |
|
public void joinPool(ExecutorService exec) { |
322 |
|
try { |
323 |
|
exec.shutdown(); |
324 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
324 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
325 |
|
} catch (SecurityException ok) { |
326 |
|
// Allowed in case test doesn't have privs |
327 |
|
} catch (InterruptedException ie) { |
344 |
|
fail("Unexpected exception"); |
345 |
|
} |
346 |
|
|
347 |
+ |
/** |
348 |
+ |
* fail with message "Unexpected exception", with argument |
349 |
+ |
*/ |
350 |
+ |
public void unexpectedException(Throwable ex) { |
351 |
+ |
ex.printStackTrace(); |
352 |
+ |
fail("Unexpected exception: " + ex); |
353 |
+ |
} |
354 |
+ |
|
355 |
|
|
356 |
|
/** |
357 |
|
* The number of elements to place in collections, arrays, etc. |
403 |
|
|
404 |
|
// Some convenient Runnable classes |
405 |
|
|
406 |
+ |
abstract class CheckedRunnable implements Runnable { |
407 |
+ |
abstract void realRun() throws Throwable; |
408 |
+ |
|
409 |
+ |
public final void run() { |
410 |
+ |
try { |
411 |
+ |
realRun(); |
412 |
+ |
} catch (Throwable t) { |
413 |
+ |
threadUnexpectedException(t); |
414 |
+ |
} |
415 |
+ |
} |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
419 |
+ |
abstract void realRun() throws Throwable; |
420 |
+ |
|
421 |
+ |
public final void run() { |
422 |
+ |
try { |
423 |
+ |
realRun(); |
424 |
+ |
threadShouldThrow(); |
425 |
+ |
} catch (InterruptedException success) { |
426 |
+ |
} catch (Throwable t) { |
427 |
+ |
threadUnexpectedException(t); |
428 |
+ |
} |
429 |
+ |
} |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
433 |
+ |
abstract T realCall() throws Throwable; |
434 |
+ |
|
435 |
+ |
public final T call() { |
436 |
+ |
try { |
437 |
+ |
return realCall(); |
438 |
+ |
} catch (Throwable t) { |
439 |
+ |
threadUnexpectedException(t); |
440 |
+ |
return null; |
441 |
+ |
} |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
|
445 |
|
static class NoOpRunnable implements Runnable { |
446 |
|
public void run() {} |
447 |
|
} |
464 |
|
public Integer call() { return one; } |
465 |
|
} |
466 |
|
|
467 |
< |
class ShortRunnable implements Runnable { |
468 |
< |
public void run() { |
469 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
423 |
< |
} |
424 |
< |
catch (Exception e) { |
425 |
< |
threadUnexpectedException(e); |
426 |
< |
} |
467 |
> |
class ShortRunnable extends CheckedRunnable { |
468 |
> |
void realRun() throws Throwable { |
469 |
> |
Thread.sleep(SHORT_DELAY_MS); |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
< |
class ShortInterruptedRunnable implements Runnable { |
474 |
< |
public void run() { |
475 |
< |
try { |
433 |
< |
Thread.sleep(SHORT_DELAY_MS); |
434 |
< |
threadShouldThrow(); |
435 |
< |
} |
436 |
< |
catch (InterruptedException success) { |
437 |
< |
} |
473 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
474 |
> |
void realRun() throws InterruptedException { |
475 |
> |
Thread.sleep(SHORT_DELAY_MS); |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
< |
class SmallRunnable implements Runnable { |
480 |
< |
public void run() { |
481 |
< |
try { |
444 |
< |
Thread.sleep(SMALL_DELAY_MS); |
445 |
< |
} |
446 |
< |
catch (Exception e) { |
447 |
< |
threadUnexpectedException(e); |
448 |
< |
} |
479 |
> |
class SmallRunnable extends CheckedRunnable { |
480 |
> |
void realRun() throws Throwable { |
481 |
> |
Thread.sleep(SMALL_DELAY_MS); |
482 |
|
} |
483 |
|
} |
484 |
|
|
485 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
486 |
< |
public void run() { |
485 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
486 |
> |
void realRun() { |
487 |
|
try { |
488 |
|
Thread.sleep(SMALL_DELAY_MS); |
489 |
|
} |
490 |
< |
catch (Exception e) { |
490 |
> |
catch (InterruptedException ok) { |
491 |
|
} |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
< |
class SmallCallable implements Callable { |
496 |
< |
public Object call() { |
497 |
< |
try { |
465 |
< |
Thread.sleep(SMALL_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch (Exception e) { |
468 |
< |
threadUnexpectedException(e); |
469 |
< |
} |
495 |
> |
class SmallCallable extends CheckedCallable { |
496 |
> |
Object realCall() throws Throwable { |
497 |
> |
Thread.sleep(SMALL_DELAY_MS); |
498 |
|
return Boolean.TRUE; |
499 |
|
} |
500 |
|
} |
501 |
|
|
502 |
< |
class SmallInterruptedRunnable implements Runnable { |
503 |
< |
public void run() { |
504 |
< |
try { |
477 |
< |
Thread.sleep(SMALL_DELAY_MS); |
478 |
< |
threadShouldThrow(); |
479 |
< |
} |
480 |
< |
catch (InterruptedException success) { |
481 |
< |
} |
502 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
503 |
> |
void realRun() throws InterruptedException { |
504 |
> |
Thread.sleep(SMALL_DELAY_MS); |
505 |
|
} |
506 |
|
} |
507 |
|
|
508 |
< |
|
509 |
< |
class MediumRunnable implements Runnable { |
510 |
< |
public void run() { |
488 |
< |
try { |
489 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
490 |
< |
} |
491 |
< |
catch (Exception e) { |
492 |
< |
threadUnexpectedException(e); |
493 |
< |
} |
508 |
> |
class MediumRunnable extends CheckedRunnable { |
509 |
> |
void realRun() throws Throwable { |
510 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
511 |
|
} |
512 |
|
} |
513 |
|
|
514 |
< |
class MediumInterruptedRunnable implements Runnable { |
515 |
< |
public void run() { |
516 |
< |
try { |
500 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
501 |
< |
threadShouldThrow(); |
502 |
< |
} |
503 |
< |
catch (InterruptedException success) { |
504 |
< |
} |
514 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
515 |
> |
void realRun() throws InterruptedException { |
516 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
521 |
< |
public void run() { |
520 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
521 |
> |
void realRun() { |
522 |
|
try { |
523 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
524 |
|
} |
525 |
< |
catch (InterruptedException success) { |
525 |
> |
catch (InterruptedException ok) { |
526 |
|
} |
527 |
|
} |
528 |
|
} |
529 |
|
|
530 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
531 |
< |
public void run() { |
530 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
531 |
> |
void realRun() { |
532 |
|
try { |
533 |
|
Thread.sleep(LONG_DELAY_MS); |
534 |
|
} |
535 |
< |
catch (InterruptedException success) { |
535 |
> |
catch (InterruptedException ok) { |
536 |
|
} |
537 |
|
} |
538 |
|
} |
603 |
|
* For use as RejectedExecutionHandler in constructors |
604 |
|
*/ |
605 |
|
static class NoOpREHandler implements RejectedExecutionHandler { |
606 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
606 |
> |
public void rejectedExecution(Runnable r, |
607 |
> |
ThreadPoolExecutor executor) {} |
608 |
|
} |
609 |
|
|
597 |
– |
|
610 |
|
} |