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