12 |
|
|
13 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
– |
|
77 |
|
/** |
78 |
|
* execute successfully executes a runnable |
79 |
|
*/ |
80 |
|
public void testExecute() throws InterruptedException { |
81 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
81 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
82 |
|
CustomExecutor p1 = new CustomExecutor(1); |
83 |
|
p1.execute(runnable); |
84 |
|
assertFalse(runnable.done); |
190 |
|
|
191 |
|
|
192 |
|
/** |
193 |
< |
* execute (null) throws NPE |
193 |
> |
* execute(null) throws NPE |
194 |
|
*/ |
195 |
|
public void testExecuteNull() throws InterruptedException { |
196 |
|
CustomExecutor se = new CustomExecutor(1); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* schedule (null) throws NPE |
205 |
> |
* schedule(null) throws NPE |
206 |
|
*/ |
207 |
|
public void testScheduleNull() throws InterruptedException { |
208 |
|
CustomExecutor se = new CustomExecutor(1); |
707 |
|
* invokeAny(c) throws NPE if c has null elements |
708 |
|
*/ |
709 |
|
public void testInvokeAny3() throws Exception { |
710 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
710 |
> |
CountDownLatch latch = new CountDownLatch(1); |
711 |
|
ExecutorService e = new CustomExecutor(2); |
712 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
713 |
+ |
l.add(latchAwaitingStringTask(latch)); |
714 |
+ |
l.add(null); |
715 |
|
try { |
714 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
715 |
– |
l.add(new Callable<String>() { |
716 |
– |
public String call() { |
717 |
– |
try { |
718 |
– |
latch.await(); |
719 |
– |
} catch (InterruptedException quittingTime) {} |
720 |
– |
return TEST_STRING; |
721 |
– |
}}); |
722 |
– |
l.add(null); |
716 |
|
e.invokeAny(l); |
717 |
|
shouldThrow(); |
718 |
|
} catch (NullPointerException success) { |
727 |
|
*/ |
728 |
|
public void testInvokeAny4() throws Exception { |
729 |
|
ExecutorService e = new CustomExecutor(2); |
730 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
731 |
+ |
l.add(new NPETask()); |
732 |
|
try { |
738 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
739 |
– |
l.add(new NPETask()); |
733 |
|
e.invokeAny(l); |
734 |
|
shouldThrow(); |
735 |
|
} catch (ExecutionException success) { |
745 |
|
public void testInvokeAny5() throws Exception { |
746 |
|
ExecutorService e = new CustomExecutor(2); |
747 |
|
try { |
748 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
748 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
749 |
|
l.add(new StringTask()); |
750 |
|
l.add(new StringTask()); |
751 |
|
String result = e.invokeAny(l); |
787 |
|
*/ |
788 |
|
public void testInvokeAll3() throws Exception { |
789 |
|
ExecutorService e = new CustomExecutor(2); |
790 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
791 |
+ |
l.add(new StringTask()); |
792 |
+ |
l.add(null); |
793 |
|
try { |
798 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
799 |
– |
l.add(new StringTask()); |
800 |
– |
l.add(null); |
794 |
|
e.invokeAll(l); |
795 |
|
shouldThrow(); |
796 |
|
} catch (NullPointerException success) { |
804 |
|
*/ |
805 |
|
public void testInvokeAll4() throws Exception { |
806 |
|
ExecutorService e = new CustomExecutor(2); |
807 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
808 |
+ |
l.add(new NPETask()); |
809 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
810 |
+ |
assertEquals(1, futures.size()); |
811 |
|
try { |
812 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
816 |
< |
l.add(new NPETask()); |
817 |
< |
List<Future<String>> result = e.invokeAll(l); |
818 |
< |
assertEquals(1, result.size()); |
819 |
< |
for (Future<String> future : result) |
820 |
< |
future.get(); |
812 |
> |
futures.get(0).get(); |
813 |
|
shouldThrow(); |
814 |
|
} catch (ExecutionException success) { |
815 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
824 |
|
public void testInvokeAll5() throws Exception { |
825 |
|
ExecutorService e = new CustomExecutor(2); |
826 |
|
try { |
827 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
827 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
828 |
|
l.add(new StringTask()); |
829 |
|
l.add(new StringTask()); |
830 |
< |
List<Future<String>> result = e.invokeAll(l); |
831 |
< |
assertEquals(2, result.size()); |
832 |
< |
for (Future<String> future : result) |
830 |
> |
List<Future<String>> futures = e.invokeAll(l); |
831 |
> |
assertEquals(2, futures.size()); |
832 |
> |
for (Future<String> future : futures) |
833 |
|
assertSame(TEST_STRING, future.get()); |
834 |
|
} finally { |
835 |
|
joinPool(e); |
855 |
|
*/ |
856 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
857 |
|
ExecutorService e = new CustomExecutor(2); |
858 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
859 |
+ |
l.add(new StringTask()); |
860 |
|
try { |
867 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
– |
l.add(new StringTask()); |
861 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
862 |
|
shouldThrow(); |
863 |
|
} catch (NullPointerException success) { |
884 |
|
* timed invokeAny(c) throws NPE if c has null elements |
885 |
|
*/ |
886 |
|
public void testTimedInvokeAny3() throws Exception { |
887 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
887 |
> |
CountDownLatch latch = new CountDownLatch(1); |
888 |
|
ExecutorService e = new CustomExecutor(2); |
889 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
+ |
l.add(latchAwaitingStringTask(latch)); |
891 |
+ |
l.add(null); |
892 |
|
try { |
898 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
899 |
– |
l.add(new Callable<String>() { |
900 |
– |
public String call() { |
901 |
– |
try { |
902 |
– |
latch.await(); |
903 |
– |
} catch (InterruptedException quittingTime) {} |
904 |
– |
return TEST_STRING; |
905 |
– |
}}); |
906 |
– |
l.add(null); |
893 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
894 |
|
shouldThrow(); |
895 |
|
} catch (NullPointerException success) { |
904 |
|
*/ |
905 |
|
public void testTimedInvokeAny4() throws Exception { |
906 |
|
ExecutorService e = new CustomExecutor(2); |
907 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
908 |
+ |
l.add(new NPETask()); |
909 |
|
try { |
922 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
923 |
– |
l.add(new NPETask()); |
910 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
911 |
|
shouldThrow(); |
912 |
|
} catch (ExecutionException success) { |
922 |
|
public void testTimedInvokeAny5() throws Exception { |
923 |
|
ExecutorService e = new CustomExecutor(2); |
924 |
|
try { |
925 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
925 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
926 |
|
l.add(new StringTask()); |
927 |
|
l.add(new StringTask()); |
928 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
951 |
|
*/ |
952 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
953 |
|
ExecutorService e = new CustomExecutor(2); |
954 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
955 |
+ |
l.add(new StringTask()); |
956 |
|
try { |
969 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
970 |
– |
l.add(new StringTask()); |
957 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
958 |
|
shouldThrow(); |
959 |
|
} catch (NullPointerException success) { |
980 |
|
*/ |
981 |
|
public void testTimedInvokeAll3() throws Exception { |
982 |
|
ExecutorService e = new CustomExecutor(2); |
983 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
984 |
+ |
l.add(new StringTask()); |
985 |
+ |
l.add(null); |
986 |
|
try { |
998 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
– |
l.add(new StringTask()); |
1000 |
– |
l.add(null); |
987 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
988 |
|
shouldThrow(); |
989 |
|
} catch (NullPointerException success) { |
997 |
|
*/ |
998 |
|
public void testTimedInvokeAll4() throws Exception { |
999 |
|
ExecutorService e = new CustomExecutor(2); |
1000 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1001 |
+ |
l.add(new NPETask()); |
1002 |
+ |
List<Future<String>> futures = |
1003 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1004 |
+ |
assertEquals(1, futures.size()); |
1005 |
|
try { |
1006 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1016 |
< |
l.add(new NPETask()); |
1017 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1018 |
< |
assertEquals(1, result.size()); |
1019 |
< |
for (Future<String> future : result) |
1020 |
< |
future.get(); |
1006 |
> |
futures.get(0).get(); |
1007 |
|
shouldThrow(); |
1008 |
|
} catch (ExecutionException success) { |
1009 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1018 |
|
public void testTimedInvokeAll5() throws Exception { |
1019 |
|
ExecutorService e = new CustomExecutor(2); |
1020 |
|
try { |
1021 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1021 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1022 |
|
l.add(new StringTask()); |
1023 |
|
l.add(new StringTask()); |
1024 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1025 |
< |
assertEquals(2, result.size()); |
1026 |
< |
for (Future<String> future : result) |
1024 |
> |
List<Future<String>> futures = |
1025 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1026 |
> |
assertEquals(2, futures.size()); |
1027 |
> |
for (Future<String> future : futures) |
1028 |
|
assertSame(TEST_STRING, future.get()); |
1029 |
|
} finally { |
1030 |
|
joinPool(e); |
1037 |
|
public void testTimedInvokeAll6() throws Exception { |
1038 |
|
ExecutorService e = new CustomExecutor(2); |
1039 |
|
try { |
1040 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1041 |
|
l.add(new StringTask()); |
1042 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1043 |
|
l.add(new StringTask()); |
1044 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1045 |
< |
assertEquals(3, result.size()); |
1046 |
< |
Iterator<Future<String>> it = result.iterator(); |
1044 |
> |
List<Future<String>> futures = |
1045 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1046 |
> |
assertEquals(3, futures.size()); |
1047 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1048 |
|
Future<String> f1 = it.next(); |
1049 |
|
Future<String> f2 = it.next(); |
1050 |
|
Future<String> f3 = it.next(); |