6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.locks.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
< |
import junit.framework.*; |
13 |
< |
import java.util.*; |
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; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.FutureTask; |
24 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
25 |
> |
import java.util.concurrent.RejectedExecutionException; |
26 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
27 |
> |
import java.util.concurrent.RunnableFuture; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
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 |
> |
|
37 |
> |
import junit.framework.Test; |
38 |
> |
import junit.framework.TestSuite; |
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; |
106 |
< |
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; |
144 |
|
} |
145 |
|
} |
146 |
|
|
118 |
– |
|
147 |
|
static class CustomTPE extends ThreadPoolExecutor { |
148 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
149 |
|
return new CustomTask<V>(c); |
190 |
|
workQueue, threadFactory, handler); |
191 |
|
} |
192 |
|
|
193 |
< |
volatile boolean beforeCalled = false; |
194 |
< |
volatile boolean afterCalled = false; |
195 |
< |
volatile boolean terminatedCalled = false; |
193 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
194 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
195 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
196 |
> |
|
197 |
|
public CustomTPE() { |
198 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
199 |
|
} |
200 |
|
protected void beforeExecute(Thread t, Runnable r) { |
201 |
< |
beforeCalled = true; |
201 |
> |
beforeCalled.countDown(); |
202 |
|
} |
203 |
|
protected void afterExecute(Runnable r, Throwable t) { |
204 |
< |
afterCalled = true; |
204 |
> |
afterCalled.countDown(); |
205 |
|
} |
206 |
|
protected void terminated() { |
207 |
< |
terminatedCalled = true; |
207 |
> |
terminatedCalled.countDown(); |
208 |
|
} |
209 |
|
|
210 |
+ |
public boolean beforeCalled() { |
211 |
+ |
return beforeCalled.getCount() == 0; |
212 |
+ |
} |
213 |
+ |
public boolean afterCalled() { |
214 |
+ |
return afterCalled.getCount() == 0; |
215 |
+ |
} |
216 |
+ |
public boolean terminatedCalled() { |
217 |
+ |
return terminatedCalled.getCount() == 0; |
218 |
+ |
} |
219 |
|
} |
220 |
|
|
221 |
|
static class FailingThreadFactory implements ThreadFactory { |
226 |
|
} |
227 |
|
} |
228 |
|
|
191 |
– |
|
229 |
|
/** |
230 |
|
* execute successfully executes a runnable |
231 |
|
*/ |
232 |
|
public void testExecute() throws InterruptedException { |
233 |
< |
final ThreadPoolExecutor p = |
234 |
< |
new CustomTPE(1, 1, |
235 |
< |
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(); |
241 |
< |
}}; |
205 |
< |
try { |
233 |
> |
try (PoolCleaner<CustomTPE> cleaner = |
234 |
> |
cleaner(new CustomTPE(1, 1, |
235 |
> |
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
> |
new ArrayBlockingQueue<Runnable>(10)))) { |
237 |
> |
final ThreadPoolExecutor p = cleaner.pool; |
238 |
> |
final CountDownLatch done = new CountDownLatch(1); |
239 |
> |
final Runnable task = new CheckedRunnable() { |
240 |
> |
public void realRun() { done.countDown(); |
241 |
> |
}}; |
242 |
|
p.execute(task); |
243 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
208 |
< |
} finally { |
209 |
< |
joinPool(p); |
243 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
244 |
|
} |
245 |
|
} |
246 |
|
|
320 |
|
threadProceed.await(); |
321 |
|
threadDone.countDown(); |
322 |
|
}}); |
323 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
323 |
> |
await(threadStarted); |
324 |
|
assertEquals(0, p.getCompletedTaskCount()); |
325 |
|
threadProceed.countDown(); |
326 |
|
threadDone.await(); |
327 |
< |
delay(SHORT_DELAY_MS); |
328 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
327 |
> |
long startTime = System.nanoTime(); |
328 |
> |
while (p.getCompletedTaskCount() != 1) { |
329 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
330 |
> |
fail("timed out"); |
331 |
> |
Thread.yield(); |
332 |
> |
} |
333 |
|
} finally { |
334 |
|
joinPool(p); |
335 |
|
} |
349 |
|
*/ |
350 |
|
public void testGetKeepAliveTime() { |
351 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
352 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
352 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
353 |
|
joinPool(p); |
354 |
|
} |
355 |
|
|
318 |
– |
|
356 |
|
/** |
357 |
|
* getThreadFactory returns factory in constructor if not set |
358 |
|
*/ |
374 |
|
joinPool(p); |
375 |
|
} |
376 |
|
|
340 |
– |
|
377 |
|
/** |
378 |
|
* setThreadFactory(null) throws NPE |
379 |
|
*/ |
410 |
|
joinPool(p); |
411 |
|
} |
412 |
|
|
377 |
– |
|
413 |
|
/** |
414 |
|
* setRejectedExecutionHandler(null) throws NPE |
415 |
|
*/ |
424 |
|
} |
425 |
|
} |
426 |
|
|
392 |
– |
|
427 |
|
/** |
428 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
429 |
|
* multiple threads active |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* isShutDown is false before shutdown, true after |
521 |
> |
* isShutdown is false before shutdown, true after |
522 |
|
*/ |
523 |
|
public void testIsShutdown() { |
524 |
|
|
529 |
|
joinPool(p); |
530 |
|
} |
531 |
|
|
498 |
– |
|
532 |
|
/** |
533 |
|
* isTerminated is false before termination, true after |
534 |
|
*/ |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
< |
* shutDownNow returns a list containing tasks that were not run |
705 |
> |
* shutdownNow returns a list containing tasks that were not run, |
706 |
> |
* and those tasks are drained from the queue |
707 |
|
*/ |
708 |
< |
public void testShutdownNow() { |
709 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
710 |
< |
List l; |
711 |
< |
try { |
712 |
< |
for (int i = 0; i < 5; i++) |
713 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
714 |
< |
} |
715 |
< |
finally { |
708 |
> |
public void testShutdownNow() throws InterruptedException { |
709 |
> |
final int poolSize = 2; |
710 |
> |
final int count = 5; |
711 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
712 |
> |
ThreadPoolExecutor p = |
713 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
714 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
715 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
716 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
717 |
> |
threadsStarted.countDown(); |
718 |
|
try { |
719 |
< |
l = p.shutdownNow(); |
720 |
< |
} catch (SecurityException ok) { return; } |
719 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
720 |
> |
} catch (InterruptedException success) {} |
721 |
> |
ran.getAndIncrement(); |
722 |
> |
}}; |
723 |
> |
for (int i = 0; i < count; i++) |
724 |
> |
p.execute(waiter); |
725 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
726 |
> |
assertEquals(poolSize, p.getActiveCount()); |
727 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
728 |
> |
final List<Runnable> queuedTasks; |
729 |
> |
try { |
730 |
> |
queuedTasks = p.shutdownNow(); |
731 |
> |
} catch (SecurityException ok) { |
732 |
> |
return; // Allowed in case test doesn't have privs |
733 |
|
} |
734 |
|
assertTrue(p.isShutdown()); |
735 |
< |
assertTrue(l.size() <= 4); |
735 |
> |
assertTrue(p.getQueue().isEmpty()); |
736 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
737 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
738 |
> |
assertTrue(p.isTerminated()); |
739 |
> |
assertEquals(poolSize, ran.get()); |
740 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
741 |
|
} |
742 |
|
|
743 |
|
// Exception Tests |
744 |
|
|
692 |
– |
|
745 |
|
/** |
746 |
|
* Constructor throws if corePoolSize argument is less than zero |
747 |
|
*/ |
748 |
|
public void testConstructor1() { |
749 |
|
try { |
750 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
750 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
751 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
752 |
|
shouldThrow(); |
753 |
|
} catch (IllegalArgumentException success) {} |
754 |
|
} |
758 |
|
*/ |
759 |
|
public void testConstructor2() { |
760 |
|
try { |
761 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
761 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
762 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
763 |
|
shouldThrow(); |
764 |
|
} catch (IllegalArgumentException success) {} |
765 |
|
} |
769 |
|
*/ |
770 |
|
public void testConstructor3() { |
771 |
|
try { |
772 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
772 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
773 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
774 |
|
shouldThrow(); |
775 |
|
} catch (IllegalArgumentException success) {} |
776 |
|
} |
780 |
|
*/ |
781 |
|
public void testConstructor4() { |
782 |
|
try { |
783 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
783 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
784 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
785 |
|
shouldThrow(); |
786 |
|
} catch (IllegalArgumentException success) {} |
787 |
|
} |
791 |
|
*/ |
792 |
|
public void testConstructor5() { |
793 |
|
try { |
794 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
794 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
795 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
796 |
|
shouldThrow(); |
797 |
|
} catch (IllegalArgumentException success) {} |
798 |
|
} |
802 |
|
*/ |
803 |
|
public void testConstructorNullPointerException() { |
804 |
|
try { |
805 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
805 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
806 |
|
shouldThrow(); |
807 |
|
} catch (NullPointerException success) {} |
808 |
|
} |
809 |
|
|
753 |
– |
|
754 |
– |
|
810 |
|
/** |
811 |
|
* Constructor throws if corePoolSize argument is less than zero |
812 |
|
*/ |
813 |
|
public void testConstructor6() { |
814 |
|
try { |
815 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
815 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
816 |
> |
new ArrayBlockingQueue<Runnable>(10), |
817 |
> |
new SimpleThreadFactory()); |
818 |
|
shouldThrow(); |
819 |
|
} catch (IllegalArgumentException success) {} |
820 |
|
} |
824 |
|
*/ |
825 |
|
public void testConstructor7() { |
826 |
|
try { |
827 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
827 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
828 |
> |
new ArrayBlockingQueue<Runnable>(10), |
829 |
> |
new SimpleThreadFactory()); |
830 |
|
shouldThrow(); |
831 |
|
} catch (IllegalArgumentException success) {} |
832 |
|
} |
836 |
|
*/ |
837 |
|
public void testConstructor8() { |
838 |
|
try { |
839 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
839 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
840 |
> |
new ArrayBlockingQueue<Runnable>(10), |
841 |
> |
new SimpleThreadFactory()); |
842 |
|
shouldThrow(); |
843 |
|
} catch (IllegalArgumentException success) {} |
844 |
|
} |
848 |
|
*/ |
849 |
|
public void testConstructor9() { |
850 |
|
try { |
851 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
851 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
852 |
> |
new ArrayBlockingQueue<Runnable>(10), |
853 |
> |
new SimpleThreadFactory()); |
854 |
|
shouldThrow(); |
855 |
|
} catch (IllegalArgumentException success) {} |
856 |
|
} |
860 |
|
*/ |
861 |
|
public void testConstructor10() { |
862 |
|
try { |
863 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
863 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
864 |
> |
new ArrayBlockingQueue<Runnable>(10), |
865 |
> |
new SimpleThreadFactory()); |
866 |
|
shouldThrow(); |
867 |
|
} catch (IllegalArgumentException success) {} |
868 |
|
} |
872 |
|
*/ |
873 |
|
public void testConstructorNullPointerException2() { |
874 |
|
try { |
875 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
875 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
876 |
|
shouldThrow(); |
877 |
|
} catch (NullPointerException success) {} |
878 |
|
} |
882 |
|
*/ |
883 |
|
public void testConstructorNullPointerException3() { |
884 |
|
try { |
885 |
< |
ThreadFactory f = null; |
886 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
885 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
886 |
> |
new ArrayBlockingQueue<Runnable>(10), |
887 |
> |
(ThreadFactory) null); |
888 |
|
shouldThrow(); |
889 |
|
} catch (NullPointerException success) {} |
890 |
|
} |
891 |
|
|
826 |
– |
|
892 |
|
/** |
893 |
|
* Constructor throws if corePoolSize argument is less than zero |
894 |
|
*/ |
895 |
|
public void testConstructor11() { |
896 |
|
try { |
897 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
897 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
898 |
> |
new ArrayBlockingQueue<Runnable>(10), |
899 |
> |
new NoOpREHandler()); |
900 |
|
shouldThrow(); |
901 |
|
} catch (IllegalArgumentException success) {} |
902 |
|
} |
906 |
|
*/ |
907 |
|
public void testConstructor12() { |
908 |
|
try { |
909 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
909 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
910 |
> |
new ArrayBlockingQueue<Runnable>(10), |
911 |
> |
new NoOpREHandler()); |
912 |
|
shouldThrow(); |
913 |
|
} catch (IllegalArgumentException success) {} |
914 |
|
} |
918 |
|
*/ |
919 |
|
public void testConstructor13() { |
920 |
|
try { |
921 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
921 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
922 |
> |
new ArrayBlockingQueue<Runnable>(10), |
923 |
> |
new NoOpREHandler()); |
924 |
|
shouldThrow(); |
925 |
|
} catch (IllegalArgumentException success) {} |
926 |
|
} |
930 |
|
*/ |
931 |
|
public void testConstructor14() { |
932 |
|
try { |
933 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
933 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
934 |
> |
new ArrayBlockingQueue<Runnable>(10), |
935 |
> |
new NoOpREHandler()); |
936 |
|
shouldThrow(); |
937 |
|
} catch (IllegalArgumentException success) {} |
938 |
|
} |
942 |
|
*/ |
943 |
|
public void testConstructor15() { |
944 |
|
try { |
945 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
945 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
946 |
> |
new ArrayBlockingQueue<Runnable>(10), |
947 |
> |
new NoOpREHandler()); |
948 |
|
shouldThrow(); |
949 |
|
} catch (IllegalArgumentException success) {} |
950 |
|
} |
954 |
|
*/ |
955 |
|
public void testConstructorNullPointerException4() { |
956 |
|
try { |
957 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
957 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
958 |
> |
null, |
959 |
> |
new NoOpREHandler()); |
960 |
|
shouldThrow(); |
961 |
|
} catch (NullPointerException success) {} |
962 |
|
} |
966 |
|
*/ |
967 |
|
public void testConstructorNullPointerException5() { |
968 |
|
try { |
969 |
< |
RejectedExecutionHandler r = null; |
970 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
969 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
970 |
> |
new ArrayBlockingQueue<Runnable>(10), |
971 |
> |
(RejectedExecutionHandler) null); |
972 |
|
shouldThrow(); |
973 |
|
} catch (NullPointerException success) {} |
974 |
|
} |
975 |
|
|
898 |
– |
|
976 |
|
/** |
977 |
|
* Constructor throws if corePoolSize argument is less than zero |
978 |
|
*/ |
979 |
|
public void testConstructor16() { |
980 |
|
try { |
981 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
981 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
982 |
> |
new ArrayBlockingQueue<Runnable>(10), |
983 |
> |
new SimpleThreadFactory(), |
984 |
> |
new NoOpREHandler()); |
985 |
|
shouldThrow(); |
986 |
|
} catch (IllegalArgumentException success) {} |
987 |
|
} |
991 |
|
*/ |
992 |
|
public void testConstructor17() { |
993 |
|
try { |
994 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
994 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
995 |
> |
new ArrayBlockingQueue<Runnable>(10), |
996 |
> |
new SimpleThreadFactory(), |
997 |
> |
new NoOpREHandler()); |
998 |
|
shouldThrow(); |
999 |
|
} catch (IllegalArgumentException success) {} |
1000 |
|
} |
1004 |
|
*/ |
1005 |
|
public void testConstructor18() { |
1006 |
|
try { |
1007 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1007 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1008 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1009 |
> |
new SimpleThreadFactory(), |
1010 |
> |
new NoOpREHandler()); |
1011 |
|
shouldThrow(); |
1012 |
|
} catch (IllegalArgumentException success) {} |
1013 |
|
} |
1017 |
|
*/ |
1018 |
|
public void testConstructor19() { |
1019 |
|
try { |
1020 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1020 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1021 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1022 |
> |
new SimpleThreadFactory(), |
1023 |
> |
new NoOpREHandler()); |
1024 |
|
shouldThrow(); |
1025 |
|
} catch (IllegalArgumentException success) {} |
1026 |
|
} |
1030 |
|
*/ |
1031 |
|
public void testConstructor20() { |
1032 |
|
try { |
1033 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1033 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1034 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1035 |
> |
new SimpleThreadFactory(), |
1036 |
> |
new NoOpREHandler()); |
1037 |
|
shouldThrow(); |
1038 |
|
} catch (IllegalArgumentException success) {} |
1039 |
|
} |
1043 |
|
*/ |
1044 |
|
public void testConstructorNullPointerException6() { |
1045 |
|
try { |
1046 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1046 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1047 |
> |
null, |
1048 |
> |
new SimpleThreadFactory(), |
1049 |
> |
new NoOpREHandler()); |
1050 |
|
shouldThrow(); |
1051 |
|
} catch (NullPointerException success) {} |
1052 |
|
} |
1056 |
|
*/ |
1057 |
|
public void testConstructorNullPointerException7() { |
1058 |
|
try { |
1059 |
< |
RejectedExecutionHandler r = null; |
1060 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1059 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1060 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1061 |
> |
new SimpleThreadFactory(), |
1062 |
> |
(RejectedExecutionHandler) null); |
1063 |
|
shouldThrow(); |
1064 |
|
} catch (NullPointerException success) {} |
1065 |
|
} |
1069 |
|
*/ |
1070 |
|
public void testConstructorNullPointerException8() { |
1071 |
|
try { |
1072 |
< |
new CustomTPE(1, 2, |
976 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1072 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1073 |
|
new ArrayBlockingQueue<Runnable>(10), |
1074 |
|
(ThreadFactory) null, |
1075 |
|
new NoOpREHandler()); |
1077 |
|
} catch (NullPointerException success) {} |
1078 |
|
} |
1079 |
|
|
984 |
– |
|
1080 |
|
/** |
1081 |
|
* execute throws RejectedExecutionException if saturated. |
1082 |
|
*/ |
1226 |
|
} |
1227 |
|
} |
1228 |
|
|
1134 |
– |
|
1229 |
|
/** |
1230 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1231 |
|
*/ |
1243 |
|
} |
1244 |
|
} |
1245 |
|
|
1152 |
– |
|
1246 |
|
/** |
1247 |
|
* execute(null) throws NPE |
1248 |
|
*/ |
1249 |
|
public void testExecuteNull() { |
1250 |
< |
ThreadPoolExecutor p = null; |
1250 |
> |
ThreadPoolExecutor p = |
1251 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1252 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1253 |
|
try { |
1159 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1254 |
|
p.execute(null); |
1255 |
|
shouldThrow(); |
1256 |
|
} catch (NullPointerException success) {} |
1308 |
|
joinPool(p); |
1309 |
|
} |
1310 |
|
|
1217 |
– |
|
1311 |
|
/** |
1312 |
|
* setKeepAliveTime throws IllegalArgumentException |
1313 |
|
* when given a negative value |
1332 |
|
public void testTerminated() { |
1333 |
|
CustomTPE p = new CustomTPE(); |
1334 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1335 |
< |
assertTrue(p.terminatedCalled); |
1335 |
> |
assertTrue(p.terminatedCalled()); |
1336 |
|
joinPool(p); |
1337 |
|
} |
1338 |
|
|
1342 |
|
public void testBeforeAfter() throws InterruptedException { |
1343 |
|
CustomTPE p = new CustomTPE(); |
1344 |
|
try { |
1345 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1346 |
< |
p.execute(r); |
1347 |
< |
delay(SHORT_DELAY_MS); |
1348 |
< |
assertTrue(r.done); |
1349 |
< |
assertTrue(p.beforeCalled); |
1350 |
< |
assertTrue(p.afterCalled); |
1345 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1346 |
> |
p.execute(new CheckedRunnable() { |
1347 |
> |
public void realRun() { |
1348 |
> |
done.countDown(); |
1349 |
> |
}}); |
1350 |
> |
await(p.afterCalled); |
1351 |
> |
assertEquals(0, done.getCount()); |
1352 |
> |
assertTrue(p.afterCalled()); |
1353 |
> |
assertTrue(p.beforeCalled()); |
1354 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1355 |
|
} finally { |
1356 |
|
joinPool(p); |
1399 |
|
} |
1400 |
|
} |
1401 |
|
|
1306 |
– |
|
1402 |
|
/** |
1403 |
|
* invokeAny(null) throws NPE |
1404 |
|
*/ |
1560 |
|
} |
1561 |
|
} |
1562 |
|
|
1468 |
– |
|
1469 |
– |
|
1563 |
|
/** |
1564 |
|
* timed invokeAny(null) throws NPE |
1565 |
|
*/ |
1746 |
|
l.add(new StringTask()); |
1747 |
|
l.add(new StringTask()); |
1748 |
|
List<Future<String>> futures = |
1749 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1749 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1750 |
|
assertEquals(2, futures.size()); |
1751 |
|
for (Future<String> future : futures) |
1752 |
|
assertSame(TEST_STRING, future.get()); |
1761 |
|
public void testTimedInvokeAll6() throws Exception { |
1762 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1763 |
|
try { |
1764 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1765 |
< |
l.add(new StringTask()); |
1766 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1767 |
< |
l.add(new StringTask()); |
1768 |
< |
List<Future<String>> futures = |
1769 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1770 |
< |
assertEquals(3, futures.size()); |
1771 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1772 |
< |
Future<String> f1 = it.next(); |
1773 |
< |
Future<String> f2 = it.next(); |
1774 |
< |
Future<String> f3 = it.next(); |
1775 |
< |
assertTrue(f1.isDone()); |
1776 |
< |
assertTrue(f2.isDone()); |
1777 |
< |
assertTrue(f3.isDone()); |
1778 |
< |
assertFalse(f1.isCancelled()); |
1779 |
< |
assertTrue(f2.isCancelled()); |
1764 |
> |
for (long timeout = timeoutMillis();;) { |
1765 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1766 |
> |
tasks.add(new StringTask("0")); |
1767 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1768 |
> |
tasks.add(new StringTask("2")); |
1769 |
> |
long startTime = System.nanoTime(); |
1770 |
> |
List<Future<String>> futures = |
1771 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1772 |
> |
assertEquals(tasks.size(), futures.size()); |
1773 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1774 |
> |
for (Future future : futures) |
1775 |
> |
assertTrue(future.isDone()); |
1776 |
> |
assertTrue(futures.get(1).isCancelled()); |
1777 |
> |
try { |
1778 |
> |
assertEquals("0", futures.get(0).get()); |
1779 |
> |
assertEquals("2", futures.get(2).get()); |
1780 |
> |
break; |
1781 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1782 |
> |
timeout *= 2; |
1783 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1784 |
> |
fail("expected exactly one task to be cancelled"); |
1785 |
> |
} |
1786 |
> |
} |
1787 |
|
} finally { |
1788 |
|
joinPool(e); |
1789 |
|
} |
1826 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1827 |
|
*/ |
1828 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1829 |
+ |
long keepAliveTime = timeoutMillis(); |
1830 |
|
final ThreadPoolExecutor p = |
1831 |
|
new CustomTPE(2, 10, |
1832 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1832 |
> |
keepAliveTime, MILLISECONDS, |
1833 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1834 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1835 |
|
try { |
1836 |
|
p.allowCoreThreadTimeOut(true); |
1837 |
|
p.execute(new CheckedRunnable() { |
1838 |
< |
public void realRun() throws InterruptedException { |
1838 |
> |
public void realRun() { |
1839 |
|
threadStarted.countDown(); |
1840 |
|
assertEquals(1, p.getPoolSize()); |
1841 |
|
}}); |
1842 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1843 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1844 |
< |
if (p.getPoolSize() == 0) |
1845 |
< |
break; |
1846 |
< |
delay(10); |
1847 |
< |
} |
1842 |
> |
await(threadStarted); |
1843 |
> |
delay(keepAliveTime); |
1844 |
> |
long startTime = System.nanoTime(); |
1845 |
> |
while (p.getPoolSize() > 0 |
1846 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1847 |
> |
Thread.yield(); |
1848 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1849 |
|
assertEquals(0, p.getPoolSize()); |
1850 |
|
} finally { |
1851 |
|
joinPool(p); |
1856 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1857 |
|
*/ |
1858 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1859 |
+ |
long keepAliveTime = timeoutMillis(); |
1860 |
|
final ThreadPoolExecutor p = |
1861 |
|
new CustomTPE(2, 10, |
1862 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1862 |
> |
keepAliveTime, MILLISECONDS, |
1863 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1864 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1865 |
|
try { |
1869 |
|
threadStarted.countDown(); |
1870 |
|
assertTrue(p.getPoolSize() >= 1); |
1871 |
|
}}); |
1872 |
< |
delay(SMALL_DELAY_MS); |
1872 |
> |
delay(2 * keepAliveTime); |
1873 |
|
assertTrue(p.getPoolSize() >= 1); |
1874 |
|
} finally { |
1875 |
|
joinPool(p); |
1876 |
|
} |
1877 |
|
} |
1878 |
|
|
1879 |
+ |
/** |
1880 |
+ |
* get(cancelled task) throws CancellationException |
1881 |
+ |
* (in part, a test of CustomTPE itself) |
1882 |
+ |
*/ |
1883 |
+ |
public void testGet_cancelled() throws Exception { |
1884 |
+ |
final ExecutorService e = |
1885 |
+ |
new CustomTPE(1, 1, |
1886 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1887 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1888 |
+ |
try { |
1889 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1890 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1891 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1892 |
+ |
for (int i = 0; i < 2; i++) { |
1893 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1894 |
+ |
throws Throwable { |
1895 |
+ |
blockerStarted.countDown(); |
1896 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1897 |
+ |
}}; |
1898 |
+ |
futures.add(e.submit(r)); |
1899 |
+ |
} |
1900 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1901 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1902 |
+ |
for (Future<?> future : futures) { |
1903 |
+ |
try { |
1904 |
+ |
future.get(); |
1905 |
+ |
shouldThrow(); |
1906 |
+ |
} catch (CancellationException success) {} |
1907 |
+ |
try { |
1908 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1909 |
+ |
shouldThrow(); |
1910 |
+ |
} catch (CancellationException success) {} |
1911 |
+ |
assertTrue(future.isCancelled()); |
1912 |
+ |
assertTrue(future.isDone()); |
1913 |
+ |
} |
1914 |
+ |
done.countDown(); |
1915 |
+ |
} finally { |
1916 |
+ |
joinPool(e); |
1917 |
+ |
} |
1918 |
+ |
} |
1919 |
+ |
|
1920 |
|
} |