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