1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
8 |
– |
import java.util.*; |
9 |
– |
import java.util.concurrent.*; |
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
< |
import java.util.concurrent.atomic.*; |
8 |
> |
|
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.List; |
11 |
> |
import java.util.concurrent.BlockingQueue; |
12 |
> |
import java.util.concurrent.Callable; |
13 |
> |
import java.util.concurrent.CancellationException; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.Delayed; |
16 |
> |
import java.util.concurrent.ExecutionException; |
17 |
> |
import java.util.concurrent.Executors; |
18 |
> |
import java.util.concurrent.ExecutorService; |
19 |
> |
import java.util.concurrent.Future; |
20 |
> |
import java.util.concurrent.RejectedExecutionException; |
21 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
22 |
> |
import java.util.concurrent.RunnableScheduledFuture; |
23 |
> |
import java.util.concurrent.ScheduledFuture; |
24 |
> |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
25 |
> |
import java.util.concurrent.ThreadFactory; |
26 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
27 |
> |
import java.util.concurrent.TimeoutException; |
28 |
> |
import java.util.concurrent.TimeUnit; |
29 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
30 |
> |
|
31 |
> |
import junit.framework.Test; |
32 |
> |
import junit.framework.TestSuite; |
33 |
|
|
34 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
35 |
|
public static void main(String[] args) { |
36 |
< |
junit.textui.TestRunner.run (suite()); |
36 |
> |
main(suite(), args); |
37 |
|
} |
38 |
|
public static Test suite() { |
39 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
57 |
|
} |
58 |
|
public boolean isCancelled() { return task.isCancelled(); } |
59 |
|
public boolean isDone() { return task.isDone(); } |
60 |
< |
public V get() throws InterruptedException, ExecutionException { |
60 |
> |
public V get() throws InterruptedException, ExecutionException { |
61 |
|
V v = task.get(); |
62 |
|
assertTrue(ran); |
63 |
|
return v; |
64 |
|
} |
65 |
< |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
65 |
> |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
66 |
|
V v = task.get(time, unit); |
67 |
|
assertTrue(ran); |
68 |
|
return v; |
69 |
|
} |
70 |
|
} |
71 |
|
|
51 |
– |
|
72 |
|
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
73 |
|
|
74 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
78 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) { |
79 |
|
return new CustomTask<V>(task); |
80 |
|
} |
81 |
< |
CustomExecutor(int corePoolSize) { super(corePoolSize);} |
81 |
> |
CustomExecutor(int corePoolSize) { super(corePoolSize); } |
82 |
|
CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { |
83 |
|
super(corePoolSize, handler); |
84 |
|
} |
93 |
|
|
94 |
|
} |
95 |
|
|
76 |
– |
|
77 |
– |
|
96 |
|
/** |
97 |
|
* execute successfully executes a runnable |
98 |
|
*/ |
99 |
|
public void testExecute() throws InterruptedException { |
100 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
101 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
102 |
< |
p1.execute(runnable); |
103 |
< |
assertFalse(runnable.done); |
104 |
< |
Thread.sleep(SHORT_DELAY_MS); |
105 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
106 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
107 |
< |
assertTrue(runnable.done); |
108 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
109 |
< |
joinPool(p1); |
100 |
> |
CustomExecutor p = new CustomExecutor(1); |
101 |
> |
final CountDownLatch done = new CountDownLatch(1); |
102 |
> |
final Runnable task = new CheckedRunnable() { |
103 |
> |
public void realRun() { |
104 |
> |
done.countDown(); |
105 |
> |
}}; |
106 |
> |
try { |
107 |
> |
p.execute(task); |
108 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
109 |
> |
} finally { |
110 |
> |
joinPool(p); |
111 |
> |
} |
112 |
|
} |
113 |
|
|
94 |
– |
|
114 |
|
/** |
115 |
|
* delayed schedule of callable successfully executes after delay |
116 |
|
*/ |
117 |
|
public void testSchedule1() throws Exception { |
118 |
< |
TrackedCallable callable = new TrackedCallable(); |
119 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
120 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
121 |
< |
assertFalse(callable.done); |
122 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
123 |
< |
assertTrue(callable.done); |
124 |
< |
assertEquals(Boolean.TRUE, f.get()); |
125 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
126 |
< |
joinPool(p1); |
118 |
> |
CustomExecutor p = new CustomExecutor(1); |
119 |
> |
final long startTime = System.nanoTime(); |
120 |
> |
final CountDownLatch done = new CountDownLatch(1); |
121 |
> |
try { |
122 |
> |
Callable task = new CheckedCallable<Boolean>() { |
123 |
> |
public Boolean realCall() { |
124 |
> |
done.countDown(); |
125 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
126 |
> |
return Boolean.TRUE; |
127 |
> |
}}; |
128 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
129 |
> |
assertSame(Boolean.TRUE, f.get()); |
130 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
131 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
132 |
> |
} finally { |
133 |
> |
joinPool(p); |
134 |
> |
} |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* delayed schedule of runnable successfully executes after delay |
139 |
< |
*/ |
140 |
< |
public void testSchedule3() throws InterruptedException { |
141 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
142 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
143 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
144 |
< |
Thread.sleep(SHORT_DELAY_MS); |
145 |
< |
assertFalse(runnable.done); |
146 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
147 |
< |
assertTrue(runnable.done); |
148 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
149 |
< |
joinPool(p1); |
138 |
> |
* delayed schedule of runnable successfully executes after delay |
139 |
> |
*/ |
140 |
> |
public void testSchedule3() throws Exception { |
141 |
> |
CustomExecutor p = new CustomExecutor(1); |
142 |
> |
final long startTime = System.nanoTime(); |
143 |
> |
final CountDownLatch done = new CountDownLatch(1); |
144 |
> |
try { |
145 |
> |
Runnable task = new CheckedRunnable() { |
146 |
> |
public void realRun() { |
147 |
> |
done.countDown(); |
148 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
149 |
> |
}}; |
150 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
151 |
> |
await(done); |
152 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
153 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
154 |
> |
} finally { |
155 |
> |
joinPool(p); |
156 |
> |
} |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
161 |
|
*/ |
162 |
|
public void testSchedule4() throws InterruptedException { |
163 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
164 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
165 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
166 |
< |
assertFalse(runnable.done); |
167 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
168 |
< |
assertTrue(runnable.done); |
169 |
< |
h.cancel(true); |
170 |
< |
joinPool(p1); |
171 |
< |
} |
172 |
< |
|
173 |
< |
static class RunnableCounter implements Runnable { |
174 |
< |
AtomicInteger count = new AtomicInteger(0); |
175 |
< |
public void run() { count.getAndIncrement(); } |
163 |
> |
CustomExecutor p = new CustomExecutor(1); |
164 |
> |
final long startTime = System.nanoTime(); |
165 |
> |
final CountDownLatch done = new CountDownLatch(1); |
166 |
> |
try { |
167 |
> |
Runnable task = new CheckedRunnable() { |
168 |
> |
public void realRun() { |
169 |
> |
done.countDown(); |
170 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
171 |
> |
}}; |
172 |
> |
ScheduledFuture f = |
173 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
174 |
> |
LONG_DELAY_MS, MILLISECONDS); |
175 |
> |
await(done); |
176 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
177 |
> |
f.cancel(true); |
178 |
> |
} finally { |
179 |
> |
joinPool(p); |
180 |
> |
} |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
185 |
|
*/ |
186 |
|
public void testSchedule5() throws InterruptedException { |
187 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
188 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
189 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
190 |
< |
assertFalse(runnable.done); |
191 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
192 |
< |
assertTrue(runnable.done); |
193 |
< |
h.cancel(true); |
194 |
< |
joinPool(p1); |
187 |
> |
CustomExecutor p = new CustomExecutor(1); |
188 |
> |
final long startTime = System.nanoTime(); |
189 |
> |
final CountDownLatch done = new CountDownLatch(1); |
190 |
> |
try { |
191 |
> |
Runnable task = new CheckedRunnable() { |
192 |
> |
public void realRun() { |
193 |
> |
done.countDown(); |
194 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
195 |
> |
}}; |
196 |
> |
ScheduledFuture f = |
197 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
198 |
> |
LONG_DELAY_MS, MILLISECONDS); |
199 |
> |
await(done); |
200 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
201 |
> |
f.cancel(true); |
202 |
> |
} finally { |
203 |
> |
joinPool(p); |
204 |
> |
} |
205 |
> |
} |
206 |
> |
|
207 |
> |
static class RunnableCounter implements Runnable { |
208 |
> |
AtomicInteger count = new AtomicInteger(0); |
209 |
> |
public void run() { count.getAndIncrement(); } |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
214 |
|
*/ |
215 |
|
public void testFixedRateSequence() throws InterruptedException { |
216 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
217 |
< |
RunnableCounter counter = new RunnableCounter(); |
218 |
< |
ScheduledFuture h = |
219 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
220 |
< |
Thread.sleep(SMALL_DELAY_MS); |
221 |
< |
h.cancel(true); |
222 |
< |
int c = counter.count.get(); |
223 |
< |
// By time scaling conventions, we must have at least |
224 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
225 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
226 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
227 |
< |
joinPool(p1); |
216 |
> |
CustomExecutor p = new CustomExecutor(1); |
217 |
> |
try { |
218 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
219 |
> |
long startTime = System.nanoTime(); |
220 |
> |
int cycles = 10; |
221 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
222 |
> |
Runnable task = new CheckedRunnable() { |
223 |
> |
public void realRun() { done.countDown(); }}; |
224 |
> |
ScheduledFuture h = |
225 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
226 |
> |
done.await(); |
227 |
> |
h.cancel(true); |
228 |
> |
double normalizedTime = |
229 |
> |
(double) millisElapsedSince(startTime) / delay; |
230 |
> |
if (normalizedTime >= cycles - 1 && |
231 |
> |
normalizedTime <= cycles) |
232 |
> |
return; |
233 |
> |
} |
234 |
> |
throw new AssertionError("unexpected execution rate"); |
235 |
> |
} finally { |
236 |
> |
joinPool(p); |
237 |
> |
} |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
242 |
|
*/ |
243 |
|
public void testFixedDelaySequence() throws InterruptedException { |
244 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
245 |
< |
RunnableCounter counter = new RunnableCounter(); |
246 |
< |
ScheduledFuture h = |
247 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
248 |
< |
Thread.sleep(SMALL_DELAY_MS); |
249 |
< |
h.cancel(true); |
250 |
< |
int c = counter.count.get(); |
251 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
252 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
253 |
< |
joinPool(p1); |
244 |
> |
CustomExecutor p = new CustomExecutor(1); |
245 |
> |
try { |
246 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
247 |
> |
long startTime = System.nanoTime(); |
248 |
> |
int cycles = 10; |
249 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
250 |
> |
Runnable task = new CheckedRunnable() { |
251 |
> |
public void realRun() { done.countDown(); }}; |
252 |
> |
ScheduledFuture h = |
253 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
254 |
> |
done.await(); |
255 |
> |
h.cancel(true); |
256 |
> |
double normalizedTime = |
257 |
> |
(double) millisElapsedSince(startTime) / delay; |
258 |
> |
if (normalizedTime >= cycles - 1 && |
259 |
> |
normalizedTime <= cycles) |
260 |
> |
return; |
261 |
> |
} |
262 |
> |
throw new AssertionError("unexpected execution rate"); |
263 |
> |
} finally { |
264 |
> |
joinPool(p); |
265 |
> |
} |
266 |
|
} |
267 |
|
|
192 |
– |
|
268 |
|
/** |
269 |
< |
* execute (null) throws NPE |
269 |
> |
* execute(null) throws NPE |
270 |
|
*/ |
271 |
|
public void testExecuteNull() throws InterruptedException { |
272 |
|
CustomExecutor se = new CustomExecutor(1); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* schedule (null) throws NPE |
281 |
> |
* schedule(null) throws NPE |
282 |
|
*/ |
283 |
|
public void testScheduleNull() throws InterruptedException { |
284 |
|
CustomExecutor se = new CustomExecutor(1); |
326 |
|
/** |
327 |
|
* schedule callable throws RejectedExecutionException if shutdown |
328 |
|
*/ |
329 |
< |
public void testSchedule3_RejectedExecutionException() { |
330 |
< |
CustomExecutor se = new CustomExecutor(1); |
331 |
< |
try { |
332 |
< |
se.shutdown(); |
333 |
< |
se.schedule(new NoOpCallable(), |
334 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
335 |
< |
shouldThrow(); |
336 |
< |
} catch (RejectedExecutionException success) { |
337 |
< |
} catch (SecurityException ok) { |
338 |
< |
} |
339 |
< |
joinPool(se); |
329 |
> |
public void testSchedule3_RejectedExecutionException() { |
330 |
> |
CustomExecutor se = new CustomExecutor(1); |
331 |
> |
try { |
332 |
> |
se.shutdown(); |
333 |
> |
se.schedule(new NoOpCallable(), |
334 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
335 |
> |
shouldThrow(); |
336 |
> |
} catch (RejectedExecutionException success) { |
337 |
> |
} catch (SecurityException ok) { |
338 |
> |
} |
339 |
> |
joinPool(se); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
343 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
344 |
|
*/ |
345 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
346 |
|
CustomExecutor se = new CustomExecutor(1); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* getActiveCount increases but doesn't overestimate, when a |
376 |
< |
* thread becomes active |
375 |
> |
* getActiveCount increases but doesn't overestimate, when a |
376 |
> |
* thread becomes active |
377 |
|
*/ |
378 |
|
public void testGetActiveCount() throws InterruptedException { |
379 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
380 |
< |
assertEquals(0, p2.getActiveCount()); |
381 |
< |
p2.execute(new SmallRunnable()); |
382 |
< |
Thread.sleep(SHORT_DELAY_MS); |
383 |
< |
assertEquals(1, p2.getActiveCount()); |
384 |
< |
joinPool(p2); |
379 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
380 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
381 |
> |
final CountDownLatch done = new CountDownLatch(1); |
382 |
> |
try { |
383 |
> |
assertEquals(0, p.getActiveCount()); |
384 |
> |
p.execute(new CheckedRunnable() { |
385 |
> |
public void realRun() throws InterruptedException { |
386 |
> |
threadStarted.countDown(); |
387 |
> |
assertEquals(1, p.getActiveCount()); |
388 |
> |
done.await(); |
389 |
> |
}}); |
390 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
391 |
> |
assertEquals(1, p.getActiveCount()); |
392 |
> |
} finally { |
393 |
> |
done.countDown(); |
394 |
> |
joinPool(p); |
395 |
> |
} |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
400 |
< |
* when tasks complete |
399 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
400 |
> |
* when tasks complete |
401 |
|
*/ |
402 |
< |
public void testGetCompletedTaskCount()throws InterruptedException { |
403 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
404 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
405 |
< |
p2.execute(new SmallRunnable()); |
406 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
407 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
408 |
< |
joinPool(p2); |
402 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
403 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
404 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
405 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
406 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
407 |
> |
try { |
408 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
409 |
> |
p.execute(new CheckedRunnable() { |
410 |
> |
public void realRun() throws InterruptedException { |
411 |
> |
threadStarted.countDown(); |
412 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
413 |
> |
threadProceed.await(); |
414 |
> |
threadDone.countDown(); |
415 |
> |
}}); |
416 |
> |
await(threadStarted); |
417 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
418 |
> |
threadProceed.countDown(); |
419 |
> |
threadDone.await(); |
420 |
> |
long startTime = System.nanoTime(); |
421 |
> |
while (p.getCompletedTaskCount() != 1) { |
422 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
423 |
> |
fail("timed out"); |
424 |
> |
Thread.yield(); |
425 |
> |
} |
426 |
> |
} finally { |
427 |
> |
joinPool(p); |
428 |
> |
} |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
432 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
433 |
|
*/ |
434 |
|
public void testGetCorePoolSize() { |
435 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
436 |
< |
assertEquals(1, p1.getCorePoolSize()); |
437 |
< |
joinPool(p1); |
435 |
> |
CustomExecutor p = new CustomExecutor(1); |
436 |
> |
assertEquals(1, p.getCorePoolSize()); |
437 |
> |
joinPool(p); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
442 |
< |
* multiple threads active |
441 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
442 |
> |
* multiple threads active |
443 |
|
*/ |
444 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
445 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
446 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
447 |
< |
p2.execute(new SmallRunnable()); |
448 |
< |
p2.execute(new SmallRunnable()); |
449 |
< |
Thread.sleep(SHORT_DELAY_MS); |
450 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
451 |
< |
joinPool(p2); |
445 |
> |
final int THREADS = 3; |
446 |
> |
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
447 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
448 |
> |
final CountDownLatch done = new CountDownLatch(1); |
449 |
> |
try { |
450 |
> |
assertEquals(0, p.getLargestPoolSize()); |
451 |
> |
for (int i = 0; i < THREADS; i++) |
452 |
> |
p.execute(new CheckedRunnable() { |
453 |
> |
public void realRun() throws InterruptedException { |
454 |
> |
threadsStarted.countDown(); |
455 |
> |
done.await(); |
456 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
457 |
> |
}}); |
458 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
459 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
460 |
> |
} finally { |
461 |
> |
done.countDown(); |
462 |
> |
joinPool(p); |
463 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
464 |
> |
} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
469 |
< |
* become active |
468 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
469 |
> |
* become active |
470 |
|
*/ |
471 |
< |
public void testGetPoolSize() { |
472 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
473 |
< |
assertEquals(0, p1.getPoolSize()); |
474 |
< |
p1.execute(new SmallRunnable()); |
475 |
< |
assertEquals(1, p1.getPoolSize()); |
476 |
< |
joinPool(p1); |
471 |
> |
public void testGetPoolSize() throws InterruptedException { |
472 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
473 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
474 |
> |
final CountDownLatch done = new CountDownLatch(1); |
475 |
> |
try { |
476 |
> |
assertEquals(0, p.getPoolSize()); |
477 |
> |
p.execute(new CheckedRunnable() { |
478 |
> |
public void realRun() throws InterruptedException { |
479 |
> |
threadStarted.countDown(); |
480 |
> |
assertEquals(1, p.getPoolSize()); |
481 |
> |
done.await(); |
482 |
> |
}}); |
483 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
484 |
> |
assertEquals(1, p.getPoolSize()); |
485 |
> |
} finally { |
486 |
> |
done.countDown(); |
487 |
> |
joinPool(p); |
488 |
> |
} |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
493 |
< |
* submitted |
492 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
493 |
> |
* submitted |
494 |
|
*/ |
495 |
|
public void testGetTaskCount() throws InterruptedException { |
496 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
497 |
< |
assertEquals(0, p1.getTaskCount()); |
498 |
< |
for (int i = 0; i < 5; i++) |
499 |
< |
p1.execute(new SmallRunnable()); |
500 |
< |
Thread.sleep(SHORT_DELAY_MS); |
501 |
< |
assertEquals(5, p1.getTaskCount()); |
502 |
< |
joinPool(p1); |
496 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
497 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
498 |
> |
final CountDownLatch done = new CountDownLatch(1); |
499 |
> |
final int TASKS = 5; |
500 |
> |
try { |
501 |
> |
assertEquals(0, p.getTaskCount()); |
502 |
> |
for (int i = 0; i < TASKS; i++) |
503 |
> |
p.execute(new CheckedRunnable() { |
504 |
> |
public void realRun() throws InterruptedException { |
505 |
> |
threadStarted.countDown(); |
506 |
> |
done.await(); |
507 |
> |
}}); |
508 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
509 |
> |
assertEquals(TASKS, p.getTaskCount()); |
510 |
> |
} finally { |
511 |
> |
done.countDown(); |
512 |
> |
joinPool(p); |
513 |
> |
} |
514 |
|
} |
515 |
|
|
516 |
|
/** |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* is isShutDown is false before shutdown, true after |
552 |
> |
* isShutdown is false before shutdown, true after |
553 |
|
*/ |
554 |
|
public void testIsShutdown() { |
555 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
555 |
> |
CustomExecutor p = new CustomExecutor(1); |
556 |
|
try { |
557 |
< |
assertFalse(p1.isShutdown()); |
557 |
> |
assertFalse(p.isShutdown()); |
558 |
|
} |
559 |
|
finally { |
560 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
560 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
561 |
|
} |
562 |
< |
assertTrue(p1.isShutdown()); |
562 |
> |
assertTrue(p.isShutdown()); |
563 |
|
} |
564 |
|
|
423 |
– |
|
565 |
|
/** |
566 |
< |
* isTerminated is false before termination, true after |
566 |
> |
* isTerminated is false before termination, true after |
567 |
|
*/ |
568 |
|
public void testIsTerminated() throws InterruptedException { |
569 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
569 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
570 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
571 |
> |
final CountDownLatch done = new CountDownLatch(1); |
572 |
> |
assertFalse(p.isTerminated()); |
573 |
|
try { |
574 |
< |
p1.execute(new SmallRunnable()); |
574 |
> |
p.execute(new CheckedRunnable() { |
575 |
> |
public void realRun() throws InterruptedException { |
576 |
> |
assertFalse(p.isTerminated()); |
577 |
> |
threadStarted.countDown(); |
578 |
> |
done.await(); |
579 |
> |
}}); |
580 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
581 |
> |
assertFalse(p.isTerminating()); |
582 |
> |
done.countDown(); |
583 |
|
} finally { |
584 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
584 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
585 |
|
} |
586 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
587 |
< |
assertTrue(p1.isTerminated()); |
586 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
587 |
> |
assertTrue(p.isTerminated()); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* isTerminating is not true when running or when terminated |
591 |
> |
* isTerminating is not true when running or when terminated |
592 |
|
*/ |
593 |
|
public void testIsTerminating() throws InterruptedException { |
594 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
595 |
< |
assertFalse(p1.isTerminating()); |
596 |
< |
try { |
597 |
< |
p1.execute(new SmallRunnable()); |
598 |
< |
assertFalse(p1.isTerminating()); |
599 |
< |
} finally { |
600 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
601 |
< |
} |
602 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
603 |
< |
assertTrue(p1.isTerminated()); |
604 |
< |
assertFalse(p1.isTerminating()); |
594 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
595 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
596 |
> |
final CountDownLatch done = new CountDownLatch(1); |
597 |
> |
try { |
598 |
> |
assertFalse(p.isTerminating()); |
599 |
> |
p.execute(new CheckedRunnable() { |
600 |
> |
public void realRun() throws InterruptedException { |
601 |
> |
assertFalse(p.isTerminating()); |
602 |
> |
threadStarted.countDown(); |
603 |
> |
done.await(); |
604 |
> |
}}); |
605 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
606 |
> |
assertFalse(p.isTerminating()); |
607 |
> |
done.countDown(); |
608 |
> |
} finally { |
609 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
610 |
> |
} |
611 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
612 |
> |
assertTrue(p.isTerminated()); |
613 |
> |
assertFalse(p.isTerminating()); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* getQueue returns the work queue, which contains queued tasks |
618 |
|
*/ |
619 |
|
public void testGetQueue() throws InterruptedException { |
620 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
621 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
622 |
< |
for (int i = 0; i < 5; i++) { |
623 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
624 |
< |
} |
625 |
< |
try { |
626 |
< |
Thread.sleep(SHORT_DELAY_MS); |
627 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
628 |
< |
assertTrue(q.contains(tasks[4])); |
620 |
> |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
621 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
622 |
> |
final CountDownLatch done = new CountDownLatch(1); |
623 |
> |
try { |
624 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
625 |
> |
for (int i = 0; i < tasks.length; i++) { |
626 |
> |
Runnable r = new CheckedRunnable() { |
627 |
> |
public void realRun() throws InterruptedException { |
628 |
> |
threadStarted.countDown(); |
629 |
> |
done.await(); |
630 |
> |
}}; |
631 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
632 |
> |
} |
633 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
634 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
635 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
636 |
|
assertFalse(q.contains(tasks[0])); |
637 |
|
} finally { |
638 |
< |
joinPool(p1); |
638 |
> |
done.countDown(); |
639 |
> |
joinPool(p); |
640 |
|
} |
641 |
|
} |
642 |
|
|
644 |
|
* remove(task) removes queued task, and fails to remove active task |
645 |
|
*/ |
646 |
|
public void testRemove() throws InterruptedException { |
647 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
647 |
> |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
648 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
649 |
< |
for (int i = 0; i < 5; i++) { |
650 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
482 |
< |
} |
649 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
650 |
> |
final CountDownLatch done = new CountDownLatch(1); |
651 |
|
try { |
652 |
< |
Thread.sleep(SHORT_DELAY_MS); |
653 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
654 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
652 |
> |
for (int i = 0; i < tasks.length; i++) { |
653 |
> |
Runnable r = new CheckedRunnable() { |
654 |
> |
public void realRun() throws InterruptedException { |
655 |
> |
threadStarted.countDown(); |
656 |
> |
done.await(); |
657 |
> |
}}; |
658 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
659 |
> |
} |
660 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
661 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
662 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
663 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
664 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
665 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
666 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
665 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
666 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
667 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
668 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
669 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
669 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
670 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
671 |
|
} finally { |
672 |
< |
joinPool(p1); |
672 |
> |
done.countDown(); |
673 |
> |
joinPool(p); |
674 |
|
} |
675 |
|
} |
676 |
|
|
677 |
|
/** |
678 |
< |
* purge removes cancelled tasks from the queue |
678 |
> |
* purge removes cancelled tasks from the queue |
679 |
|
*/ |
680 |
|
public void testPurge() throws InterruptedException { |
681 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
681 |
> |
CustomExecutor p = new CustomExecutor(1); |
682 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
683 |
< |
for (int i = 0; i < 5; i++) { |
684 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
685 |
< |
} |
683 |
> |
for (int i = 0; i < tasks.length; i++) |
684 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
685 |
> |
LONG_DELAY_MS, MILLISECONDS); |
686 |
|
try { |
687 |
< |
int max = 5; |
687 |
> |
int max = tasks.length; |
688 |
|
if (tasks[4].cancel(true)) --max; |
689 |
|
if (tasks[3].cancel(true)) --max; |
690 |
|
// There must eventually be an interference-free point at |
691 |
|
// which purge will not fail. (At worst, when queue is empty.) |
692 |
< |
int k; |
693 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
694 |
< |
p1.purge(); |
695 |
< |
long count = p1.getTaskCount(); |
696 |
< |
if (count >= 0 && count <= max) |
697 |
< |
break; |
698 |
< |
Thread.sleep(1); |
699 |
< |
} |
523 |
< |
assertTrue(k < SMALL_DELAY_MS); |
692 |
> |
long startTime = System.nanoTime(); |
693 |
> |
do { |
694 |
> |
p.purge(); |
695 |
> |
long count = p.getTaskCount(); |
696 |
> |
if (count == max) |
697 |
> |
return; |
698 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
699 |
> |
fail("Purge failed to remove cancelled tasks"); |
700 |
|
} finally { |
701 |
< |
joinPool(p1); |
701 |
> |
for (ScheduledFuture task : tasks) |
702 |
> |
task.cancel(true); |
703 |
> |
joinPool(p); |
704 |
|
} |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* shutDownNow returns a list containing tasks that were not run |
708 |
> |
* shutdownNow returns a list containing tasks that were not run |
709 |
|
*/ |
710 |
< |
public void testShutDownNow() { |
711 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
710 |
> |
public void testShutdownNow() { |
711 |
> |
CustomExecutor p = new CustomExecutor(1); |
712 |
|
for (int i = 0; i < 5; i++) |
713 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
714 |
< |
List l; |
713 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
714 |
> |
LONG_DELAY_MS, MILLISECONDS); |
715 |
|
try { |
716 |
< |
l = p1.shutdownNow(); |
716 |
> |
List<Runnable> l = p.shutdownNow(); |
717 |
> |
assertTrue(p.isShutdown()); |
718 |
> |
assertEquals(5, l.size()); |
719 |
|
} catch (SecurityException ok) { |
720 |
< |
return; |
720 |
> |
// Allowed in case test doesn't have privs |
721 |
> |
} finally { |
722 |
> |
joinPool(p); |
723 |
|
} |
542 |
– |
assertTrue(p1.isShutdown()); |
543 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
544 |
– |
joinPool(p1); |
724 |
|
} |
725 |
|
|
726 |
|
/** |
727 |
|
* In default setting, shutdown cancels periodic but not delayed |
728 |
|
* tasks at shutdown |
729 |
|
*/ |
730 |
< |
public void testShutDown1() throws InterruptedException { |
731 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
732 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
733 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
730 |
> |
public void testShutdown1() throws InterruptedException { |
731 |
> |
CustomExecutor p = new CustomExecutor(1); |
732 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
733 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
734 |
|
|
735 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
736 |
< |
for (int i = 0; i < 5; i++) |
737 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
738 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
739 |
< |
BlockingQueue q = p1.getQueue(); |
740 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
741 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
742 |
< |
assertFalse(t.isCancelled()); |
743 |
< |
} |
744 |
< |
assertTrue(p1.isShutdown()); |
745 |
< |
Thread.sleep(SMALL_DELAY_MS); |
746 |
< |
for (int i = 0; i < 5; ++i) { |
747 |
< |
assertTrue(tasks[i].isDone()); |
748 |
< |
assertFalse(tasks[i].isCancelled()); |
736 |
> |
for (int i = 0; i < tasks.length; i++) |
737 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
738 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
739 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
740 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
741 |
> |
for (ScheduledFuture task : tasks) { |
742 |
> |
assertFalse(task.isDone()); |
743 |
> |
assertFalse(task.isCancelled()); |
744 |
> |
assertTrue(q.contains(task)); |
745 |
> |
} |
746 |
> |
assertTrue(p.isShutdown()); |
747 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
748 |
> |
assertTrue(p.isTerminated()); |
749 |
> |
for (ScheduledFuture task : tasks) { |
750 |
> |
assertTrue(task.isDone()); |
751 |
> |
assertFalse(task.isCancelled()); |
752 |
|
} |
753 |
|
} |
754 |
|
|
573 |
– |
|
755 |
|
/** |
756 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
757 |
|
* delayed tasks are cancelled at shutdown |
758 |
|
*/ |
759 |
< |
public void testShutDown2() throws InterruptedException { |
760 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
761 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
759 |
> |
public void testShutdown2() throws InterruptedException { |
760 |
> |
CustomExecutor p = new CustomExecutor(1); |
761 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
762 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
763 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
764 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
765 |
< |
for (int i = 0; i < 5; i++) |
766 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
767 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
768 |
< |
assertTrue(p1.isShutdown()); |
769 |
< |
BlockingQueue q = p1.getQueue(); |
765 |
> |
for (int i = 0; i < tasks.length; i++) |
766 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
767 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
768 |
> |
BlockingQueue q = p.getQueue(); |
769 |
> |
assertEquals(tasks.length, q.size()); |
770 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
771 |
> |
assertTrue(p.isShutdown()); |
772 |
|
assertTrue(q.isEmpty()); |
773 |
< |
Thread.sleep(SMALL_DELAY_MS); |
774 |
< |
assertTrue(p1.isTerminated()); |
773 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
774 |
> |
assertTrue(p.isTerminated()); |
775 |
> |
for (ScheduledFuture task : tasks) { |
776 |
> |
assertTrue(task.isDone()); |
777 |
> |
assertTrue(task.isCancelled()); |
778 |
> |
} |
779 |
|
} |
780 |
|
|
592 |
– |
|
781 |
|
/** |
782 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
783 |
< |
* periodic tasks are not cancelled at shutdown |
783 |
> |
* periodic tasks are cancelled at shutdown |
784 |
|
*/ |
785 |
< |
public void testShutDown3() throws InterruptedException { |
786 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
787 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
785 |
> |
public void testShutdown3() throws InterruptedException { |
786 |
> |
CustomExecutor p = new CustomExecutor(1); |
787 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
788 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
789 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
790 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
791 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
792 |
> |
long initialDelay = LONG_DELAY_MS; |
793 |
|
ScheduledFuture task = |
794 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
795 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
796 |
< |
assertTrue(p1.isShutdown()); |
797 |
< |
BlockingQueue q = p1.getQueue(); |
798 |
< |
assertTrue(q.isEmpty()); |
799 |
< |
Thread.sleep(SHORT_DELAY_MS); |
800 |
< |
assertTrue(p1.isTerminated()); |
794 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
795 |
> |
5, MILLISECONDS); |
796 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
797 |
> |
assertTrue(p.isShutdown()); |
798 |
> |
assertTrue(p.getQueue().isEmpty()); |
799 |
> |
assertTrue(task.isDone()); |
800 |
> |
assertTrue(task.isCancelled()); |
801 |
> |
joinPool(p); |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
806 |
< |
* periodic tasks are cancelled at shutdown |
806 |
> |
* periodic tasks are not cancelled at shutdown |
807 |
|
*/ |
808 |
< |
public void testShutDown4() throws InterruptedException { |
809 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
808 |
> |
public void testShutdown4() throws InterruptedException { |
809 |
> |
CustomExecutor p = new CustomExecutor(1); |
810 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
811 |
|
try { |
812 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
812 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
813 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
814 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
815 |
> |
final Runnable r = new CheckedRunnable() { |
816 |
> |
public void realRun() { |
817 |
> |
counter.countDown(); |
818 |
> |
}}; |
819 |
|
ScheduledFuture task = |
820 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
820 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
821 |
> |
assertFalse(task.isDone()); |
822 |
|
assertFalse(task.isCancelled()); |
823 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
823 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
824 |
|
assertFalse(task.isCancelled()); |
825 |
< |
assertFalse(p1.isTerminated()); |
826 |
< |
assertTrue(p1.isShutdown()); |
827 |
< |
Thread.sleep(SHORT_DELAY_MS); |
825 |
> |
assertFalse(p.isTerminated()); |
826 |
> |
assertTrue(p.isShutdown()); |
827 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
828 |
|
assertFalse(task.isCancelled()); |
829 |
< |
assertTrue(task.cancel(true)); |
829 |
> |
assertTrue(task.cancel(false)); |
830 |
|
assertTrue(task.isDone()); |
831 |
< |
Thread.sleep(SHORT_DELAY_MS); |
832 |
< |
assertTrue(p1.isTerminated()); |
831 |
> |
assertTrue(task.isCancelled()); |
832 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
833 |
> |
assertTrue(p.isTerminated()); |
834 |
|
} |
835 |
|
finally { |
836 |
< |
joinPool(p1); |
836 |
> |
joinPool(p); |
837 |
|
} |
838 |
|
} |
839 |
|
|
911 |
|
* invokeAny(c) throws NPE if c has null elements |
912 |
|
*/ |
913 |
|
public void testInvokeAny3() throws Exception { |
914 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
914 |
> |
CountDownLatch latch = new CountDownLatch(1); |
915 |
|
ExecutorService e = new CustomExecutor(2); |
916 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
917 |
+ |
l.add(latchAwaitingStringTask(latch)); |
918 |
+ |
l.add(null); |
919 |
|
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 ok) {} |
720 |
– |
return TEST_STRING; |
721 |
– |
}}); |
722 |
– |
l.add(null); |
920 |
|
e.invokeAny(l); |
921 |
|
shouldThrow(); |
922 |
|
} catch (NullPointerException success) { |
931 |
|
*/ |
932 |
|
public void testInvokeAny4() throws Exception { |
933 |
|
ExecutorService e = new CustomExecutor(2); |
934 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
935 |
+ |
l.add(new NPETask()); |
936 |
|
try { |
738 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
739 |
– |
l.add(new NPETask()); |
937 |
|
e.invokeAny(l); |
938 |
|
shouldThrow(); |
939 |
|
} catch (ExecutionException success) { |
949 |
|
public void testInvokeAny5() throws Exception { |
950 |
|
ExecutorService e = new CustomExecutor(2); |
951 |
|
try { |
952 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
952 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
953 |
|
l.add(new StringTask()); |
954 |
|
l.add(new StringTask()); |
955 |
|
String result = e.invokeAny(l); |
991 |
|
*/ |
992 |
|
public void testInvokeAll3() throws Exception { |
993 |
|
ExecutorService e = new CustomExecutor(2); |
994 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
995 |
+ |
l.add(new StringTask()); |
996 |
+ |
l.add(null); |
997 |
|
try { |
798 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
799 |
– |
l.add(new StringTask()); |
800 |
– |
l.add(null); |
998 |
|
e.invokeAll(l); |
999 |
|
shouldThrow(); |
1000 |
|
} catch (NullPointerException success) { |
1008 |
|
*/ |
1009 |
|
public void testInvokeAll4() throws Exception { |
1010 |
|
ExecutorService e = new CustomExecutor(2); |
1011 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1012 |
+ |
l.add(new NPETask()); |
1013 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
1014 |
+ |
assertEquals(1, futures.size()); |
1015 |
|
try { |
1016 |
< |
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(); |
1016 |
> |
futures.get(0).get(); |
1017 |
|
shouldThrow(); |
1018 |
|
} catch (ExecutionException success) { |
1019 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1028 |
|
public void testInvokeAll5() throws Exception { |
1029 |
|
ExecutorService e = new CustomExecutor(2); |
1030 |
|
try { |
1031 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1031 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1032 |
|
l.add(new StringTask()); |
1033 |
|
l.add(new StringTask()); |
1034 |
< |
List<Future<String>> result = e.invokeAll(l); |
1035 |
< |
assertEquals(2, result.size()); |
1036 |
< |
for (Future<String> future : result) |
1034 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1035 |
> |
assertEquals(2, futures.size()); |
1036 |
> |
for (Future<String> future : futures) |
1037 |
|
assertSame(TEST_STRING, future.get()); |
1038 |
|
} finally { |
1039 |
|
joinPool(e); |
1059 |
|
*/ |
1060 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1061 |
|
ExecutorService e = new CustomExecutor(2); |
1062 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1063 |
+ |
l.add(new StringTask()); |
1064 |
|
try { |
867 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
– |
l.add(new StringTask()); |
1065 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1066 |
|
shouldThrow(); |
1067 |
|
} catch (NullPointerException success) { |
1088 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1089 |
|
*/ |
1090 |
|
public void testTimedInvokeAny3() throws Exception { |
1091 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1091 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1092 |
|
ExecutorService e = new CustomExecutor(2); |
1093 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1094 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1095 |
+ |
l.add(null); |
1096 |
|
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 ok) {} |
904 |
– |
return TEST_STRING; |
905 |
– |
}}); |
906 |
– |
l.add(null); |
1097 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1098 |
|
shouldThrow(); |
1099 |
|
} catch (NullPointerException success) { |
1108 |
|
*/ |
1109 |
|
public void testTimedInvokeAny4() throws Exception { |
1110 |
|
ExecutorService e = new CustomExecutor(2); |
1111 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1112 |
+ |
l.add(new NPETask()); |
1113 |
|
try { |
922 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
923 |
– |
l.add(new NPETask()); |
1114 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1115 |
|
shouldThrow(); |
1116 |
|
} catch (ExecutionException success) { |
1126 |
|
public void testTimedInvokeAny5() throws Exception { |
1127 |
|
ExecutorService e = new CustomExecutor(2); |
1128 |
|
try { |
1129 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1129 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
|
l.add(new StringTask()); |
1131 |
|
l.add(new StringTask()); |
1132 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1155 |
|
*/ |
1156 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1157 |
|
ExecutorService e = new CustomExecutor(2); |
1158 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1159 |
+ |
l.add(new StringTask()); |
1160 |
|
try { |
969 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
970 |
– |
l.add(new StringTask()); |
1161 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1162 |
|
shouldThrow(); |
1163 |
|
} catch (NullPointerException success) { |
1184 |
|
*/ |
1185 |
|
public void testTimedInvokeAll3() throws Exception { |
1186 |
|
ExecutorService e = new CustomExecutor(2); |
1187 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1188 |
+ |
l.add(new StringTask()); |
1189 |
+ |
l.add(null); |
1190 |
|
try { |
998 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
– |
l.add(new StringTask()); |
1000 |
– |
l.add(null); |
1191 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1192 |
|
shouldThrow(); |
1193 |
|
} catch (NullPointerException success) { |
1201 |
|
*/ |
1202 |
|
public void testTimedInvokeAll4() throws Exception { |
1203 |
|
ExecutorService e = new CustomExecutor(2); |
1204 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1205 |
+ |
l.add(new NPETask()); |
1206 |
+ |
List<Future<String>> futures = |
1207 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1208 |
+ |
assertEquals(1, futures.size()); |
1209 |
|
try { |
1210 |
< |
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(); |
1210 |
> |
futures.get(0).get(); |
1211 |
|
shouldThrow(); |
1212 |
|
} catch (ExecutionException success) { |
1213 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1222 |
|
public void testTimedInvokeAll5() throws Exception { |
1223 |
|
ExecutorService e = new CustomExecutor(2); |
1224 |
|
try { |
1225 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1225 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1226 |
|
l.add(new StringTask()); |
1227 |
|
l.add(new StringTask()); |
1228 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1229 |
< |
assertEquals(2, result.size()); |
1230 |
< |
for (Future<String> future : result) |
1228 |
> |
List<Future<String>> futures = |
1229 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1230 |
> |
assertEquals(2, futures.size()); |
1231 |
> |
for (Future<String> future : futures) |
1232 |
|
assertSame(TEST_STRING, future.get()); |
1233 |
|
} finally { |
1234 |
|
joinPool(e); |
1241 |
|
public void testTimedInvokeAll6() throws Exception { |
1242 |
|
ExecutorService e = new CustomExecutor(2); |
1243 |
|
try { |
1244 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1245 |
< |
l.add(new StringTask()); |
1246 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1247 |
< |
l.add(new StringTask()); |
1248 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1249 |
< |
assertEquals(3, result.size()); |
1250 |
< |
Iterator<Future<String>> it = result.iterator(); |
1251 |
< |
Future<String> f1 = it.next(); |
1252 |
< |
Future<String> f2 = it.next(); |
1253 |
< |
Future<String> f3 = it.next(); |
1254 |
< |
assertTrue(f1.isDone()); |
1255 |
< |
assertTrue(f2.isDone()); |
1256 |
< |
assertTrue(f3.isDone()); |
1257 |
< |
assertFalse(f1.isCancelled()); |
1258 |
< |
assertTrue(f2.isCancelled()); |
1244 |
> |
for (long timeout = timeoutMillis();;) { |
1245 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1246 |
> |
tasks.add(new StringTask("0")); |
1247 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1248 |
> |
tasks.add(new StringTask("2")); |
1249 |
> |
long startTime = System.nanoTime(); |
1250 |
> |
List<Future<String>> futures = |
1251 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1252 |
> |
assertEquals(tasks.size(), futures.size()); |
1253 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1254 |
> |
for (Future future : futures) |
1255 |
> |
assertTrue(future.isDone()); |
1256 |
> |
assertTrue(futures.get(1).isCancelled()); |
1257 |
> |
try { |
1258 |
> |
assertEquals("0", futures.get(0).get()); |
1259 |
> |
assertEquals("2", futures.get(2).get()); |
1260 |
> |
break; |
1261 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1262 |
> |
timeout *= 2; |
1263 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1264 |
> |
fail("expected exactly one task to be cancelled"); |
1265 |
> |
} |
1266 |
> |
} |
1267 |
|
} finally { |
1268 |
|
joinPool(e); |
1269 |
|
} |