7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.List; |
14 |
|
import java.util.concurrent.ArrayBlockingQueue; |
15 |
|
import java.util.concurrent.BlockingQueue; |
16 |
|
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
|
import java.util.concurrent.Executors; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
|
import java.util.concurrent.TimeUnit; |
33 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
|
import java.util.concurrent.locks.Condition; |
35 |
|
import java.util.concurrent.locks.ReentrantLock; |
36 |
|
|
39 |
|
|
40 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
public static Test suite() { |
45 |
|
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
61 |
|
CustomTask(final Runnable r, final V res) { |
62 |
|
if (r == null) throw new NullPointerException(); |
63 |
|
callable = new Callable<V>() { |
64 |
< |
public V call() throws Exception { r.run(); return res; }}; |
64 |
> |
public V call() throws Exception { r.run(); return res; }}; |
65 |
|
} |
66 |
|
public boolean isDone() { |
67 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
101 |
|
} |
102 |
|
lock.lock(); |
103 |
|
try { |
104 |
< |
result = v; |
105 |
< |
exception = e; |
106 |
< |
done = true; |
107 |
< |
thread = null; |
108 |
< |
cond.signalAll(); |
104 |
> |
if (!done) { |
105 |
> |
result = v; |
106 |
> |
exception = e; |
107 |
> |
done = true; |
108 |
> |
thread = null; |
109 |
> |
cond.signalAll(); |
110 |
> |
} |
111 |
|
} |
112 |
|
finally { lock.unlock(); } |
113 |
|
} |
116 |
|
try { |
117 |
|
while (!done) |
118 |
|
cond.await(); |
119 |
+ |
if (cancelled) |
120 |
+ |
throw new CancellationException(); |
121 |
|
if (exception != null) |
122 |
|
throw new ExecutionException(exception); |
123 |
|
return result; |
129 |
|
long nanos = unit.toNanos(timeout); |
130 |
|
lock.lock(); |
131 |
|
try { |
132 |
< |
for (;;) { |
133 |
< |
if (done) break; |
127 |
< |
if (nanos < 0) |
132 |
> |
while (!done) { |
133 |
> |
if (nanos <= 0L) |
134 |
|
throw new TimeoutException(); |
135 |
|
nanos = cond.awaitNanos(nanos); |
136 |
|
} |
137 |
+ |
if (cancelled) |
138 |
+ |
throw new CancellationException(); |
139 |
|
if (exception != null) |
140 |
|
throw new ExecutionException(exception); |
141 |
|
return result; |
232 |
|
public void testExecute() throws InterruptedException { |
233 |
|
final ThreadPoolExecutor p = |
234 |
|
new CustomTPE(1, 1, |
235 |
< |
LONG_DELAY_MS, MILLISECONDS, |
235 |
> |
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
|
new ArrayBlockingQueue<Runnable>(10)); |
237 |
< |
final CountDownLatch done = new CountDownLatch(1); |
238 |
< |
final Runnable task = new CheckedRunnable() { |
239 |
< |
public void realRun() { |
240 |
< |
done.countDown(); |
233 |
< |
}}; |
234 |
< |
try { |
237 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
238 |
> |
final CountDownLatch done = new CountDownLatch(1); |
239 |
> |
final Runnable task = new CheckedRunnable() { |
240 |
> |
public void realRun() { done.countDown(); }}; |
241 |
|
p.execute(task); |
242 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
237 |
< |
} finally { |
238 |
< |
joinPool(p); |
242 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
|
} |
244 |
|
} |
245 |
|
|
254 |
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
256 |
|
final CountDownLatch done = new CountDownLatch(1); |
257 |
< |
try { |
257 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
258 |
|
assertEquals(0, p.getActiveCount()); |
259 |
|
p.execute(new CheckedRunnable() { |
260 |
|
public void realRun() throws InterruptedException { |
264 |
|
}}); |
265 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
266 |
|
assertEquals(1, p.getActiveCount()); |
263 |
– |
} finally { |
267 |
|
done.countDown(); |
265 |
– |
joinPool(p); |
268 |
|
} |
269 |
|
} |
270 |
|
|
272 |
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
273 |
|
*/ |
274 |
|
public void testPrestartCoreThread() { |
275 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
< |
assertEquals(0, p.getPoolSize()); |
277 |
< |
assertTrue(p.prestartCoreThread()); |
278 |
< |
assertEquals(1, p.getPoolSize()); |
279 |
< |
assertTrue(p.prestartCoreThread()); |
280 |
< |
assertEquals(2, p.getPoolSize()); |
281 |
< |
assertFalse(p.prestartCoreThread()); |
282 |
< |
assertEquals(2, p.getPoolSize()); |
283 |
< |
joinPool(p); |
275 |
> |
ThreadPoolExecutor p = |
276 |
> |
new CustomTPE(2, 6, |
277 |
> |
LONG_DELAY_MS, MILLISECONDS, |
278 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
279 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
280 |
> |
assertEquals(0, p.getPoolSize()); |
281 |
> |
assertTrue(p.prestartCoreThread()); |
282 |
> |
assertEquals(1, p.getPoolSize()); |
283 |
> |
assertTrue(p.prestartCoreThread()); |
284 |
> |
assertEquals(2, p.getPoolSize()); |
285 |
> |
assertFalse(p.prestartCoreThread()); |
286 |
> |
assertEquals(2, p.getPoolSize()); |
287 |
> |
p.setCorePoolSize(4); |
288 |
> |
assertTrue(p.prestartCoreThread()); |
289 |
> |
assertEquals(3, p.getPoolSize()); |
290 |
> |
assertTrue(p.prestartCoreThread()); |
291 |
> |
assertEquals(4, p.getPoolSize()); |
292 |
> |
assertFalse(p.prestartCoreThread()); |
293 |
> |
assertEquals(4, p.getPoolSize()); |
294 |
> |
} |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* prestartAllCoreThreads starts all corePoolSize threads |
299 |
|
*/ |
300 |
|
public void testPrestartAllCoreThreads() { |
301 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
302 |
< |
assertEquals(0, p.getPoolSize()); |
303 |
< |
p.prestartAllCoreThreads(); |
304 |
< |
assertEquals(2, p.getPoolSize()); |
305 |
< |
p.prestartAllCoreThreads(); |
306 |
< |
assertEquals(2, p.getPoolSize()); |
307 |
< |
joinPool(p); |
301 |
> |
ThreadPoolExecutor p = |
302 |
> |
new CustomTPE(2, 6, |
303 |
> |
LONG_DELAY_MS, MILLISECONDS, |
304 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
305 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
306 |
> |
assertEquals(0, p.getPoolSize()); |
307 |
> |
p.prestartAllCoreThreads(); |
308 |
> |
assertEquals(2, p.getPoolSize()); |
309 |
> |
p.prestartAllCoreThreads(); |
310 |
> |
assertEquals(2, p.getPoolSize()); |
311 |
> |
p.setCorePoolSize(4); |
312 |
> |
p.prestartAllCoreThreads(); |
313 |
> |
assertEquals(4, p.getPoolSize()); |
314 |
> |
p.prestartAllCoreThreads(); |
315 |
> |
assertEquals(4, p.getPoolSize()); |
316 |
> |
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
325 |
|
new CustomTPE(2, 2, |
326 |
|
LONG_DELAY_MS, MILLISECONDS, |
327 |
|
new ArrayBlockingQueue<Runnable>(10)); |
328 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
329 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
330 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
331 |
< |
try { |
328 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
329 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
330 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
331 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
332 |
|
assertEquals(0, p.getCompletedTaskCount()); |
333 |
|
p.execute(new CheckedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
347 |
|
fail("timed out"); |
348 |
|
Thread.yield(); |
349 |
|
} |
328 |
– |
} finally { |
329 |
– |
joinPool(p); |
350 |
|
} |
351 |
|
} |
352 |
|
|
364 |
|
*/ |
365 |
|
public void testGetKeepAliveTime() { |
366 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
367 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
367 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
368 |
|
joinPool(p); |
369 |
|
} |
370 |
|
|
717 |
|
} |
718 |
|
|
719 |
|
/** |
720 |
< |
* shutdownNow returns a list containing tasks that were not run |
720 |
> |
* shutdownNow returns a list containing tasks that were not run, |
721 |
> |
* and those tasks are drained from the queue |
722 |
|
*/ |
723 |
< |
public void testShutdownNow() { |
724 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
725 |
< |
List l; |
726 |
< |
try { |
727 |
< |
for (int i = 0; i < 5; i++) |
728 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
729 |
< |
} |
730 |
< |
finally { |
723 |
> |
public void testShutdownNow() throws InterruptedException { |
724 |
> |
final int poolSize = 2; |
725 |
> |
final int count = 5; |
726 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
727 |
> |
ThreadPoolExecutor p = |
728 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
729 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
730 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
731 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
732 |
> |
threadsStarted.countDown(); |
733 |
|
try { |
734 |
< |
l = p.shutdownNow(); |
735 |
< |
} catch (SecurityException ok) { return; } |
734 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
735 |
> |
} catch (InterruptedException success) {} |
736 |
> |
ran.getAndIncrement(); |
737 |
> |
}}; |
738 |
> |
for (int i = 0; i < count; i++) |
739 |
> |
p.execute(waiter); |
740 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
741 |
> |
assertEquals(poolSize, p.getActiveCount()); |
742 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
743 |
> |
final List<Runnable> queuedTasks; |
744 |
> |
try { |
745 |
> |
queuedTasks = p.shutdownNow(); |
746 |
> |
} catch (SecurityException ok) { |
747 |
> |
return; // Allowed in case test doesn't have privs |
748 |
|
} |
749 |
|
assertTrue(p.isShutdown()); |
750 |
< |
assertTrue(l.size() <= 4); |
750 |
> |
assertTrue(p.getQueue().isEmpty()); |
751 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
752 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
753 |
> |
assertTrue(p.isTerminated()); |
754 |
> |
assertEquals(poolSize, ran.get()); |
755 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
756 |
|
} |
757 |
|
|
758 |
|
// Exception Tests |
762 |
|
*/ |
763 |
|
public void testConstructor1() { |
764 |
|
try { |
765 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
765 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
766 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
767 |
|
shouldThrow(); |
768 |
|
} catch (IllegalArgumentException success) {} |
769 |
|
} |
773 |
|
*/ |
774 |
|
public void testConstructor2() { |
775 |
|
try { |
776 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
776 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
777 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
778 |
|
shouldThrow(); |
779 |
|
} catch (IllegalArgumentException success) {} |
780 |
|
} |
784 |
|
*/ |
785 |
|
public void testConstructor3() { |
786 |
|
try { |
787 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
787 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
788 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
789 |
|
shouldThrow(); |
790 |
|
} catch (IllegalArgumentException success) {} |
791 |
|
} |
795 |
|
*/ |
796 |
|
public void testConstructor4() { |
797 |
|
try { |
798 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
798 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
799 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
800 |
|
shouldThrow(); |
801 |
|
} catch (IllegalArgumentException success) {} |
802 |
|
} |
806 |
|
*/ |
807 |
|
public void testConstructor5() { |
808 |
|
try { |
809 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
809 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
810 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
811 |
|
shouldThrow(); |
812 |
|
} catch (IllegalArgumentException success) {} |
813 |
|
} |
817 |
|
*/ |
818 |
|
public void testConstructorNullPointerException() { |
819 |
|
try { |
820 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
820 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
821 |
|
shouldThrow(); |
822 |
|
} catch (NullPointerException success) {} |
823 |
|
} |
827 |
|
*/ |
828 |
|
public void testConstructor6() { |
829 |
|
try { |
830 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
830 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
831 |
> |
new ArrayBlockingQueue<Runnable>(10), |
832 |
> |
new SimpleThreadFactory()); |
833 |
|
shouldThrow(); |
834 |
|
} catch (IllegalArgumentException success) {} |
835 |
|
} |
839 |
|
*/ |
840 |
|
public void testConstructor7() { |
841 |
|
try { |
842 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
842 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
843 |
> |
new ArrayBlockingQueue<Runnable>(10), |
844 |
> |
new SimpleThreadFactory()); |
845 |
|
shouldThrow(); |
846 |
|
} catch (IllegalArgumentException success) {} |
847 |
|
} |
851 |
|
*/ |
852 |
|
public void testConstructor8() { |
853 |
|
try { |
854 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
854 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
855 |
> |
new ArrayBlockingQueue<Runnable>(10), |
856 |
> |
new SimpleThreadFactory()); |
857 |
|
shouldThrow(); |
858 |
|
} catch (IllegalArgumentException success) {} |
859 |
|
} |
863 |
|
*/ |
864 |
|
public void testConstructor9() { |
865 |
|
try { |
866 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
866 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
867 |
> |
new ArrayBlockingQueue<Runnable>(10), |
868 |
> |
new SimpleThreadFactory()); |
869 |
|
shouldThrow(); |
870 |
|
} catch (IllegalArgumentException success) {} |
871 |
|
} |
875 |
|
*/ |
876 |
|
public void testConstructor10() { |
877 |
|
try { |
878 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
878 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
879 |
> |
new ArrayBlockingQueue<Runnable>(10), |
880 |
> |
new SimpleThreadFactory()); |
881 |
|
shouldThrow(); |
882 |
|
} catch (IllegalArgumentException success) {} |
883 |
|
} |
887 |
|
*/ |
888 |
|
public void testConstructorNullPointerException2() { |
889 |
|
try { |
890 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
890 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
891 |
|
shouldThrow(); |
892 |
|
} catch (NullPointerException success) {} |
893 |
|
} |
897 |
|
*/ |
898 |
|
public void testConstructorNullPointerException3() { |
899 |
|
try { |
900 |
< |
ThreadFactory f = null; |
901 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
900 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
901 |
> |
new ArrayBlockingQueue<Runnable>(10), |
902 |
> |
(ThreadFactory) null); |
903 |
|
shouldThrow(); |
904 |
|
} catch (NullPointerException success) {} |
905 |
|
} |
909 |
|
*/ |
910 |
|
public void testConstructor11() { |
911 |
|
try { |
912 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
912 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
913 |
> |
new ArrayBlockingQueue<Runnable>(10), |
914 |
> |
new NoOpREHandler()); |
915 |
|
shouldThrow(); |
916 |
|
} catch (IllegalArgumentException success) {} |
917 |
|
} |
921 |
|
*/ |
922 |
|
public void testConstructor12() { |
923 |
|
try { |
924 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
924 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
925 |
> |
new ArrayBlockingQueue<Runnable>(10), |
926 |
> |
new NoOpREHandler()); |
927 |
|
shouldThrow(); |
928 |
|
} catch (IllegalArgumentException success) {} |
929 |
|
} |
933 |
|
*/ |
934 |
|
public void testConstructor13() { |
935 |
|
try { |
936 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
936 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
937 |
> |
new ArrayBlockingQueue<Runnable>(10), |
938 |
> |
new NoOpREHandler()); |
939 |
|
shouldThrow(); |
940 |
|
} catch (IllegalArgumentException success) {} |
941 |
|
} |
945 |
|
*/ |
946 |
|
public void testConstructor14() { |
947 |
|
try { |
948 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
948 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
949 |
> |
new ArrayBlockingQueue<Runnable>(10), |
950 |
> |
new NoOpREHandler()); |
951 |
|
shouldThrow(); |
952 |
|
} catch (IllegalArgumentException success) {} |
953 |
|
} |
957 |
|
*/ |
958 |
|
public void testConstructor15() { |
959 |
|
try { |
960 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
960 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
961 |
> |
new ArrayBlockingQueue<Runnable>(10), |
962 |
> |
new NoOpREHandler()); |
963 |
|
shouldThrow(); |
964 |
|
} catch (IllegalArgumentException success) {} |
965 |
|
} |
969 |
|
*/ |
970 |
|
public void testConstructorNullPointerException4() { |
971 |
|
try { |
972 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
972 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
973 |
> |
null, |
974 |
> |
new NoOpREHandler()); |
975 |
|
shouldThrow(); |
976 |
|
} catch (NullPointerException success) {} |
977 |
|
} |
981 |
|
*/ |
982 |
|
public void testConstructorNullPointerException5() { |
983 |
|
try { |
984 |
< |
RejectedExecutionHandler r = null; |
985 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
984 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
985 |
> |
new ArrayBlockingQueue<Runnable>(10), |
986 |
> |
(RejectedExecutionHandler) null); |
987 |
|
shouldThrow(); |
988 |
|
} catch (NullPointerException success) {} |
989 |
|
} |
993 |
|
*/ |
994 |
|
public void testConstructor16() { |
995 |
|
try { |
996 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
996 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
997 |
> |
new ArrayBlockingQueue<Runnable>(10), |
998 |
> |
new SimpleThreadFactory(), |
999 |
> |
new NoOpREHandler()); |
1000 |
|
shouldThrow(); |
1001 |
|
} catch (IllegalArgumentException success) {} |
1002 |
|
} |
1006 |
|
*/ |
1007 |
|
public void testConstructor17() { |
1008 |
|
try { |
1009 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1009 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
1010 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1011 |
> |
new SimpleThreadFactory(), |
1012 |
> |
new NoOpREHandler()); |
1013 |
|
shouldThrow(); |
1014 |
|
} catch (IllegalArgumentException success) {} |
1015 |
|
} |
1019 |
|
*/ |
1020 |
|
public void testConstructor18() { |
1021 |
|
try { |
1022 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1022 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1023 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1024 |
> |
new SimpleThreadFactory(), |
1025 |
> |
new NoOpREHandler()); |
1026 |
|
shouldThrow(); |
1027 |
|
} catch (IllegalArgumentException success) {} |
1028 |
|
} |
1032 |
|
*/ |
1033 |
|
public void testConstructor19() { |
1034 |
|
try { |
1035 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1035 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1036 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1037 |
> |
new SimpleThreadFactory(), |
1038 |
> |
new NoOpREHandler()); |
1039 |
|
shouldThrow(); |
1040 |
|
} catch (IllegalArgumentException success) {} |
1041 |
|
} |
1045 |
|
*/ |
1046 |
|
public void testConstructor20() { |
1047 |
|
try { |
1048 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1048 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1049 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1050 |
> |
new SimpleThreadFactory(), |
1051 |
> |
new NoOpREHandler()); |
1052 |
|
shouldThrow(); |
1053 |
|
} catch (IllegalArgumentException success) {} |
1054 |
|
} |
1058 |
|
*/ |
1059 |
|
public void testConstructorNullPointerException6() { |
1060 |
|
try { |
1061 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1061 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1062 |
> |
null, |
1063 |
> |
new SimpleThreadFactory(), |
1064 |
> |
new NoOpREHandler()); |
1065 |
|
shouldThrow(); |
1066 |
|
} catch (NullPointerException success) {} |
1067 |
|
} |
1071 |
|
*/ |
1072 |
|
public void testConstructorNullPointerException7() { |
1073 |
|
try { |
1074 |
< |
RejectedExecutionHandler r = null; |
1075 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1074 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1075 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1076 |
> |
new SimpleThreadFactory(), |
1077 |
> |
(RejectedExecutionHandler) null); |
1078 |
|
shouldThrow(); |
1079 |
|
} catch (NullPointerException success) {} |
1080 |
|
} |
1084 |
|
*/ |
1085 |
|
public void testConstructorNullPointerException8() { |
1086 |
|
try { |
1087 |
< |
new CustomTPE(1, 2, |
999 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1087 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1088 |
|
new ArrayBlockingQueue<Runnable>(10), |
1089 |
|
(ThreadFactory) null, |
1090 |
|
new NoOpREHandler()); |
1262 |
|
* execute(null) throws NPE |
1263 |
|
*/ |
1264 |
|
public void testExecuteNull() { |
1265 |
< |
ThreadPoolExecutor p = null; |
1265 |
> |
ThreadPoolExecutor p = |
1266 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1267 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1268 |
|
try { |
1179 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1269 |
|
p.execute(null); |
1270 |
|
shouldThrow(); |
1271 |
|
} catch (NullPointerException success) {} |
1761 |
|
l.add(new StringTask()); |
1762 |
|
l.add(new StringTask()); |
1763 |
|
List<Future<String>> futures = |
1764 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1764 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1765 |
|
assertEquals(2, futures.size()); |
1766 |
|
for (Future<String> future : futures) |
1767 |
|
assertSame(TEST_STRING, future.get()); |
1776 |
|
public void testTimedInvokeAll6() throws Exception { |
1777 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1778 |
|
try { |
1779 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1780 |
< |
l.add(new StringTask()); |
1781 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1782 |
< |
l.add(new StringTask()); |
1783 |
< |
List<Future<String>> futures = |
1784 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1785 |
< |
assertEquals(l.size(), futures.size()); |
1786 |
< |
for (Future future : futures) |
1787 |
< |
assertTrue(future.isDone()); |
1788 |
< |
assertFalse(futures.get(0).isCancelled()); |
1789 |
< |
assertTrue(futures.get(1).isCancelled()); |
1779 |
> |
for (long timeout = timeoutMillis();;) { |
1780 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1781 |
> |
tasks.add(new StringTask("0")); |
1782 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1783 |
> |
tasks.add(new StringTask("2")); |
1784 |
> |
long startTime = System.nanoTime(); |
1785 |
> |
List<Future<String>> futures = |
1786 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1787 |
> |
assertEquals(tasks.size(), futures.size()); |
1788 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1789 |
> |
for (Future future : futures) |
1790 |
> |
assertTrue(future.isDone()); |
1791 |
> |
assertTrue(futures.get(1).isCancelled()); |
1792 |
> |
try { |
1793 |
> |
assertEquals("0", futures.get(0).get()); |
1794 |
> |
assertEquals("2", futures.get(2).get()); |
1795 |
> |
break; |
1796 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1797 |
> |
timeout *= 2; |
1798 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1799 |
> |
fail("expected exactly one task to be cancelled"); |
1800 |
> |
} |
1801 |
> |
} |
1802 |
|
} finally { |
1803 |
|
joinPool(e); |
1804 |
|
} |
1841 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1842 |
|
*/ |
1843 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1844 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1844 |
> |
long keepAliveTime = timeoutMillis(); |
1845 |
|
final ThreadPoolExecutor p = |
1846 |
|
new CustomTPE(2, 10, |
1847 |
< |
coreThreadTimeOut, MILLISECONDS, |
1847 |
> |
keepAliveTime, MILLISECONDS, |
1848 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1849 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1850 |
|
try { |
1851 |
|
p.allowCoreThreadTimeOut(true); |
1852 |
|
p.execute(new CheckedRunnable() { |
1853 |
< |
public void realRun() throws InterruptedException { |
1853 |
> |
public void realRun() { |
1854 |
|
threadStarted.countDown(); |
1855 |
|
assertEquals(1, p.getPoolSize()); |
1856 |
|
}}); |
1857 |
|
await(threadStarted); |
1858 |
< |
delay(coreThreadTimeOut); |
1858 |
> |
delay(keepAliveTime); |
1859 |
|
long startTime = System.nanoTime(); |
1860 |
|
while (p.getPoolSize() > 0 |
1861 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1871 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1872 |
|
*/ |
1873 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1874 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1874 |
> |
long keepAliveTime = timeoutMillis(); |
1875 |
|
final ThreadPoolExecutor p = |
1876 |
|
new CustomTPE(2, 10, |
1877 |
< |
coreThreadTimeOut, MILLISECONDS, |
1877 |
> |
keepAliveTime, MILLISECONDS, |
1878 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1879 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1880 |
|
try { |
1884 |
|
threadStarted.countDown(); |
1885 |
|
assertTrue(p.getPoolSize() >= 1); |
1886 |
|
}}); |
1887 |
< |
delay(2 * coreThreadTimeOut); |
1887 |
> |
delay(2 * keepAliveTime); |
1888 |
|
assertTrue(p.getPoolSize() >= 1); |
1889 |
|
} finally { |
1890 |
|
joinPool(p); |
1891 |
|
} |
1892 |
|
} |
1893 |
|
|
1894 |
+ |
/** |
1895 |
+ |
* get(cancelled task) throws CancellationException |
1896 |
+ |
* (in part, a test of CustomTPE itself) |
1897 |
+ |
*/ |
1898 |
+ |
public void testGet_cancelled() throws Exception { |
1899 |
+ |
final ExecutorService e = |
1900 |
+ |
new CustomTPE(1, 1, |
1901 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1902 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1903 |
+ |
try { |
1904 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1905 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1906 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1907 |
+ |
for (int i = 0; i < 2; i++) { |
1908 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1909 |
+ |
throws Throwable { |
1910 |
+ |
blockerStarted.countDown(); |
1911 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1912 |
+ |
}}; |
1913 |
+ |
futures.add(e.submit(r)); |
1914 |
+ |
} |
1915 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1916 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1917 |
+ |
for (Future<?> future : futures) { |
1918 |
+ |
try { |
1919 |
+ |
future.get(); |
1920 |
+ |
shouldThrow(); |
1921 |
+ |
} catch (CancellationException success) {} |
1922 |
+ |
try { |
1923 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1924 |
+ |
shouldThrow(); |
1925 |
+ |
} catch (CancellationException success) {} |
1926 |
+ |
assertTrue(future.isCancelled()); |
1927 |
+ |
assertTrue(future.isDone()); |
1928 |
+ |
} |
1929 |
+ |
done.countDown(); |
1930 |
+ |
} finally { |
1931 |
+ |
joinPool(e); |
1932 |
+ |
} |
1933 |
+ |
} |
1934 |
+ |
|
1935 |
|
} |