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