author | dl |
Thu, 03 Mar 2016 10:36:08 -0800 | |
changeset 36231 | 52e0ee9847fb |
parent 35394 | 282c3cb6a0c1 |
child 40817 | 4f5fb115676d |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
35 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
35394 | 36 |
import static java.util.concurrent.TimeUnit.SECONDS; |
37 |
||
38 |
import java.util.ArrayList; |
|
39 |
import java.util.HashSet; |
|
40 |
import java.util.List; |
|
41 |
import java.util.concurrent.BlockingQueue; |
|
42 |
import java.util.concurrent.Callable; |
|
43 |
import java.util.concurrent.CancellationException; |
|
44 |
import java.util.concurrent.CountDownLatch; |
|
45 |
import java.util.concurrent.Delayed; |
|
46 |
import java.util.concurrent.ExecutionException; |
|
47 |
import java.util.concurrent.Executors; |
|
48 |
import java.util.concurrent.ExecutorService; |
|
49 |
import java.util.concurrent.Future; |
|
50 |
import java.util.concurrent.RejectedExecutionException; |
|
51 |
import java.util.concurrent.RejectedExecutionHandler; |
|
52 |
import java.util.concurrent.RunnableScheduledFuture; |
|
53 |
import java.util.concurrent.ScheduledFuture; |
|
54 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
55 |
import java.util.concurrent.ThreadFactory; |
|
56 |
import java.util.concurrent.ThreadPoolExecutor; |
|
57 |
import java.util.concurrent.TimeoutException; |
|
58 |
import java.util.concurrent.TimeUnit; |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
59 |
import java.util.concurrent.atomic.AtomicBoolean; |
35394 | 60 |
import java.util.concurrent.atomic.AtomicInteger; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
61 |
import java.util.concurrent.atomic.AtomicLong; |
35394 | 62 |
|
63 |
import junit.framework.Test; |
|
64 |
import junit.framework.TestSuite; |
|
65 |
||
66 |
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
|
67 |
public static void main(String[] args) { |
|
68 |
main(suite(), args); |
|
69 |
} |
|
70 |
public static Test suite() { |
|
71 |
return new TestSuite(ScheduledExecutorSubclassTest.class); |
|
72 |
} |
|
73 |
||
74 |
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
|
75 |
RunnableScheduledFuture<V> task; |
|
76 |
volatile boolean ran; |
|
77 |
CustomTask(RunnableScheduledFuture<V> t) { task = t; } |
|
78 |
public boolean isPeriodic() { return task.isPeriodic(); } |
|
79 |
public void run() { |
|
80 |
ran = true; |
|
81 |
task.run(); |
|
82 |
} |
|
83 |
public long getDelay(TimeUnit unit) { return task.getDelay(unit); } |
|
84 |
public int compareTo(Delayed t) { |
|
85 |
return task.compareTo(((CustomTask)t).task); |
|
86 |
} |
|
87 |
public boolean cancel(boolean mayInterruptIfRunning) { |
|
88 |
return task.cancel(mayInterruptIfRunning); |
|
89 |
} |
|
90 |
public boolean isCancelled() { return task.isCancelled(); } |
|
91 |
public boolean isDone() { return task.isDone(); } |
|
92 |
public V get() throws InterruptedException, ExecutionException { |
|
93 |
V v = task.get(); |
|
94 |
assertTrue(ran); |
|
95 |
return v; |
|
96 |
} |
|
97 |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
|
98 |
V v = task.get(time, unit); |
|
99 |
assertTrue(ran); |
|
100 |
return v; |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
|
105 |
||
106 |
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
|
107 |
return new CustomTask<V>(task); |
|
108 |
} |
|
109 |
||
110 |
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) { |
|
111 |
return new CustomTask<V>(task); |
|
112 |
} |
|
113 |
CustomExecutor(int corePoolSize) { super(corePoolSize); } |
|
114 |
CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { |
|
115 |
super(corePoolSize, handler); |
|
116 |
} |
|
117 |
||
118 |
CustomExecutor(int corePoolSize, ThreadFactory threadFactory) { |
|
119 |
super(corePoolSize, threadFactory); |
|
120 |
} |
|
121 |
CustomExecutor(int corePoolSize, ThreadFactory threadFactory, |
|
122 |
RejectedExecutionHandler handler) { |
|
123 |
super(corePoolSize, threadFactory, handler); |
|
124 |
} |
|
125 |
||
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* execute successfully executes a runnable |
|
130 |
*/ |
|
131 |
public void testExecute() throws InterruptedException { |
|
132 |
final CustomExecutor p = new CustomExecutor(1); |
|
133 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
134 |
final CountDownLatch done = new CountDownLatch(1); |
|
135 |
final Runnable task = new CheckedRunnable() { |
|
136 |
public void realRun() { done.countDown(); }}; |
|
137 |
p.execute(task); |
|
138 |
await(done); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* delayed schedule of callable successfully executes after delay |
|
144 |
*/ |
|
145 |
public void testSchedule1() throws Exception { |
|
146 |
final CountDownLatch done = new CountDownLatch(1); |
|
147 |
final CustomExecutor p = new CustomExecutor(1); |
|
148 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
149 |
final long startTime = System.nanoTime(); |
|
150 |
Callable task = new CheckedCallable<Boolean>() { |
|
151 |
public Boolean realCall() { |
|
152 |
done.countDown(); |
|
153 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
154 |
return Boolean.TRUE; |
|
155 |
}}; |
|
156 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
|
157 |
assertSame(Boolean.TRUE, f.get()); |
|
158 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* delayed schedule of runnable successfully executes after delay |
|
164 |
*/ |
|
165 |
public void testSchedule3() throws Exception { |
|
166 |
final CustomExecutor p = new CustomExecutor(1); |
|
167 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
168 |
final long startTime = System.nanoTime(); |
|
169 |
final CountDownLatch done = new CountDownLatch(1); |
|
170 |
Runnable task = new CheckedRunnable() { |
|
171 |
public void realRun() { |
|
172 |
done.countDown(); |
|
173 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
174 |
}}; |
|
175 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
|
176 |
await(done); |
|
177 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
|
178 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* scheduleAtFixedRate executes runnable after given initial delay |
|
184 |
*/ |
|
185 |
public void testSchedule4() throws InterruptedException { |
|
186 |
final CustomExecutor p = new CustomExecutor(1); |
|
187 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
188 |
final long startTime = System.nanoTime(); |
|
189 |
final CountDownLatch done = new CountDownLatch(1); |
|
190 |
Runnable task = new CheckedRunnable() { |
|
191 |
public void realRun() { |
|
192 |
done.countDown(); |
|
193 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
194 |
}}; |
|
195 |
ScheduledFuture f = |
|
196 |
p.scheduleAtFixedRate(task, timeoutMillis(), |
|
197 |
LONG_DELAY_MS, MILLISECONDS); |
|
198 |
await(done); |
|
199 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
200 |
f.cancel(true); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* scheduleWithFixedDelay executes runnable after given initial delay |
|
206 |
*/ |
|
207 |
public void testSchedule5() throws InterruptedException { |
|
208 |
final CustomExecutor p = new CustomExecutor(1); |
|
209 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
210 |
final long startTime = System.nanoTime(); |
|
211 |
final CountDownLatch done = new CountDownLatch(1); |
|
212 |
Runnable task = new CheckedRunnable() { |
|
213 |
public void realRun() { |
|
214 |
done.countDown(); |
|
215 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
216 |
}}; |
|
217 |
ScheduledFuture f = |
|
218 |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
|
219 |
LONG_DELAY_MS, MILLISECONDS); |
|
220 |
await(done); |
|
221 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
222 |
f.cancel(true); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
static class RunnableCounter implements Runnable { |
|
227 |
AtomicInteger count = new AtomicInteger(0); |
|
228 |
public void run() { count.getAndIncrement(); } |
|
229 |
} |
|
230 |
||
231 |
/** |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
232 |
* scheduleAtFixedRate executes series of tasks at given rate. |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
233 |
* Eventually, it must hold that: |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
234 |
* cycles - 1 <= elapsedMillis/delay < cycles |
35394 | 235 |
*/ |
236 |
public void testFixedRateSequence() throws InterruptedException { |
|
237 |
final CustomExecutor p = new CustomExecutor(1); |
|
238 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
239 |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
240 |
final long startTime = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
241 |
final int cycles = 8; |
35394 | 242 |
final CountDownLatch done = new CountDownLatch(cycles); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
243 |
final Runnable task = new CheckedRunnable() { |
35394 | 244 |
public void realRun() { done.countDown(); }}; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
245 |
final ScheduledFuture periodicTask = |
35394 | 246 |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
247 |
final int totalDelayMillis = (cycles - 1) * delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
248 |
await(done, totalDelayMillis + LONG_DELAY_MS); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
249 |
periodicTask.cancel(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
250 |
final long elapsedMillis = millisElapsedSince(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
251 |
assertTrue(elapsedMillis >= totalDelayMillis); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
252 |
if (elapsedMillis <= cycles * delay) |
35394 | 253 |
return; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
254 |
// else retry with longer delay |
35394 | 255 |
} |
256 |
fail("unexpected execution rate"); |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
/** |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
261 |
* scheduleWithFixedDelay executes series of tasks with given period. |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
262 |
* Eventually, it must hold that each task starts at least delay and at |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
263 |
* most 2 * delay after the termination of the previous task. |
35394 | 264 |
*/ |
265 |
public void testFixedDelaySequence() throws InterruptedException { |
|
266 |
final CustomExecutor p = new CustomExecutor(1); |
|
267 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
268 |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
269 |
final long startTime = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
270 |
final AtomicLong previous = new AtomicLong(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
271 |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
272 |
final int cycles = 8; |
35394 | 273 |
final CountDownLatch done = new CountDownLatch(cycles); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
274 |
final int d = delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
275 |
final Runnable task = new CheckedRunnable() { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
276 |
public void realRun() { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
277 |
long now = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
278 |
long elapsedMillis |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
279 |
= NANOSECONDS.toMillis(now - previous.get()); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
280 |
if (done.getCount() == cycles) { // first execution |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
281 |
if (elapsedMillis >= d) |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
282 |
tryLongerDelay.set(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
283 |
} else { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
284 |
assertTrue(elapsedMillis >= d); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
285 |
if (elapsedMillis >= 2 * d) |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
286 |
tryLongerDelay.set(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
287 |
} |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
288 |
previous.set(now); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
289 |
done.countDown(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
290 |
}}; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
291 |
final ScheduledFuture periodicTask = |
35394 | 292 |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
293 |
final int totalDelayMillis = (cycles - 1) * delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
294 |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
295 |
periodicTask.cancel(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
296 |
final long elapsedMillis = millisElapsedSince(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
297 |
assertTrue(elapsedMillis >= totalDelayMillis); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
298 |
if (!tryLongerDelay.get()) |
35394 | 299 |
return; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
300 |
// else retry with longer delay |
35394 | 301 |
} |
302 |
fail("unexpected execution rate"); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* execute(null) throws NPE |
|
308 |
*/ |
|
309 |
public void testExecuteNull() throws InterruptedException { |
|
310 |
final CustomExecutor p = new CustomExecutor(1); |
|
311 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
312 |
try { |
|
313 |
p.execute(null); |
|
314 |
shouldThrow(); |
|
315 |
} catch (NullPointerException success) {} |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* schedule(null) throws NPE |
|
321 |
*/ |
|
322 |
public void testScheduleNull() throws InterruptedException { |
|
323 |
final CustomExecutor p = new CustomExecutor(1); |
|
324 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
325 |
try { |
|
326 |
TrackedCallable callable = null; |
|
327 |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
|
328 |
shouldThrow(); |
|
329 |
} catch (NullPointerException success) {} |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* execute throws RejectedExecutionException if shutdown |
|
335 |
*/ |
|
336 |
public void testSchedule1_RejectedExecutionException() { |
|
337 |
final CustomExecutor p = new CustomExecutor(1); |
|
338 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
339 |
try { |
|
340 |
p.shutdown(); |
|
341 |
p.schedule(new NoOpRunnable(), |
|
342 |
MEDIUM_DELAY_MS, MILLISECONDS); |
|
343 |
shouldThrow(); |
|
344 |
} catch (RejectedExecutionException success) { |
|
345 |
} catch (SecurityException ok) {} |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* schedule throws RejectedExecutionException if shutdown |
|
351 |
*/ |
|
352 |
public void testSchedule2_RejectedExecutionException() { |
|
353 |
final CustomExecutor p = new CustomExecutor(1); |
|
354 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
355 |
try { |
|
356 |
p.shutdown(); |
|
357 |
p.schedule(new NoOpCallable(), |
|
358 |
MEDIUM_DELAY_MS, MILLISECONDS); |
|
359 |
shouldThrow(); |
|
360 |
} catch (RejectedExecutionException success) { |
|
361 |
} catch (SecurityException ok) {} |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* schedule callable throws RejectedExecutionException if shutdown |
|
367 |
*/ |
|
368 |
public void testSchedule3_RejectedExecutionException() { |
|
369 |
final CustomExecutor p = new CustomExecutor(1); |
|
370 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
371 |
try { |
|
372 |
p.shutdown(); |
|
373 |
p.schedule(new NoOpCallable(), |
|
374 |
MEDIUM_DELAY_MS, MILLISECONDS); |
|
375 |
shouldThrow(); |
|
376 |
} catch (RejectedExecutionException success) { |
|
377 |
} catch (SecurityException ok) {} |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
|
383 |
*/ |
|
384 |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
|
385 |
final CustomExecutor p = new CustomExecutor(1); |
|
386 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
387 |
try { |
|
388 |
p.shutdown(); |
|
389 |
p.scheduleAtFixedRate(new NoOpRunnable(), |
|
390 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
|
391 |
shouldThrow(); |
|
392 |
} catch (RejectedExecutionException success) { |
|
393 |
} catch (SecurityException ok) {} |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
|
399 |
*/ |
|
400 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
|
401 |
final CustomExecutor p = new CustomExecutor(1); |
|
402 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
403 |
try { |
|
404 |
p.shutdown(); |
|
405 |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
|
406 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
|
407 |
shouldThrow(); |
|
408 |
} catch (RejectedExecutionException success) { |
|
409 |
} catch (SecurityException ok) {} |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
/** |
|
414 |
* getActiveCount increases but doesn't overestimate, when a |
|
415 |
* thread becomes active |
|
416 |
*/ |
|
417 |
public void testGetActiveCount() throws InterruptedException { |
|
418 |
final CountDownLatch done = new CountDownLatch(1); |
|
419 |
final ThreadPoolExecutor p = new CustomExecutor(2); |
|
420 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
421 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
422 |
assertEquals(0, p.getActiveCount()); |
|
423 |
p.execute(new CheckedRunnable() { |
|
424 |
public void realRun() throws InterruptedException { |
|
425 |
threadStarted.countDown(); |
|
426 |
assertEquals(1, p.getActiveCount()); |
|
427 |
await(done); |
|
428 |
}}); |
|
429 |
await(threadStarted); |
|
430 |
assertEquals(1, p.getActiveCount()); |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
/** |
|
435 |
* getCompletedTaskCount increases, but doesn't overestimate, |
|
436 |
* when tasks complete |
|
437 |
*/ |
|
438 |
public void testGetCompletedTaskCount() throws InterruptedException { |
|
439 |
final ThreadPoolExecutor p = new CustomExecutor(2); |
|
440 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
441 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
442 |
final CountDownLatch threadProceed = new CountDownLatch(1); |
|
443 |
final CountDownLatch threadDone = new CountDownLatch(1); |
|
444 |
assertEquals(0, p.getCompletedTaskCount()); |
|
445 |
p.execute(new CheckedRunnable() { |
|
446 |
public void realRun() throws InterruptedException { |
|
447 |
threadStarted.countDown(); |
|
448 |
assertEquals(0, p.getCompletedTaskCount()); |
|
449 |
threadProceed.await(); |
|
450 |
threadDone.countDown(); |
|
451 |
}}); |
|
452 |
await(threadStarted); |
|
453 |
assertEquals(0, p.getCompletedTaskCount()); |
|
454 |
threadProceed.countDown(); |
|
455 |
threadDone.await(); |
|
456 |
long startTime = System.nanoTime(); |
|
457 |
while (p.getCompletedTaskCount() != 1) { |
|
458 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
|
459 |
fail("timed out"); |
|
460 |
Thread.yield(); |
|
461 |
} |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* getCorePoolSize returns size given in constructor if not otherwise set |
|
467 |
*/ |
|
468 |
public void testGetCorePoolSize() { |
|
469 |
final CustomExecutor p = new CustomExecutor(1); |
|
470 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
471 |
assertEquals(1, p.getCorePoolSize()); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* getLargestPoolSize increases, but doesn't overestimate, when |
|
477 |
* multiple threads active |
|
478 |
*/ |
|
479 |
public void testGetLargestPoolSize() throws InterruptedException { |
|
480 |
final int THREADS = 3; |
|
481 |
final CountDownLatch done = new CountDownLatch(1); |
|
482 |
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
|
483 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
484 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
|
485 |
assertEquals(0, p.getLargestPoolSize()); |
|
486 |
for (int i = 0; i < THREADS; i++) |
|
487 |
p.execute(new CheckedRunnable() { |
|
488 |
public void realRun() throws InterruptedException { |
|
489 |
threadsStarted.countDown(); |
|
490 |
await(done); |
|
491 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
492 |
}}); |
|
493 |
await(threadsStarted); |
|
494 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
495 |
} |
|
496 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* getPoolSize increases, but doesn't overestimate, when threads |
|
501 |
* become active |
|
502 |
*/ |
|
503 |
public void testGetPoolSize() throws InterruptedException { |
|
504 |
final CountDownLatch done = new CountDownLatch(1); |
|
505 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
|
506 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
507 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
508 |
assertEquals(0, p.getPoolSize()); |
|
509 |
p.execute(new CheckedRunnable() { |
|
510 |
public void realRun() throws InterruptedException { |
|
511 |
threadStarted.countDown(); |
|
512 |
assertEquals(1, p.getPoolSize()); |
|
513 |
await(done); |
|
514 |
}}); |
|
515 |
await(threadStarted); |
|
516 |
assertEquals(1, p.getPoolSize()); |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
/** |
|
521 |
* getTaskCount increases, but doesn't overestimate, when tasks |
|
522 |
* submitted |
|
523 |
*/ |
|
524 |
public void testGetTaskCount() throws InterruptedException { |
|
525 |
final int TASKS = 3; |
|
526 |
final CountDownLatch done = new CountDownLatch(1); |
|
527 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
|
528 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
529 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
530 |
assertEquals(0, p.getTaskCount()); |
|
531 |
assertEquals(0, p.getCompletedTaskCount()); |
|
532 |
p.execute(new CheckedRunnable() { |
|
533 |
public void realRun() throws InterruptedException { |
|
534 |
threadStarted.countDown(); |
|
535 |
await(done); |
|
536 |
}}); |
|
537 |
await(threadStarted); |
|
538 |
assertEquals(1, p.getTaskCount()); |
|
539 |
assertEquals(0, p.getCompletedTaskCount()); |
|
540 |
for (int i = 0; i < TASKS; i++) { |
|
541 |
assertEquals(1 + i, p.getTaskCount()); |
|
542 |
p.execute(new CheckedRunnable() { |
|
543 |
public void realRun() throws InterruptedException { |
|
544 |
threadStarted.countDown(); |
|
545 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
546 |
await(done); |
|
547 |
}}); |
|
548 |
} |
|
549 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
550 |
assertEquals(0, p.getCompletedTaskCount()); |
|
551 |
} |
|
552 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
553 |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
|
554 |
} |
|
555 |
||
556 |
/** |
|
557 |
* getThreadFactory returns factory in constructor if not set |
|
558 |
*/ |
|
559 |
public void testGetThreadFactory() { |
|
560 |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
|
561 |
final CustomExecutor p = new CustomExecutor(1, threadFactory); |
|
562 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
563 |
assertSame(threadFactory, p.getThreadFactory()); |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
/** |
|
568 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
|
569 |
*/ |
|
570 |
public void testSetThreadFactory() { |
|
571 |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
|
572 |
final CustomExecutor p = new CustomExecutor(1); |
|
573 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
574 |
p.setThreadFactory(threadFactory); |
|
575 |
assertSame(threadFactory, p.getThreadFactory()); |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* setThreadFactory(null) throws NPE |
|
581 |
*/ |
|
582 |
public void testSetThreadFactoryNull() { |
|
583 |
final CustomExecutor p = new CustomExecutor(1); |
|
584 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
585 |
try { |
|
586 |
p.setThreadFactory(null); |
|
587 |
shouldThrow(); |
|
588 |
} catch (NullPointerException success) {} |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
/** |
|
593 |
* isShutdown is false before shutdown, true after |
|
594 |
*/ |
|
595 |
public void testIsShutdown() { |
|
596 |
final CustomExecutor p = new CustomExecutor(1); |
|
597 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
598 |
assertFalse(p.isShutdown()); |
|
599 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
600 |
assertTrue(p.isShutdown()); |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* isTerminated is false before termination, true after |
|
606 |
*/ |
|
607 |
public void testIsTerminated() throws InterruptedException { |
|
608 |
final CountDownLatch done = new CountDownLatch(1); |
|
609 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
|
610 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
611 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
612 |
p.execute(new CheckedRunnable() { |
|
613 |
public void realRun() throws InterruptedException { |
|
614 |
assertFalse(p.isTerminated()); |
|
615 |
threadStarted.countDown(); |
|
616 |
await(done); |
|
617 |
}}); |
|
618 |
await(threadStarted); |
|
619 |
assertFalse(p.isTerminated()); |
|
620 |
assertFalse(p.isTerminating()); |
|
621 |
done.countDown(); |
|
622 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
623 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
624 |
assertTrue(p.isTerminated()); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* isTerminating is not true when running or when terminated |
|
630 |
*/ |
|
631 |
public void testIsTerminating() throws InterruptedException { |
|
632 |
final CountDownLatch done = new CountDownLatch(1); |
|
633 |
final ThreadPoolExecutor p = new CustomExecutor(1); |
|
634 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
635 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
636 |
assertFalse(p.isTerminating()); |
|
637 |
p.execute(new CheckedRunnable() { |
|
638 |
public void realRun() throws InterruptedException { |
|
639 |
assertFalse(p.isTerminating()); |
|
640 |
threadStarted.countDown(); |
|
641 |
await(done); |
|
642 |
}}); |
|
643 |
await(threadStarted); |
|
644 |
assertFalse(p.isTerminating()); |
|
645 |
done.countDown(); |
|
646 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
647 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
648 |
assertTrue(p.isTerminated()); |
|
649 |
assertFalse(p.isTerminating()); |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* getQueue returns the work queue, which contains queued tasks |
|
655 |
*/ |
|
656 |
public void testGetQueue() throws InterruptedException { |
|
657 |
final CountDownLatch done = new CountDownLatch(1); |
|
658 |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
|
659 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
660 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
661 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
662 |
for (int i = 0; i < tasks.length; i++) { |
|
663 |
Runnable r = new CheckedRunnable() { |
|
664 |
public void realRun() throws InterruptedException { |
|
665 |
threadStarted.countDown(); |
|
666 |
await(done); |
|
667 |
}}; |
|
668 |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
|
669 |
} |
|
670 |
await(threadStarted); |
|
671 |
BlockingQueue<Runnable> q = p.getQueue(); |
|
672 |
assertTrue(q.contains(tasks[tasks.length - 1])); |
|
673 |
assertFalse(q.contains(tasks[0])); |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
/** |
|
678 |
* remove(task) removes queued task, and fails to remove active task |
|
679 |
*/ |
|
680 |
public void testRemove() throws InterruptedException { |
|
681 |
final CountDownLatch done = new CountDownLatch(1); |
|
682 |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
|
683 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
684 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
685 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
686 |
for (int i = 0; i < tasks.length; i++) { |
|
687 |
Runnable r = new CheckedRunnable() { |
|
688 |
public void realRun() throws InterruptedException { |
|
689 |
threadStarted.countDown(); |
|
690 |
await(done); |
|
691 |
}}; |
|
692 |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
|
693 |
} |
|
694 |
await(threadStarted); |
|
695 |
BlockingQueue<Runnable> q = p.getQueue(); |
|
696 |
assertFalse(p.remove((Runnable)tasks[0])); |
|
697 |
assertTrue(q.contains((Runnable)tasks[4])); |
|
698 |
assertTrue(q.contains((Runnable)tasks[3])); |
|
699 |
assertTrue(p.remove((Runnable)tasks[4])); |
|
700 |
assertFalse(p.remove((Runnable)tasks[4])); |
|
701 |
assertFalse(q.contains((Runnable)tasks[4])); |
|
702 |
assertTrue(q.contains((Runnable)tasks[3])); |
|
703 |
assertTrue(p.remove((Runnable)tasks[3])); |
|
704 |
assertFalse(q.contains((Runnable)tasks[3])); |
|
705 |
} |
|
706 |
} |
|
707 |
||
708 |
/** |
|
709 |
* purge removes cancelled tasks from the queue |
|
710 |
*/ |
|
711 |
public void testPurge() throws InterruptedException { |
|
712 |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
713 |
final Runnable releaser = new Runnable() { public void run() { |
|
714 |
for (ScheduledFuture task : tasks) |
|
715 |
if (task != null) task.cancel(true); }}; |
|
716 |
final CustomExecutor p = new CustomExecutor(1); |
|
717 |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
|
718 |
for (int i = 0; i < tasks.length; i++) |
|
719 |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
|
720 |
LONG_DELAY_MS, MILLISECONDS); |
|
721 |
int max = tasks.length; |
|
722 |
if (tasks[4].cancel(true)) --max; |
|
723 |
if (tasks[3].cancel(true)) --max; |
|
724 |
// There must eventually be an interference-free point at |
|
725 |
// which purge will not fail. (At worst, when queue is empty.) |
|
726 |
long startTime = System.nanoTime(); |
|
727 |
do { |
|
728 |
p.purge(); |
|
729 |
long count = p.getTaskCount(); |
|
730 |
if (count == max) |
|
731 |
return; |
|
732 |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
733 |
fail("Purge failed to remove cancelled tasks"); |
|
734 |
} |
|
735 |
} |
|
736 |
||
737 |
/** |
|
738 |
* shutdownNow returns a list containing tasks that were not run, |
|
739 |
* and those tasks are drained from the queue |
|
740 |
*/ |
|
741 |
public void testShutdownNow() throws InterruptedException { |
|
742 |
final int poolSize = 2; |
|
743 |
final int count = 5; |
|
744 |
final AtomicInteger ran = new AtomicInteger(0); |
|
745 |
final CustomExecutor p = new CustomExecutor(poolSize); |
|
746 |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
|
747 |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
|
748 |
threadsStarted.countDown(); |
|
749 |
try { |
|
750 |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
|
751 |
} catch (InterruptedException success) {} |
|
752 |
ran.getAndIncrement(); |
|
753 |
}}; |
|
754 |
for (int i = 0; i < count; i++) |
|
755 |
p.execute(waiter); |
|
756 |
await(threadsStarted); |
|
757 |
assertEquals(poolSize, p.getActiveCount()); |
|
758 |
assertEquals(0, p.getCompletedTaskCount()); |
|
759 |
final List<Runnable> queuedTasks; |
|
760 |
try { |
|
761 |
queuedTasks = p.shutdownNow(); |
|
762 |
} catch (SecurityException ok) { |
|
763 |
return; // Allowed in case test doesn't have privs |
|
764 |
} |
|
765 |
assertTrue(p.isShutdown()); |
|
766 |
assertTrue(p.getQueue().isEmpty()); |
|
767 |
assertEquals(count - poolSize, queuedTasks.size()); |
|
768 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
769 |
assertTrue(p.isTerminated()); |
|
770 |
assertEquals(poolSize, ran.get()); |
|
771 |
assertEquals(poolSize, p.getCompletedTaskCount()); |
|
772 |
} |
|
773 |
||
774 |
/** |
|
775 |
* shutdownNow returns a list containing tasks that were not run, |
|
776 |
* and those tasks are drained from the queue |
|
777 |
*/ |
|
778 |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
|
779 |
final CustomExecutor p = new CustomExecutor(1); |
|
780 |
List<ScheduledFuture> tasks = new ArrayList<>(); |
|
781 |
for (int i = 0; i < 3; i++) { |
|
782 |
Runnable r = new NoOpRunnable(); |
|
783 |
tasks.add(p.schedule(r, 9, SECONDS)); |
|
784 |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
|
785 |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
|
786 |
} |
|
787 |
if (testImplementationDetails) |
|
788 |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
|
789 |
final List<Runnable> queuedTasks; |
|
790 |
try { |
|
791 |
queuedTasks = p.shutdownNow(); |
|
792 |
} catch (SecurityException ok) { |
|
793 |
return; // Allowed in case test doesn't have privs |
|
794 |
} |
|
795 |
assertTrue(p.isShutdown()); |
|
796 |
assertTrue(p.getQueue().isEmpty()); |
|
797 |
if (testImplementationDetails) |
|
798 |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
|
799 |
assertEquals(tasks.size(), queuedTasks.size()); |
|
800 |
for (ScheduledFuture task : tasks) { |
|
801 |
assertFalse(((CustomTask)task).ran); |
|
802 |
assertFalse(task.isDone()); |
|
803 |
assertFalse(task.isCancelled()); |
|
804 |
} |
|
805 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
806 |
assertTrue(p.isTerminated()); |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* By default, periodic tasks are cancelled at shutdown. |
|
811 |
* By default, delayed tasks keep running after shutdown. |
|
812 |
* Check that changing the default values work: |
|
813 |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
|
814 |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
|
815 |
*/ |
|
816 |
public void testShutdown_cancellation() throws Exception { |
|
817 |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
|
818 |
for (Boolean policy : allBooleans) |
|
819 |
{ |
|
820 |
final int poolSize = 2; |
|
821 |
final CustomExecutor p = new CustomExecutor(poolSize); |
|
822 |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
|
823 |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
|
824 |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
|
825 |
if (policy != null) { |
|
826 |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
|
827 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
|
828 |
p.setRemoveOnCancelPolicy(policy); |
|
829 |
} |
|
830 |
assertEquals(effectiveDelayedPolicy, |
|
831 |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
|
832 |
assertEquals(effectivePeriodicPolicy, |
|
833 |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
|
834 |
assertEquals(effectiveRemovePolicy, |
|
835 |
p.getRemoveOnCancelPolicy()); |
|
836 |
// Strategy: Wedge the pool with poolSize "blocker" threads |
|
837 |
final AtomicInteger ran = new AtomicInteger(0); |
|
838 |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
|
839 |
final CountDownLatch unblock = new CountDownLatch(1); |
|
840 |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
|
841 |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
|
842 |
Runnable task = new CheckedRunnable() { public void realRun() |
|
843 |
throws InterruptedException { |
|
844 |
poolBlocked.countDown(); |
|
845 |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
|
846 |
ran.getAndIncrement(); |
|
847 |
}}; |
|
848 |
List<Future<?>> blockers = new ArrayList<>(); |
|
849 |
List<Future<?>> periodics = new ArrayList<>(); |
|
850 |
List<Future<?>> delayeds = new ArrayList<>(); |
|
851 |
for (int i = 0; i < poolSize; i++) |
|
852 |
blockers.add(p.submit(task)); |
|
853 |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
|
854 |
||
855 |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
|
856 |
1, 1, MILLISECONDS)); |
|
857 |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
|
858 |
1, 1, MILLISECONDS)); |
|
859 |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
|
860 |
||
861 |
assertTrue(p.getQueue().containsAll(periodics)); |
|
862 |
assertTrue(p.getQueue().containsAll(delayeds)); |
|
863 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
864 |
assertTrue(p.isShutdown()); |
|
865 |
assertFalse(p.isTerminated()); |
|
866 |
for (Future<?> periodic : periodics) { |
|
867 |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
|
868 |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
|
869 |
} |
|
870 |
for (Future<?> delayed : delayeds) { |
|
871 |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
|
872 |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
|
873 |
} |
|
874 |
if (testImplementationDetails) { |
|
875 |
assertEquals(effectivePeriodicPolicy, |
|
876 |
p.getQueue().containsAll(periodics)); |
|
877 |
assertEquals(effectiveDelayedPolicy, |
|
878 |
p.getQueue().containsAll(delayeds)); |
|
879 |
} |
|
880 |
// Release all pool threads |
|
881 |
unblock.countDown(); |
|
882 |
||
883 |
for (Future<?> delayed : delayeds) { |
|
884 |
if (effectiveDelayedPolicy) { |
|
885 |
assertNull(delayed.get()); |
|
886 |
} |
|
887 |
} |
|
888 |
if (effectivePeriodicPolicy) { |
|
889 |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
|
890 |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
|
891 |
for (Future<?> periodic : periodics) { |
|
892 |
assertTrue(periodic.cancel(false)); |
|
893 |
assertTrue(periodic.isCancelled()); |
|
894 |
assertTrue(periodic.isDone()); |
|
895 |
} |
|
896 |
} |
|
897 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
898 |
assertTrue(p.isTerminated()); |
|
899 |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
|
900 |
}} |
|
901 |
||
902 |
/** |
|
903 |
* completed submit of callable returns result |
|
904 |
*/ |
|
905 |
public void testSubmitCallable() throws Exception { |
|
906 |
final ExecutorService e = new CustomExecutor(2); |
|
907 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
908 |
Future<String> future = e.submit(new StringTask()); |
|
909 |
String result = future.get(); |
|
910 |
assertSame(TEST_STRING, result); |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
/** |
|
915 |
* completed submit of runnable returns successfully |
|
916 |
*/ |
|
917 |
public void testSubmitRunnable() throws Exception { |
|
918 |
final ExecutorService e = new CustomExecutor(2); |
|
919 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
920 |
Future<?> future = e.submit(new NoOpRunnable()); |
|
921 |
future.get(); |
|
922 |
assertTrue(future.isDone()); |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
/** |
|
927 |
* completed submit of (runnable, result) returns result |
|
928 |
*/ |
|
929 |
public void testSubmitRunnable2() throws Exception { |
|
930 |
final ExecutorService e = new CustomExecutor(2); |
|
931 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
932 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
|
933 |
String result = future.get(); |
|
934 |
assertSame(TEST_STRING, result); |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
/** |
|
939 |
* invokeAny(null) throws NPE |
|
940 |
*/ |
|
941 |
public void testInvokeAny1() throws Exception { |
|
942 |
final ExecutorService e = new CustomExecutor(2); |
|
943 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
944 |
try { |
|
945 |
e.invokeAny(null); |
|
946 |
shouldThrow(); |
|
947 |
} catch (NullPointerException success) {} |
|
948 |
} |
|
949 |
} |
|
950 |
||
951 |
/** |
|
952 |
* invokeAny(empty collection) throws IAE |
|
953 |
*/ |
|
954 |
public void testInvokeAny2() throws Exception { |
|
955 |
final ExecutorService e = new CustomExecutor(2); |
|
956 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
957 |
try { |
|
958 |
e.invokeAny(new ArrayList<Callable<String>>()); |
|
959 |
shouldThrow(); |
|
960 |
} catch (IllegalArgumentException success) {} |
|
961 |
} |
|
962 |
} |
|
963 |
||
964 |
/** |
|
965 |
* invokeAny(c) throws NPE if c has null elements |
|
966 |
*/ |
|
967 |
public void testInvokeAny3() throws Exception { |
|
968 |
final CountDownLatch latch = new CountDownLatch(1); |
|
969 |
final ExecutorService e = new CustomExecutor(2); |
|
970 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
971 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
972 |
l.add(latchAwaitingStringTask(latch)); |
|
973 |
l.add(null); |
|
974 |
try { |
|
975 |
e.invokeAny(l); |
|
976 |
shouldThrow(); |
|
977 |
} catch (NullPointerException success) {} |
|
978 |
latch.countDown(); |
|
979 |
} |
|
980 |
} |
|
981 |
||
982 |
/** |
|
983 |
* invokeAny(c) throws ExecutionException if no task completes |
|
984 |
*/ |
|
985 |
public void testInvokeAny4() throws Exception { |
|
986 |
final ExecutorService e = new CustomExecutor(2); |
|
987 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
988 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
989 |
l.add(new NPETask()); |
|
990 |
try { |
|
991 |
e.invokeAny(l); |
|
992 |
shouldThrow(); |
|
993 |
} catch (ExecutionException success) { |
|
994 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
995 |
} |
|
996 |
} |
|
997 |
} |
|
998 |
||
999 |
/** |
|
1000 |
* invokeAny(c) returns result of some task |
|
1001 |
*/ |
|
1002 |
public void testInvokeAny5() throws Exception { |
|
1003 |
final ExecutorService e = new CustomExecutor(2); |
|
1004 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1005 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1006 |
l.add(new StringTask()); |
|
1007 |
l.add(new StringTask()); |
|
1008 |
String result = e.invokeAny(l); |
|
1009 |
assertSame(TEST_STRING, result); |
|
1010 |
} |
|
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* invokeAll(null) throws NPE |
|
1015 |
*/ |
|
1016 |
public void testInvokeAll1() throws Exception { |
|
1017 |
final ExecutorService e = new CustomExecutor(2); |
|
1018 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1019 |
try { |
|
1020 |
e.invokeAll(null); |
|
1021 |
shouldThrow(); |
|
1022 |
} catch (NullPointerException success) {} |
|
1023 |
} |
|
1024 |
} |
|
1025 |
||
1026 |
/** |
|
1027 |
* invokeAll(empty collection) returns empty collection |
|
1028 |
*/ |
|
1029 |
public void testInvokeAll2() throws Exception { |
|
1030 |
final ExecutorService e = new CustomExecutor(2); |
|
1031 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1032 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
|
1033 |
assertTrue(r.isEmpty()); |
|
1034 |
} |
|
1035 |
} |
|
1036 |
||
1037 |
/** |
|
1038 |
* invokeAll(c) throws NPE if c has null elements |
|
1039 |
*/ |
|
1040 |
public void testInvokeAll3() throws Exception { |
|
1041 |
final ExecutorService e = new CustomExecutor(2); |
|
1042 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1043 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1044 |
l.add(new StringTask()); |
|
1045 |
l.add(null); |
|
1046 |
try { |
|
1047 |
e.invokeAll(l); |
|
1048 |
shouldThrow(); |
|
1049 |
} catch (NullPointerException success) {} |
|
1050 |
} |
|
1051 |
} |
|
1052 |
||
1053 |
/** |
|
1054 |
* get of invokeAll(c) throws exception on failed task |
|
1055 |
*/ |
|
1056 |
public void testInvokeAll4() throws Exception { |
|
1057 |
final ExecutorService e = new CustomExecutor(2); |
|
1058 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1059 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1060 |
l.add(new NPETask()); |
|
1061 |
List<Future<String>> futures = e.invokeAll(l); |
|
1062 |
assertEquals(1, futures.size()); |
|
1063 |
try { |
|
1064 |
futures.get(0).get(); |
|
1065 |
shouldThrow(); |
|
1066 |
} catch (ExecutionException success) { |
|
1067 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1068 |
} |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
1073 |
* invokeAll(c) returns results of all completed tasks |
|
1074 |
*/ |
|
1075 |
public void testInvokeAll5() throws Exception { |
|
1076 |
final ExecutorService e = new CustomExecutor(2); |
|
1077 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1078 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1079 |
l.add(new StringTask()); |
|
1080 |
l.add(new StringTask()); |
|
1081 |
List<Future<String>> futures = e.invokeAll(l); |
|
1082 |
assertEquals(2, futures.size()); |
|
1083 |
for (Future<String> future : futures) |
|
1084 |
assertSame(TEST_STRING, future.get()); |
|
1085 |
} |
|
1086 |
} |
|
1087 |
||
1088 |
/** |
|
1089 |
* timed invokeAny(null) throws NPE |
|
1090 |
*/ |
|
1091 |
public void testTimedInvokeAny1() throws Exception { |
|
1092 |
final ExecutorService e = new CustomExecutor(2); |
|
1093 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1094 |
try { |
|
1095 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
|
1096 |
shouldThrow(); |
|
1097 |
} catch (NullPointerException success) {} |
|
1098 |
} |
|
1099 |
} |
|
1100 |
||
1101 |
/** |
|
1102 |
* timed invokeAny(,,null) throws NPE |
|
1103 |
*/ |
|
1104 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
|
1105 |
final ExecutorService e = new CustomExecutor(2); |
|
1106 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1107 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1108 |
l.add(new StringTask()); |
|
1109 |
try { |
|
1110 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
|
1111 |
shouldThrow(); |
|
1112 |
} catch (NullPointerException success) {} |
|
1113 |
} |
|
1114 |
} |
|
1115 |
||
1116 |
/** |
|
1117 |
* timed invokeAny(empty collection) throws IAE |
|
1118 |
*/ |
|
1119 |
public void testTimedInvokeAny2() throws Exception { |
|
1120 |
final ExecutorService e = new CustomExecutor(2); |
|
1121 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1122 |
try { |
|
1123 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
|
1124 |
shouldThrow(); |
|
1125 |
} catch (IllegalArgumentException success) {} |
|
1126 |
} |
|
1127 |
} |
|
1128 |
||
1129 |
/** |
|
1130 |
* timed invokeAny(c) throws NPE if c has null elements |
|
1131 |
*/ |
|
1132 |
public void testTimedInvokeAny3() throws Exception { |
|
1133 |
CountDownLatch latch = new CountDownLatch(1); |
|
1134 |
final ExecutorService e = new CustomExecutor(2); |
|
1135 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1136 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1137 |
l.add(latchAwaitingStringTask(latch)); |
|
1138 |
l.add(null); |
|
1139 |
try { |
|
1140 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
|
1141 |
shouldThrow(); |
|
1142 |
} catch (NullPointerException success) {} |
|
1143 |
latch.countDown(); |
|
1144 |
} |
|
1145 |
} |
|
1146 |
||
1147 |
/** |
|
1148 |
* timed invokeAny(c) throws ExecutionException if no task completes |
|
1149 |
*/ |
|
1150 |
public void testTimedInvokeAny4() throws Exception { |
|
1151 |
final ExecutorService e = new CustomExecutor(2); |
|
1152 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1153 |
long startTime = System.nanoTime(); |
|
1154 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1155 |
l.add(new NPETask()); |
|
1156 |
try { |
|
1157 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
1158 |
shouldThrow(); |
|
1159 |
} catch (ExecutionException success) { |
|
1160 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1161 |
} |
|
1162 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
/** |
|
1167 |
* timed invokeAny(c) returns result of some task |
|
1168 |
*/ |
|
1169 |
public void testTimedInvokeAny5() throws Exception { |
|
1170 |
final ExecutorService e = new CustomExecutor(2); |
|
1171 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1172 |
long startTime = System.nanoTime(); |
|
1173 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1174 |
l.add(new StringTask()); |
|
1175 |
l.add(new StringTask()); |
|
1176 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
1177 |
assertSame(TEST_STRING, result); |
|
1178 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
1179 |
} |
|
1180 |
} |
|
1181 |
||
1182 |
/** |
|
1183 |
* timed invokeAll(null) throws NPE |
|
1184 |
*/ |
|
1185 |
public void testTimedInvokeAll1() throws Exception { |
|
1186 |
final ExecutorService e = new CustomExecutor(2); |
|
1187 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1188 |
try { |
|
1189 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
|
1190 |
shouldThrow(); |
|
1191 |
} catch (NullPointerException success) {} |
|
1192 |
} |
|
1193 |
} |
|
1194 |
||
1195 |
/** |
|
1196 |
* timed invokeAll(,,null) throws NPE |
|
1197 |
*/ |
|
1198 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
|
1199 |
final ExecutorService e = new CustomExecutor(2); |
|
1200 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1201 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1202 |
l.add(new StringTask()); |
|
1203 |
try { |
|
1204 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
|
1205 |
shouldThrow(); |
|
1206 |
} catch (NullPointerException success) {} |
|
1207 |
} |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
1211 |
* timed invokeAll(empty collection) returns empty collection |
|
1212 |
*/ |
|
1213 |
public void testTimedInvokeAll2() throws Exception { |
|
1214 |
final ExecutorService e = new CustomExecutor(2); |
|
1215 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1216 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
|
1217 |
assertTrue(r.isEmpty()); |
|
1218 |
} |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
1222 |
* timed invokeAll(c) throws NPE if c has null elements |
|
1223 |
*/ |
|
1224 |
public void testTimedInvokeAll3() throws Exception { |
|
1225 |
final ExecutorService e = new CustomExecutor(2); |
|
1226 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1227 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1228 |
l.add(new StringTask()); |
|
1229 |
l.add(null); |
|
1230 |
try { |
|
1231 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
|
1232 |
shouldThrow(); |
|
1233 |
} catch (NullPointerException success) {} |
|
1234 |
} |
|
1235 |
} |
|
1236 |
||
1237 |
/** |
|
1238 |
* get of element of invokeAll(c) throws exception on failed task |
|
1239 |
*/ |
|
1240 |
public void testTimedInvokeAll4() throws Exception { |
|
1241 |
final ExecutorService e = new CustomExecutor(2); |
|
1242 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1243 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1244 |
l.add(new NPETask()); |
|
1245 |
List<Future<String>> futures = |
|
1246 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
|
1247 |
assertEquals(1, futures.size()); |
|
1248 |
try { |
|
1249 |
futures.get(0).get(); |
|
1250 |
shouldThrow(); |
|
1251 |
} catch (ExecutionException success) { |
|
1252 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1253 |
} |
|
1254 |
} |
|
1255 |
} |
|
1256 |
||
1257 |
/** |
|
1258 |
* timed invokeAll(c) returns results of all completed tasks |
|
1259 |
*/ |
|
1260 |
public void testTimedInvokeAll5() throws Exception { |
|
1261 |
final ExecutorService e = new CustomExecutor(2); |
|
1262 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1263 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
|
1264 |
l.add(new StringTask()); |
|
1265 |
l.add(new StringTask()); |
|
1266 |
List<Future<String>> futures = |
|
1267 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
|
1268 |
assertEquals(2, futures.size()); |
|
1269 |
for (Future<String> future : futures) |
|
1270 |
assertSame(TEST_STRING, future.get()); |
|
1271 |
} |
|
1272 |
} |
|
1273 |
||
1274 |
/** |
|
1275 |
* timed invokeAll(c) cancels tasks not completed by timeout |
|
1276 |
*/ |
|
1277 |
public void testTimedInvokeAll6() throws Exception { |
|
1278 |
for (long timeout = timeoutMillis();;) { |
|
1279 |
final CountDownLatch done = new CountDownLatch(1); |
|
1280 |
final Callable<String> waiter = new CheckedCallable<String>() { |
|
1281 |
public String realCall() { |
|
1282 |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
|
1283 |
catch (InterruptedException ok) {} |
|
1284 |
return "1"; }}; |
|
1285 |
final ExecutorService p = new CustomExecutor(2); |
|
1286 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
1287 |
List<Callable<String>> tasks = new ArrayList<>(); |
|
1288 |
tasks.add(new StringTask("0")); |
|
1289 |
tasks.add(waiter); |
|
1290 |
tasks.add(new StringTask("2")); |
|
1291 |
long startTime = System.nanoTime(); |
|
1292 |
List<Future<String>> futures = |
|
1293 |
p.invokeAll(tasks, timeout, MILLISECONDS); |
|
1294 |
assertEquals(tasks.size(), futures.size()); |
|
1295 |
assertTrue(millisElapsedSince(startTime) >= timeout); |
|
1296 |
for (Future future : futures) |
|
1297 |
assertTrue(future.isDone()); |
|
1298 |
assertTrue(futures.get(1).isCancelled()); |
|
1299 |
try { |
|
1300 |
assertEquals("0", futures.get(0).get()); |
|
1301 |
assertEquals("2", futures.get(2).get()); |
|
1302 |
break; |
|
1303 |
} catch (CancellationException retryWithLongerTimeout) { |
|
1304 |
timeout *= 2; |
|
1305 |
if (timeout >= LONG_DELAY_MS / 2) |
|
1306 |
fail("expected exactly one task to be cancelled"); |
|
1307 |
} |
|
1308 |
} |
|
1309 |
} |
|
1310 |
} |
|
1311 |
||
1312 |
} |