427 |
|
try { |
428 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
429 |
|
l.add(new NPETask()); |
430 |
< |
List<Future<String>> result = e.invokeAll(l); |
431 |
< |
assertEquals(1, result.size()); |
432 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
433 |
< |
it.next().get(); |
430 |
> |
e.invokeAny(l); |
431 |
|
} catch(ExecutionException success) { |
432 |
|
} catch(Exception ex) { |
433 |
|
unexpectedException(); |
540 |
|
} catch(Exception ex) { |
541 |
|
unexpectedException(); |
542 |
|
} finally { |
543 |
+ |
joinPool(e); |
544 |
+ |
} |
545 |
+ |
} |
546 |
+ |
|
547 |
+ |
|
548 |
+ |
/** |
549 |
+ |
* timed invokeAny(null) throws NPE |
550 |
+ |
*/ |
551 |
+ |
public void testTimedInvokeAny1() { |
552 |
+ |
ExecutorService e = new DirectExecutorService(); |
553 |
+ |
try { |
554 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
555 |
+ |
} catch (NullPointerException success) { |
556 |
+ |
} catch(Exception ex) { |
557 |
+ |
unexpectedException(); |
558 |
+ |
} finally { |
559 |
+ |
joinPool(e); |
560 |
+ |
} |
561 |
+ |
} |
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* timed invokeAny(,,null) throws NPE |
565 |
+ |
*/ |
566 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
567 |
+ |
ExecutorService e = new DirectExecutorService(); |
568 |
+ |
try { |
569 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
570 |
+ |
l.add(new StringTask()); |
571 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
572 |
+ |
} catch (NullPointerException success) { |
573 |
+ |
} catch(Exception ex) { |
574 |
+ |
unexpectedException(); |
575 |
+ |
} finally { |
576 |
+ |
joinPool(e); |
577 |
+ |
} |
578 |
+ |
} |
579 |
+ |
|
580 |
+ |
/** |
581 |
+ |
* timed invokeAny(empty collection) throws IAE |
582 |
+ |
*/ |
583 |
+ |
public void testTimedInvokeAny2() { |
584 |
+ |
ExecutorService e = new DirectExecutorService(); |
585 |
+ |
try { |
586 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
587 |
+ |
} catch (IllegalArgumentException success) { |
588 |
+ |
} catch(Exception ex) { |
589 |
+ |
unexpectedException(); |
590 |
+ |
} finally { |
591 |
+ |
joinPool(e); |
592 |
+ |
} |
593 |
+ |
} |
594 |
+ |
|
595 |
+ |
/** |
596 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
597 |
+ |
*/ |
598 |
+ |
public void testTimedInvokeAny3() { |
599 |
+ |
ExecutorService e = new DirectExecutorService(); |
600 |
+ |
try { |
601 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
602 |
+ |
l.add(new StringTask()); |
603 |
+ |
l.add(null); |
604 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
605 |
+ |
} catch (NullPointerException success) { |
606 |
+ |
} catch(Exception ex) { |
607 |
+ |
ex.printStackTrace(); |
608 |
+ |
unexpectedException(); |
609 |
+ |
} finally { |
610 |
+ |
joinPool(e); |
611 |
+ |
} |
612 |
+ |
} |
613 |
+ |
|
614 |
+ |
/** |
615 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
616 |
+ |
*/ |
617 |
+ |
public void testTimedInvokeAny4() { |
618 |
+ |
ExecutorService e = new DirectExecutorService(); |
619 |
+ |
try { |
620 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
621 |
+ |
l.add(new NPETask()); |
622 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
623 |
+ |
} catch(ExecutionException success) { |
624 |
+ |
} catch(Exception ex) { |
625 |
+ |
unexpectedException(); |
626 |
+ |
} finally { |
627 |
+ |
joinPool(e); |
628 |
+ |
} |
629 |
+ |
} |
630 |
+ |
|
631 |
+ |
/** |
632 |
+ |
* timed invokeAny(c) returns result of some task |
633 |
+ |
*/ |
634 |
+ |
public void testTimedInvokeAny5() { |
635 |
+ |
ExecutorService e = new DirectExecutorService(); |
636 |
+ |
try { |
637 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
638 |
+ |
l.add(new StringTask()); |
639 |
+ |
l.add(new StringTask()); |
640 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
641 |
+ |
assertSame(TEST_STRING, result); |
642 |
+ |
} catch (ExecutionException success) { |
643 |
+ |
} catch(Exception ex) { |
644 |
+ |
unexpectedException(); |
645 |
+ |
} finally { |
646 |
+ |
joinPool(e); |
647 |
+ |
} |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
+ |
* timed invokeAll(null) throws NPE |
652 |
+ |
*/ |
653 |
+ |
public void testTimedInvokeAll1() { |
654 |
+ |
ExecutorService e = new DirectExecutorService(); |
655 |
+ |
try { |
656 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
657 |
+ |
} catch (NullPointerException success) { |
658 |
+ |
} catch(Exception ex) { |
659 |
+ |
unexpectedException(); |
660 |
+ |
} finally { |
661 |
+ |
joinPool(e); |
662 |
+ |
} |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
/** |
666 |
+ |
* timed invokeAll(,,null) throws NPE |
667 |
+ |
*/ |
668 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
669 |
+ |
ExecutorService e = new DirectExecutorService(); |
670 |
+ |
try { |
671 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
672 |
+ |
l.add(new StringTask()); |
673 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
674 |
+ |
} catch (NullPointerException success) { |
675 |
+ |
} catch(Exception ex) { |
676 |
+ |
unexpectedException(); |
677 |
+ |
} finally { |
678 |
+ |
joinPool(e); |
679 |
+ |
} |
680 |
+ |
} |
681 |
+ |
|
682 |
+ |
/** |
683 |
+ |
* timed invokeAll(empty collection) returns empty collection |
684 |
+ |
*/ |
685 |
+ |
public void testTimedInvokeAll2() { |
686 |
+ |
ExecutorService e = new DirectExecutorService(); |
687 |
+ |
try { |
688 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
689 |
+ |
assertTrue(r.isEmpty()); |
690 |
+ |
} catch(Exception ex) { |
691 |
+ |
unexpectedException(); |
692 |
+ |
} finally { |
693 |
+ |
joinPool(e); |
694 |
+ |
} |
695 |
+ |
} |
696 |
+ |
|
697 |
+ |
/** |
698 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
699 |
+ |
*/ |
700 |
+ |
public void testTimedInvokeAll3() { |
701 |
+ |
ExecutorService e = new DirectExecutorService(); |
702 |
+ |
try { |
703 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
704 |
+ |
l.add(new StringTask()); |
705 |
+ |
l.add(null); |
706 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
707 |
+ |
} catch (NullPointerException success) { |
708 |
+ |
} catch(Exception ex) { |
709 |
+ |
unexpectedException(); |
710 |
+ |
} finally { |
711 |
+ |
joinPool(e); |
712 |
+ |
} |
713 |
+ |
} |
714 |
+ |
|
715 |
+ |
/** |
716 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
717 |
+ |
*/ |
718 |
+ |
public void testTimedInvokeAll4() { |
719 |
+ |
ExecutorService e = new DirectExecutorService(); |
720 |
+ |
try { |
721 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
722 |
+ |
l.add(new NPETask()); |
723 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
724 |
+ |
assertEquals(1, result.size()); |
725 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
726 |
+ |
it.next().get(); |
727 |
+ |
} catch(ExecutionException success) { |
728 |
+ |
} catch(Exception ex) { |
729 |
+ |
unexpectedException(); |
730 |
+ |
} finally { |
731 |
+ |
joinPool(e); |
732 |
+ |
} |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
/** |
736 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
737 |
+ |
*/ |
738 |
+ |
public void testTimedInvokeAll5() { |
739 |
+ |
ExecutorService e = new DirectExecutorService(); |
740 |
+ |
try { |
741 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
742 |
+ |
l.add(new StringTask()); |
743 |
+ |
l.add(new StringTask()); |
744 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
745 |
+ |
assertEquals(2, result.size()); |
746 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
747 |
+ |
assertSame(TEST_STRING, it.next().get()); |
748 |
+ |
} catch (ExecutionException success) { |
749 |
+ |
} catch(Exception ex) { |
750 |
+ |
unexpectedException(); |
751 |
+ |
} finally { |
752 |
+ |
joinPool(e); |
753 |
+ |
} |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
/** |
757 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
758 |
+ |
*/ |
759 |
+ |
public void testTimedInvokeAll6() { |
760 |
+ |
ExecutorService e = Executors.newCachedThreadPool(); |
761 |
+ |
try { |
762 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
763 |
+ |
l.add(new StringTask()); |
764 |
+ |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
765 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
766 |
+ |
assertEquals(2, result.size()); |
767 |
+ |
Iterator<Future<String>> it = result.iterator(); |
768 |
+ |
Future<String> f1 = it.next(); |
769 |
+ |
Future<String> f2 = it.next(); |
770 |
+ |
assertTrue(f1.isDone()); |
771 |
+ |
assertFalse(f1.isCancelled()); |
772 |
+ |
assertTrue(f2.isDone()); |
773 |
+ |
assertTrue(f2.isCancelled()); |
774 |
+ |
} catch(Exception ex) { |
775 |
+ |
unexpectedException(); |
776 |
+ |
} finally { |
777 |
|
joinPool(e); |
778 |
|
} |
779 |
|
} |