7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.util.concurrent.atomic.*; |
12 |
|
|
13 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(ScheduledExecutorSubclassTest.class); |
18 |
> |
return new TestSuite(ScheduledExecutorSubclassTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
74 |
|
} |
75 |
|
|
76 |
|
|
76 |
– |
|
77 |
|
/** |
78 |
|
* execute successfully executes a runnable |
79 |
|
*/ |
80 |
< |
public void testExecute() { |
81 |
< |
try { |
82 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
83 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
84 |
< |
p1.execute(runnable); |
85 |
< |
assertFalse(runnable.done); |
86 |
< |
Thread.sleep(SHORT_DELAY_MS); |
87 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
88 |
< |
try { |
89 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
90 |
< |
} catch (InterruptedException e) { |
91 |
< |
unexpectedException(); |
92 |
< |
} |
93 |
< |
assertTrue(runnable.done); |
94 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
95 |
< |
joinPool(p1); |
96 |
< |
} |
97 |
< |
catch (Exception e) { |
98 |
< |
unexpectedException(); |
80 |
> |
public void testExecute() throws InterruptedException { |
81 |
> |
CustomExecutor p = new CustomExecutor(1); |
82 |
> |
final CountDownLatch done = new CountDownLatch(1); |
83 |
> |
final Runnable task = new CheckedRunnable() { |
84 |
> |
public void realRun() { |
85 |
> |
done.countDown(); |
86 |
> |
}}; |
87 |
> |
try { |
88 |
> |
p.execute(task); |
89 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
90 |
> |
} finally { |
91 |
> |
joinPool(p); |
92 |
|
} |
100 |
– |
|
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
/** |
97 |
|
* delayed schedule of callable successfully executes after delay |
98 |
|
*/ |
99 |
< |
public void testSchedule1() { |
100 |
< |
try { |
101 |
< |
TrackedCallable callable = new TrackedCallable(); |
102 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
103 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
104 |
< |
assertFalse(callable.done); |
105 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
106 |
< |
assertTrue(callable.done); |
107 |
< |
assertEquals(Boolean.TRUE, f.get()); |
108 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
109 |
< |
joinPool(p1); |
110 |
< |
} catch (RejectedExecutionException e) {} |
111 |
< |
catch (Exception e) { |
112 |
< |
e.printStackTrace(); |
113 |
< |
unexpectedException(); |
99 |
> |
public void testSchedule1() throws Exception { |
100 |
> |
CustomExecutor p = new CustomExecutor(1); |
101 |
> |
final long t0 = System.nanoTime(); |
102 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
103 |
> |
final CountDownLatch done = new CountDownLatch(1); |
104 |
> |
try { |
105 |
> |
Callable task = new CheckedCallable<Boolean>() { |
106 |
> |
public Boolean realCall() { |
107 |
> |
done.countDown(); |
108 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
109 |
> |
return Boolean.TRUE; |
110 |
> |
}}; |
111 |
> |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
112 |
> |
assertEquals(Boolean.TRUE, f.get()); |
113 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
114 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
115 |
> |
} finally { |
116 |
> |
joinPool(p); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* delayed schedule of runnable successfully executes after delay |
122 |
< |
*/ |
123 |
< |
public void testSchedule3() { |
124 |
< |
try { |
125 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
126 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
127 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
128 |
< |
Thread.sleep(SHORT_DELAY_MS); |
129 |
< |
assertFalse(runnable.done); |
130 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
131 |
< |
assertTrue(runnable.done); |
132 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
133 |
< |
joinPool(p1); |
134 |
< |
} catch (Exception e) { |
135 |
< |
unexpectedException(); |
121 |
> |
* delayed schedule of runnable successfully executes after delay |
122 |
> |
*/ |
123 |
> |
public void testSchedule3() throws Exception { |
124 |
> |
CustomExecutor p = new CustomExecutor(1); |
125 |
> |
final long t0 = System.nanoTime(); |
126 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
127 |
> |
final CountDownLatch done = new CountDownLatch(1); |
128 |
> |
try { |
129 |
> |
Runnable task = new CheckedRunnable() { |
130 |
> |
public void realRun() { |
131 |
> |
done.countDown(); |
132 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
133 |
> |
}}; |
134 |
> |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
135 |
> |
assertNull(f.get()); |
136 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
137 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
138 |
> |
} finally { |
139 |
> |
joinPool(p); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
145 |
|
*/ |
146 |
< |
public void testSchedule4() { |
147 |
< |
try { |
148 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
149 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
150 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
< |
assertFalse(runnable.done); |
152 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
153 |
< |
assertTrue(runnable.done); |
154 |
< |
h.cancel(true); |
155 |
< |
joinPool(p1); |
156 |
< |
} catch (Exception e) { |
157 |
< |
unexpectedException(); |
146 |
> |
public void testSchedule4() throws InterruptedException { |
147 |
> |
CustomExecutor p = new CustomExecutor(1); |
148 |
> |
final long t0 = System.nanoTime(); |
149 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
150 |
> |
final CountDownLatch done = new CountDownLatch(1); |
151 |
> |
try { |
152 |
> |
Runnable task = new CheckedRunnable() { |
153 |
> |
public void realRun() { |
154 |
> |
done.countDown(); |
155 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
156 |
> |
}}; |
157 |
> |
ScheduledFuture f = |
158 |
> |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
159 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
160 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
161 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
162 |
> |
f.cancel(true); |
163 |
> |
} finally { |
164 |
> |
joinPool(p); |
165 |
|
} |
166 |
|
} |
167 |
|
|
162 |
– |
static class RunnableCounter implements Runnable { |
163 |
– |
AtomicInteger count = new AtomicInteger(0); |
164 |
– |
public void run() { count.getAndIncrement(); } |
165 |
– |
} |
166 |
– |
|
168 |
|
/** |
169 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
170 |
|
*/ |
171 |
< |
public void testSchedule5() { |
172 |
< |
try { |
173 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
174 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
175 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
176 |
< |
assertFalse(runnable.done); |
177 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
178 |
< |
assertTrue(runnable.done); |
179 |
< |
h.cancel(true); |
180 |
< |
joinPool(p1); |
181 |
< |
} catch (Exception e) { |
182 |
< |
unexpectedException(); |
171 |
> |
public void testSchedule5() throws InterruptedException { |
172 |
> |
CustomExecutor p = new CustomExecutor(1); |
173 |
> |
final long t0 = System.nanoTime(); |
174 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
175 |
> |
final CountDownLatch done = new CountDownLatch(1); |
176 |
> |
try { |
177 |
> |
Runnable task = new CheckedRunnable() { |
178 |
> |
public void realRun() { |
179 |
> |
done.countDown(); |
180 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
181 |
> |
}}; |
182 |
> |
ScheduledFuture f = |
183 |
> |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
184 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
185 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
186 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
187 |
> |
f.cancel(true); |
188 |
> |
} finally { |
189 |
> |
joinPool(p); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
+ |
static class RunnableCounter implements Runnable { |
194 |
+ |
AtomicInteger count = new AtomicInteger(0); |
195 |
+ |
public void run() { count.getAndIncrement(); } |
196 |
+ |
} |
197 |
+ |
|
198 |
|
/** |
199 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
200 |
|
*/ |
201 |
< |
public void testFixedRateSequence() { |
202 |
< |
try { |
203 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
204 |
< |
RunnableCounter counter = new RunnableCounter(); |
205 |
< |
ScheduledFuture h = |
206 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
207 |
< |
Thread.sleep(SMALL_DELAY_MS); |
208 |
< |
h.cancel(true); |
209 |
< |
int c = counter.count.get(); |
210 |
< |
// By time scaling conventions, we must have at least |
211 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
212 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
213 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
201 |
< |
joinPool(p1); |
202 |
< |
} catch (Exception e) { |
203 |
< |
unexpectedException(); |
204 |
< |
} |
201 |
> |
public void testFixedRateSequence() throws InterruptedException { |
202 |
> |
CustomExecutor p = new CustomExecutor(1); |
203 |
> |
RunnableCounter counter = new RunnableCounter(); |
204 |
> |
ScheduledFuture h = |
205 |
> |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
206 |
> |
Thread.sleep(SMALL_DELAY_MS); |
207 |
> |
h.cancel(true); |
208 |
> |
int c = counter.count.get(); |
209 |
> |
// By time scaling conventions, we must have at least |
210 |
> |
// an execution per SHORT delay, but no more than one SHORT more |
211 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
212 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
213 |
> |
joinPool(p); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
218 |
|
*/ |
219 |
< |
public void testFixedDelaySequence() { |
220 |
< |
try { |
221 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
222 |
< |
RunnableCounter counter = new RunnableCounter(); |
223 |
< |
ScheduledFuture h = |
224 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
225 |
< |
Thread.sleep(SMALL_DELAY_MS); |
226 |
< |
h.cancel(true); |
227 |
< |
int c = counter.count.get(); |
228 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
229 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
221 |
< |
joinPool(p1); |
222 |
< |
} catch (Exception e) { |
223 |
< |
unexpectedException(); |
224 |
< |
} |
219 |
> |
public void testFixedDelaySequence() throws InterruptedException { |
220 |
> |
CustomExecutor p = new CustomExecutor(1); |
221 |
> |
RunnableCounter counter = new RunnableCounter(); |
222 |
> |
ScheduledFuture h = |
223 |
> |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
224 |
> |
Thread.sleep(SMALL_DELAY_MS); |
225 |
> |
h.cancel(true); |
226 |
> |
int c = counter.count.get(); |
227 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
228 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
229 |
> |
joinPool(p); |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
/** |
234 |
< |
* execute (null) throws NPE |
234 |
> |
* execute(null) throws NPE |
235 |
|
*/ |
236 |
< |
public void testExecuteNull() { |
237 |
< |
CustomExecutor se = null; |
236 |
> |
public void testExecuteNull() throws InterruptedException { |
237 |
> |
CustomExecutor se = new CustomExecutor(1); |
238 |
|
try { |
239 |
< |
se = new CustomExecutor(1); |
235 |
< |
se.execute(null); |
239 |
> |
se.execute(null); |
240 |
|
shouldThrow(); |
241 |
< |
} catch (NullPointerException success) {} |
242 |
< |
catch (Exception e) { |
239 |
< |
unexpectedException(); |
240 |
< |
} |
241 |
< |
|
242 |
< |
joinPool(se); |
241 |
> |
} catch (NullPointerException success) {} |
242 |
> |
joinPool(se); |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
< |
* schedule (null) throws NPE |
246 |
> |
* schedule(null) throws NPE |
247 |
|
*/ |
248 |
< |
public void testScheduleNull() { |
248 |
> |
public void testScheduleNull() throws InterruptedException { |
249 |
|
CustomExecutor se = new CustomExecutor(1); |
250 |
< |
try { |
250 |
> |
try { |
251 |
|
TrackedCallable callable = null; |
252 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
252 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
253 |
|
shouldThrow(); |
254 |
< |
} catch (NullPointerException success) {} |
255 |
< |
catch (Exception e) { |
256 |
< |
unexpectedException(); |
257 |
< |
} |
258 |
< |
joinPool(se); |
254 |
> |
} catch (NullPointerException success) {} |
255 |
> |
joinPool(se); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
263 |
|
try { |
264 |
|
se.shutdown(); |
265 |
|
se.schedule(new NoOpRunnable(), |
266 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
266 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
267 |
|
shouldThrow(); |
268 |
|
} catch (RejectedExecutionException success) { |
269 |
|
} catch (SecurityException ok) { |
270 |
|
} |
271 |
|
|
272 |
|
joinPool(se); |
276 |
– |
|
273 |
|
} |
274 |
|
|
275 |
|
/** |
280 |
|
try { |
281 |
|
se.shutdown(); |
282 |
|
se.schedule(new NoOpCallable(), |
283 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
283 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
284 |
|
shouldThrow(); |
285 |
|
} catch (RejectedExecutionException success) { |
286 |
|
} catch (SecurityException ok) { |
294 |
|
public void testSchedule3_RejectedExecutionException() { |
295 |
|
CustomExecutor se = new CustomExecutor(1); |
296 |
|
try { |
297 |
< |
se.shutdown(); |
298 |
< |
se.schedule(new NoOpCallable(), |
299 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
300 |
< |
shouldThrow(); |
301 |
< |
} catch (RejectedExecutionException success) { |
302 |
< |
} catch (SecurityException ok) { |
303 |
< |
} |
297 |
> |
se.shutdown(); |
298 |
> |
se.schedule(new NoOpCallable(), |
299 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
300 |
> |
shouldThrow(); |
301 |
> |
} catch (RejectedExecutionException success) { |
302 |
> |
} catch (SecurityException ok) { |
303 |
> |
} |
304 |
|
joinPool(se); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
308 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
309 |
|
*/ |
310 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
311 |
|
CustomExecutor se = new CustomExecutor(1); |
312 |
|
try { |
313 |
|
se.shutdown(); |
314 |
|
se.scheduleAtFixedRate(new NoOpRunnable(), |
315 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
315 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
316 |
|
shouldThrow(); |
317 |
|
} catch (RejectedExecutionException success) { |
318 |
|
} catch (SecurityException ok) { |
328 |
|
try { |
329 |
|
se.shutdown(); |
330 |
|
se.scheduleWithFixedDelay(new NoOpRunnable(), |
331 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
331 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
332 |
|
shouldThrow(); |
333 |
|
} catch (RejectedExecutionException success) { |
334 |
|
} catch (SecurityException ok) { |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
< |
* getActiveCount increases but doesn't overestimate, when a |
341 |
< |
* thread becomes active |
340 |
> |
* getActiveCount increases but doesn't overestimate, when a |
341 |
> |
* thread becomes active |
342 |
|
*/ |
343 |
< |
public void testGetActiveCount() { |
344 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
345 |
< |
assertEquals(0, p2.getActiveCount()); |
346 |
< |
p2.execute(new SmallRunnable()); |
347 |
< |
try { |
348 |
< |
Thread.sleep(SHORT_DELAY_MS); |
349 |
< |
} catch (Exception e) { |
350 |
< |
unexpectedException(); |
343 |
> |
public void testGetActiveCount() throws InterruptedException { |
344 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
345 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
346 |
> |
final CountDownLatch done = new CountDownLatch(1); |
347 |
> |
try { |
348 |
> |
assertEquals(0, p.getActiveCount()); |
349 |
> |
p.execute(new CheckedRunnable() { |
350 |
> |
public void realRun() throws InterruptedException { |
351 |
> |
threadStarted.countDown(); |
352 |
> |
assertEquals(1, p.getActiveCount()); |
353 |
> |
done.await(); |
354 |
> |
}}); |
355 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
356 |
> |
assertEquals(1, p.getActiveCount()); |
357 |
> |
} finally { |
358 |
> |
done.countDown(); |
359 |
> |
joinPool(p); |
360 |
|
} |
356 |
– |
assertEquals(1, p2.getActiveCount()); |
357 |
– |
joinPool(p2); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
365 |
< |
* when tasks complete |
364 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
365 |
> |
* when tasks complete |
366 |
|
*/ |
367 |
< |
public void testGetCompletedTaskCount() { |
368 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
369 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
370 |
< |
p2.execute(new SmallRunnable()); |
371 |
< |
try { |
372 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
373 |
< |
} catch (Exception e) { |
374 |
< |
unexpectedException(); |
367 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
368 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
369 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
370 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
371 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
372 |
> |
try { |
373 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
374 |
> |
p.execute(new CheckedRunnable() { |
375 |
> |
public void realRun() throws InterruptedException { |
376 |
> |
threadStarted.countDown(); |
377 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
378 |
> |
threadProceed.await(); |
379 |
> |
threadDone.countDown(); |
380 |
> |
}}); |
381 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
382 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
383 |
> |
threadProceed.countDown(); |
384 |
> |
threadDone.await(); |
385 |
> |
Thread.sleep(SHORT_DELAY_MS); |
386 |
> |
assertEquals(1, p.getCompletedTaskCount()); |
387 |
> |
} finally { |
388 |
> |
joinPool(p); |
389 |
|
} |
373 |
– |
assertEquals(1, p2.getCompletedTaskCount()); |
374 |
– |
joinPool(p2); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
393 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
394 |
|
*/ |
395 |
|
public void testGetCorePoolSize() { |
396 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
397 |
< |
assertEquals(1, p1.getCorePoolSize()); |
398 |
< |
joinPool(p1); |
396 |
> |
CustomExecutor p = new CustomExecutor(1); |
397 |
> |
assertEquals(1, p.getCorePoolSize()); |
398 |
> |
joinPool(p); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
403 |
< |
* multiple threads active |
402 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
403 |
> |
* multiple threads active |
404 |
|
*/ |
405 |
< |
public void testGetLargestPoolSize() { |
406 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
407 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
408 |
< |
p2.execute(new SmallRunnable()); |
409 |
< |
p2.execute(new SmallRunnable()); |
410 |
< |
try { |
411 |
< |
Thread.sleep(SHORT_DELAY_MS); |
412 |
< |
} catch (Exception e) { |
413 |
< |
unexpectedException(); |
405 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
406 |
> |
final int THREADS = 3; |
407 |
> |
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
408 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
409 |
> |
final CountDownLatch done = new CountDownLatch(1); |
410 |
> |
try { |
411 |
> |
assertEquals(0, p.getLargestPoolSize()); |
412 |
> |
for (int i = 0; i < THREADS; i++) |
413 |
> |
p.execute(new CheckedRunnable() { |
414 |
> |
public void realRun() throws InterruptedException { |
415 |
> |
threadsStarted.countDown(); |
416 |
> |
done.await(); |
417 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
418 |
> |
}}); |
419 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
420 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
421 |
> |
} finally { |
422 |
> |
done.countDown(); |
423 |
> |
joinPool(p); |
424 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
425 |
|
} |
400 |
– |
assertEquals(2, p2.getLargestPoolSize()); |
401 |
– |
joinPool(p2); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
430 |
< |
* become active |
429 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
430 |
> |
* become active |
431 |
|
*/ |
432 |
< |
public void testGetPoolSize() { |
433 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
434 |
< |
assertEquals(0, p1.getPoolSize()); |
435 |
< |
p1.execute(new SmallRunnable()); |
436 |
< |
assertEquals(1, p1.getPoolSize()); |
437 |
< |
joinPool(p1); |
432 |
> |
public void testGetPoolSize() throws InterruptedException { |
433 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
434 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
435 |
> |
final CountDownLatch done = new CountDownLatch(1); |
436 |
> |
try { |
437 |
> |
assertEquals(0, p.getPoolSize()); |
438 |
> |
p.execute(new CheckedRunnable() { |
439 |
> |
public void realRun() throws InterruptedException { |
440 |
> |
threadStarted.countDown(); |
441 |
> |
assertEquals(1, p.getPoolSize()); |
442 |
> |
done.await(); |
443 |
> |
}}); |
444 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
445 |
> |
assertEquals(1, p.getPoolSize()); |
446 |
> |
} finally { |
447 |
> |
done.countDown(); |
448 |
> |
joinPool(p); |
449 |
> |
} |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
454 |
< |
* submitted |
453 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
454 |
> |
* submitted |
455 |
|
*/ |
456 |
< |
public void testGetTaskCount() { |
457 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
458 |
< |
assertEquals(0, p1.getTaskCount()); |
459 |
< |
for (int i = 0; i < 5; i++) |
460 |
< |
p1.execute(new SmallRunnable()); |
461 |
< |
try { |
462 |
< |
Thread.sleep(SHORT_DELAY_MS); |
463 |
< |
} catch (Exception e) { |
464 |
< |
unexpectedException(); |
456 |
> |
public void testGetTaskCount() throws InterruptedException { |
457 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
458 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
459 |
> |
final CountDownLatch done = new CountDownLatch(1); |
460 |
> |
final int TASKS = 5; |
461 |
> |
try { |
462 |
> |
assertEquals(0, p.getTaskCount()); |
463 |
> |
for (int i = 0; i < TASKS; i++) |
464 |
> |
p.execute(new CheckedRunnable() { |
465 |
> |
public void realRun() throws InterruptedException { |
466 |
> |
threadStarted.countDown(); |
467 |
> |
done.await(); |
468 |
> |
}}); |
469 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
470 |
> |
assertEquals(TASKS, p.getTaskCount()); |
471 |
> |
} finally { |
472 |
> |
done.countDown(); |
473 |
> |
joinPool(p); |
474 |
|
} |
430 |
– |
assertEquals(5, p1.getTaskCount()); |
431 |
– |
joinPool(p1); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
479 |
|
*/ |
480 |
|
public void testGetThreadFactory() { |
481 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
482 |
< |
CustomExecutor p = new CustomExecutor(1, tf); |
482 |
> |
CustomExecutor p = new CustomExecutor(1, tf); |
483 |
|
assertSame(tf, p.getThreadFactory()); |
484 |
|
joinPool(p); |
485 |
|
} |
489 |
|
*/ |
490 |
|
public void testSetThreadFactory() { |
491 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
492 |
< |
CustomExecutor p = new CustomExecutor(1); |
492 |
> |
CustomExecutor p = new CustomExecutor(1); |
493 |
|
p.setThreadFactory(tf); |
494 |
|
assertSame(tf, p.getThreadFactory()); |
495 |
|
joinPool(p); |
499 |
|
* setThreadFactory(null) throws NPE |
500 |
|
*/ |
501 |
|
public void testSetThreadFactoryNull() { |
502 |
< |
CustomExecutor p = new CustomExecutor(1); |
502 |
> |
CustomExecutor p = new CustomExecutor(1); |
503 |
|
try { |
504 |
|
p.setThreadFactory(null); |
505 |
|
shouldThrow(); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* is isShutDown is false before shutdown, true after |
513 |
> |
* isShutDown is false before shutdown, true after |
514 |
|
*/ |
515 |
|
public void testIsShutdown() { |
516 |
< |
|
474 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
516 |
> |
CustomExecutor p = new CustomExecutor(1); |
517 |
|
try { |
518 |
< |
assertFalse(p1.isShutdown()); |
518 |
> |
assertFalse(p.isShutdown()); |
519 |
|
} |
520 |
|
finally { |
521 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
521 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
522 |
|
} |
523 |
< |
assertTrue(p1.isShutdown()); |
523 |
> |
assertTrue(p.isShutdown()); |
524 |
|
} |
525 |
|
|
526 |
|
|
527 |
|
/** |
528 |
< |
* isTerminated is false before termination, true after |
528 |
> |
* isTerminated is false before termination, true after |
529 |
|
*/ |
530 |
< |
public void testIsTerminated() { |
531 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
530 |
> |
public void testIsTerminated() throws InterruptedException { |
531 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
532 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
533 |
> |
final CountDownLatch done = new CountDownLatch(1); |
534 |
> |
assertFalse(p.isTerminated()); |
535 |
|
try { |
536 |
< |
p1.execute(new SmallRunnable()); |
536 |
> |
p.execute(new CheckedRunnable() { |
537 |
> |
public void realRun() throws InterruptedException { |
538 |
> |
threadStarted.countDown(); |
539 |
> |
assertFalse(p.isTerminated()); |
540 |
> |
done.await(); |
541 |
> |
}}); |
542 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
543 |
> |
done.countDown(); |
544 |
|
} finally { |
545 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
494 |
< |
} |
495 |
< |
try { |
496 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
497 |
< |
assertTrue(p1.isTerminated()); |
498 |
< |
} catch (Exception e) { |
499 |
< |
unexpectedException(); |
545 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
546 |
|
} |
547 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
548 |
+ |
assertTrue(p.isTerminated()); |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* isTerminating is not true when running or when terminated |
552 |
> |
* isTerminating is not true when running or when terminated |
553 |
|
*/ |
554 |
< |
public void testIsTerminating() { |
555 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
556 |
< |
assertFalse(p1.isTerminating()); |
554 |
> |
public void testIsTerminating() throws InterruptedException { |
555 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
556 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
557 |
> |
final CountDownLatch done = new CountDownLatch(1); |
558 |
|
try { |
559 |
< |
p1.execute(new SmallRunnable()); |
560 |
< |
assertFalse(p1.isTerminating()); |
559 |
> |
assertFalse(p.isTerminating()); |
560 |
> |
p.execute(new CheckedRunnable() { |
561 |
> |
public void realRun() throws InterruptedException { |
562 |
> |
threadStarted.countDown(); |
563 |
> |
assertFalse(p.isTerminating()); |
564 |
> |
done.await(); |
565 |
> |
}}); |
566 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
567 |
> |
assertFalse(p.isTerminating()); |
568 |
> |
done.countDown(); |
569 |
|
} finally { |
570 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
514 |
< |
} |
515 |
< |
try { |
516 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
< |
assertTrue(p1.isTerminated()); |
518 |
< |
assertFalse(p1.isTerminating()); |
519 |
< |
} catch (Exception e) { |
520 |
< |
unexpectedException(); |
570 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
571 |
|
} |
572 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
573 |
+ |
assertTrue(p.isTerminated()); |
574 |
+ |
assertFalse(p.isTerminating()); |
575 |
|
} |
576 |
|
|
577 |
|
/** |
578 |
|
* getQueue returns the work queue, which contains queued tasks |
579 |
|
*/ |
580 |
< |
public void testGetQueue() { |
581 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
582 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
583 |
< |
for (int i = 0; i < 5; i++) { |
531 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
532 |
< |
} |
580 |
> |
public void testGetQueue() throws InterruptedException { |
581 |
> |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
582 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
583 |
> |
final CountDownLatch done = new CountDownLatch(1); |
584 |
|
try { |
585 |
< |
Thread.sleep(SHORT_DELAY_MS); |
586 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
587 |
< |
assertTrue(q.contains(tasks[4])); |
585 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
586 |
> |
for (int i = 0; i < tasks.length; i++) { |
587 |
> |
Runnable r = new CheckedRunnable() { |
588 |
> |
public void realRun() throws InterruptedException { |
589 |
> |
threadStarted.countDown(); |
590 |
> |
done.await(); |
591 |
> |
}}; |
592 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
593 |
> |
} |
594 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
595 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
596 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
597 |
|
assertFalse(q.contains(tasks[0])); |
538 |
– |
} catch (Exception e) { |
539 |
– |
unexpectedException(); |
598 |
|
} finally { |
599 |
< |
joinPool(p1); |
599 |
> |
done.countDown(); |
600 |
> |
joinPool(p); |
601 |
|
} |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
|
* remove(task) removes queued task, and fails to remove active task |
606 |
|
*/ |
607 |
< |
public void testRemove() { |
608 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
607 |
> |
public void testRemove() throws InterruptedException { |
608 |
> |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
609 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
610 |
< |
for (int i = 0; i < 5; i++) { |
611 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
553 |
< |
} |
610 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
611 |
> |
final CountDownLatch done = new CountDownLatch(1); |
612 |
|
try { |
613 |
< |
Thread.sleep(SHORT_DELAY_MS); |
614 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
615 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
613 |
> |
for (int i = 0; i < tasks.length; i++) { |
614 |
> |
Runnable r = new CheckedRunnable() { |
615 |
> |
public void realRun() throws InterruptedException { |
616 |
> |
threadStarted.countDown(); |
617 |
> |
done.await(); |
618 |
> |
}}; |
619 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
620 |
> |
} |
621 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
622 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
623 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
624 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
625 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
626 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
627 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
626 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
627 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
628 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
629 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
630 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
630 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
631 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
566 |
– |
} catch (Exception e) { |
567 |
– |
unexpectedException(); |
632 |
|
} finally { |
633 |
< |
joinPool(p1); |
633 |
> |
done.countDown(); |
634 |
> |
joinPool(p); |
635 |
|
} |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
< |
* purge removes cancelled tasks from the queue |
639 |
> |
* purge removes cancelled tasks from the queue |
640 |
|
*/ |
641 |
< |
public void testPurge() { |
642 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
641 |
> |
public void testPurge() throws InterruptedException { |
642 |
> |
CustomExecutor p = new CustomExecutor(1); |
643 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
644 |
< |
for (int i = 0; i < 5; i++) { |
645 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
644 |
> |
for (int i = 0; i < tasks.length; i++) { |
645 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
646 |
|
} |
647 |
|
try { |
648 |
< |
int max = 5; |
648 |
> |
int max = tasks.length; |
649 |
|
if (tasks[4].cancel(true)) --max; |
650 |
|
if (tasks[3].cancel(true)) --max; |
651 |
|
// There must eventually be an interference-free point at |
652 |
|
// which purge will not fail. (At worst, when queue is empty.) |
653 |
|
int k; |
654 |
|
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
655 |
< |
p1.purge(); |
656 |
< |
long count = p1.getTaskCount(); |
655 |
> |
p.purge(); |
656 |
> |
long count = p.getTaskCount(); |
657 |
|
if (count >= 0 && count <= max) |
658 |
|
break; |
659 |
|
Thread.sleep(1); |
660 |
|
} |
661 |
|
assertTrue(k < SMALL_DELAY_MS); |
597 |
– |
} catch (Exception e) { |
598 |
– |
unexpectedException(); |
662 |
|
} finally { |
663 |
< |
joinPool(p1); |
663 |
> |
for (ScheduledFuture task : tasks) |
664 |
> |
task.cancel(true); |
665 |
> |
joinPool(p); |
666 |
|
} |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* shutDownNow returns a list containing tasks that were not run |
670 |
> |
* shutDownNow returns a list containing tasks that were not run |
671 |
|
*/ |
672 |
|
public void testShutDownNow() { |
673 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
673 |
> |
CustomExecutor p = new CustomExecutor(1); |
674 |
|
for (int i = 0; i < 5; i++) |
675 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
675 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
676 |
|
List l; |
677 |
|
try { |
678 |
< |
l = p1.shutdownNow(); |
678 |
> |
l = p.shutdownNow(); |
679 |
|
} catch (SecurityException ok) { |
680 |
|
return; |
681 |
|
} |
682 |
< |
assertTrue(p1.isShutdown()); |
683 |
< |
assertTrue(l.size() > 0 && l.size() <= 5); |
684 |
< |
joinPool(p1); |
682 |
> |
assertTrue(p.isShutdown()); |
683 |
> |
assertTrue(l.size() > 0 && l.size() <= 5); |
684 |
> |
joinPool(p); |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
|
* In default setting, shutdown cancels periodic but not delayed |
689 |
|
* tasks at shutdown |
690 |
|
*/ |
691 |
< |
public void testShutDown1() { |
692 |
< |
try { |
693 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
694 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
630 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
631 |
< |
|
632 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
633 |
< |
for (int i = 0; i < 5; i++) |
634 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
635 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
636 |
< |
BlockingQueue q = p1.getQueue(); |
637 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
638 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
639 |
< |
assertFalse(t.isCancelled()); |
640 |
< |
} |
641 |
< |
assertTrue(p1.isShutdown()); |
642 |
< |
Thread.sleep(SMALL_DELAY_MS); |
643 |
< |
for (int i = 0; i < 5; ++i) { |
644 |
< |
assertTrue(tasks[i].isDone()); |
645 |
< |
assertFalse(tasks[i].isCancelled()); |
646 |
< |
} |
691 |
> |
public void testShutDown1() throws InterruptedException { |
692 |
> |
CustomExecutor p = new CustomExecutor(1); |
693 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
694 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
695 |
|
|
696 |
+ |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
697 |
+ |
for (int i = 0; i < tasks.length; i++) |
698 |
+ |
tasks[i] = p.schedule(new NoOpRunnable(), |
699 |
+ |
SHORT_DELAY_MS, MILLISECONDS); |
700 |
+ |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
701 |
+ |
BlockingQueue<Runnable> q = p.getQueue(); |
702 |
+ |
for (ScheduledFuture task : tasks) { |
703 |
+ |
assertFalse(task.isDone()); |
704 |
+ |
assertFalse(task.isCancelled()); |
705 |
+ |
assertTrue(q.contains(task)); |
706 |
|
} |
707 |
< |
catch (Exception ex) { |
708 |
< |
unexpectedException(); |
707 |
> |
assertTrue(p.isShutdown()); |
708 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
709 |
> |
assertTrue(p.isTerminated()); |
710 |
> |
for (ScheduledFuture task : tasks) { |
711 |
> |
assertTrue(task.isDone()); |
712 |
> |
assertFalse(task.isCancelled()); |
713 |
|
} |
714 |
|
} |
715 |
|
|
718 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
719 |
|
* delayed tasks are cancelled at shutdown |
720 |
|
*/ |
721 |
< |
public void testShutDown2() { |
722 |
< |
try { |
723 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
724 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
725 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
726 |
< |
for (int i = 0; i < 5; i++) |
727 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
728 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
729 |
< |
assertTrue(p1.isShutdown()); |
730 |
< |
BlockingQueue q = p1.getQueue(); |
731 |
< |
assertTrue(q.isEmpty()); |
732 |
< |
Thread.sleep(SMALL_DELAY_MS); |
733 |
< |
assertTrue(p1.isTerminated()); |
734 |
< |
} |
735 |
< |
catch (Exception ex) { |
736 |
< |
unexpectedException(); |
721 |
> |
public void testShutDown2() throws InterruptedException { |
722 |
> |
CustomExecutor p = new CustomExecutor(1); |
723 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
724 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
725 |
> |
for (int i = 0; i < tasks.length; i++) |
726 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
727 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
728 |
> |
BlockingQueue q = p.getQueue(); |
729 |
> |
assertEquals(tasks.length, q.size()); |
730 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
731 |
> |
assertTrue(p.isShutdown()); |
732 |
> |
assertTrue(q.isEmpty()); |
733 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
734 |
> |
assertTrue(p.isTerminated()); |
735 |
> |
for (ScheduledFuture task : tasks) { |
736 |
> |
assertTrue(task.isDone()); |
737 |
> |
assertTrue(task.isCancelled()); |
738 |
|
} |
739 |
|
} |
740 |
|
|
741 |
|
|
742 |
|
/** |
743 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
744 |
< |
* periodic tasks are not cancelled at shutdown |
744 |
> |
* periodic tasks are cancelled at shutdown |
745 |
|
*/ |
746 |
< |
public void testShutDown3() { |
747 |
< |
try { |
748 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
749 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
750 |
< |
ScheduledFuture task = |
751 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
752 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
753 |
< |
assertTrue(p1.isShutdown()); |
754 |
< |
BlockingQueue q = p1.getQueue(); |
755 |
< |
assertTrue(q.isEmpty()); |
756 |
< |
Thread.sleep(SHORT_DELAY_MS); |
757 |
< |
assertTrue(p1.isTerminated()); |
758 |
< |
} |
696 |
< |
catch (Exception ex) { |
697 |
< |
unexpectedException(); |
698 |
< |
} |
746 |
> |
public void testShutDown3() throws InterruptedException { |
747 |
> |
CustomExecutor p = new CustomExecutor(1); |
748 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
749 |
> |
ScheduledFuture task = |
750 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
751 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
752 |
> |
assertTrue(p.isShutdown()); |
753 |
> |
BlockingQueue q = p.getQueue(); |
754 |
> |
assertTrue(p.getQueue().isEmpty()); |
755 |
> |
assertTrue(task.isDone()); |
756 |
> |
assertTrue(task.isCancelled()); |
757 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
758 |
> |
assertTrue(p.isTerminated()); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
762 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
763 |
< |
* periodic tasks are cancelled at shutdown |
763 |
> |
* periodic tasks are not cancelled at shutdown |
764 |
|
*/ |
765 |
< |
public void testShutDown4() { |
766 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
767 |
< |
try { |
768 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
765 |
> |
public void testShutDown4() throws InterruptedException { |
766 |
> |
CustomExecutor p = new CustomExecutor(1); |
767 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
768 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
769 |
> |
try { |
770 |
> |
final Runnable r = new CheckedRunnable() { |
771 |
> |
public void realRun() { |
772 |
> |
counter.countDown(); |
773 |
> |
}}; |
774 |
|
ScheduledFuture task = |
775 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
775 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
776 |
> |
assertFalse(task.isDone()); |
777 |
|
assertFalse(task.isCancelled()); |
778 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
778 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
779 |
|
assertFalse(task.isCancelled()); |
780 |
< |
assertFalse(p1.isTerminated()); |
781 |
< |
assertTrue(p1.isShutdown()); |
782 |
< |
Thread.sleep(SHORT_DELAY_MS); |
780 |
> |
assertFalse(p.isTerminated()); |
781 |
> |
assertTrue(p.isShutdown()); |
782 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
783 |
|
assertFalse(task.isCancelled()); |
784 |
< |
assertTrue(task.cancel(true)); |
784 |
> |
assertTrue(task.cancel(false)); |
785 |
|
assertTrue(task.isDone()); |
786 |
< |
Thread.sleep(SHORT_DELAY_MS); |
787 |
< |
assertTrue(p1.isTerminated()); |
788 |
< |
} |
723 |
< |
catch (Exception ex) { |
724 |
< |
unexpectedException(); |
786 |
> |
assertTrue(task.isCancelled()); |
787 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
788 |
> |
assertTrue(p.isTerminated()); |
789 |
|
} |
790 |
|
finally { |
791 |
< |
joinPool(p1); |
791 |
> |
joinPool(p); |
792 |
|
} |
793 |
|
} |
794 |
|
|
795 |
|
/** |
796 |
|
* completed submit of callable returns result |
797 |
|
*/ |
798 |
< |
public void testSubmitCallable() { |
798 |
> |
public void testSubmitCallable() throws Exception { |
799 |
|
ExecutorService e = new CustomExecutor(2); |
800 |
|
try { |
801 |
|
Future<String> future = e.submit(new StringTask()); |
802 |
|
String result = future.get(); |
803 |
|
assertSame(TEST_STRING, result); |
740 |
– |
} |
741 |
– |
catch (ExecutionException ex) { |
742 |
– |
unexpectedException(); |
743 |
– |
} |
744 |
– |
catch (InterruptedException ex) { |
745 |
– |
unexpectedException(); |
804 |
|
} finally { |
805 |
|
joinPool(e); |
806 |
|
} |
809 |
|
/** |
810 |
|
* completed submit of runnable returns successfully |
811 |
|
*/ |
812 |
< |
public void testSubmitRunnable() { |
812 |
> |
public void testSubmitRunnable() throws Exception { |
813 |
|
ExecutorService e = new CustomExecutor(2); |
814 |
|
try { |
815 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
816 |
|
future.get(); |
817 |
|
assertTrue(future.isDone()); |
760 |
– |
} |
761 |
– |
catch (ExecutionException ex) { |
762 |
– |
unexpectedException(); |
763 |
– |
} |
764 |
– |
catch (InterruptedException ex) { |
765 |
– |
unexpectedException(); |
818 |
|
} finally { |
819 |
|
joinPool(e); |
820 |
|
} |
823 |
|
/** |
824 |
|
* completed submit of (runnable, result) returns result |
825 |
|
*/ |
826 |
< |
public void testSubmitRunnable2() { |
826 |
> |
public void testSubmitRunnable2() throws Exception { |
827 |
|
ExecutorService e = new CustomExecutor(2); |
828 |
|
try { |
829 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
830 |
|
String result = future.get(); |
831 |
|
assertSame(TEST_STRING, result); |
780 |
– |
} |
781 |
– |
catch (ExecutionException ex) { |
782 |
– |
unexpectedException(); |
783 |
– |
} |
784 |
– |
catch (InterruptedException ex) { |
785 |
– |
unexpectedException(); |
832 |
|
} finally { |
833 |
|
joinPool(e); |
834 |
|
} |
837 |
|
/** |
838 |
|
* invokeAny(null) throws NPE |
839 |
|
*/ |
840 |
< |
public void testInvokeAny1() { |
840 |
> |
public void testInvokeAny1() throws Exception { |
841 |
|
ExecutorService e = new CustomExecutor(2); |
842 |
|
try { |
843 |
|
e.invokeAny(null); |
844 |
+ |
shouldThrow(); |
845 |
|
} catch (NullPointerException success) { |
799 |
– |
} catch (Exception ex) { |
800 |
– |
unexpectedException(); |
846 |
|
} finally { |
847 |
|
joinPool(e); |
848 |
|
} |
851 |
|
/** |
852 |
|
* invokeAny(empty collection) throws IAE |
853 |
|
*/ |
854 |
< |
public void testInvokeAny2() { |
854 |
> |
public void testInvokeAny2() throws Exception { |
855 |
|
ExecutorService e = new CustomExecutor(2); |
856 |
|
try { |
857 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
858 |
+ |
shouldThrow(); |
859 |
|
} catch (IllegalArgumentException success) { |
814 |
– |
} catch (Exception ex) { |
815 |
– |
unexpectedException(); |
860 |
|
} finally { |
861 |
|
joinPool(e); |
862 |
|
} |
865 |
|
/** |
866 |
|
* invokeAny(c) throws NPE if c has null elements |
867 |
|
*/ |
868 |
< |
public void testInvokeAny3() { |
868 |
> |
public void testInvokeAny3() throws Exception { |
869 |
> |
CountDownLatch latch = new CountDownLatch(1); |
870 |
|
ExecutorService e = new CustomExecutor(2); |
871 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
872 |
+ |
l.add(latchAwaitingStringTask(latch)); |
873 |
+ |
l.add(null); |
874 |
|
try { |
827 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
828 |
– |
l.add(new StringTask()); |
829 |
– |
l.add(null); |
875 |
|
e.invokeAny(l); |
876 |
+ |
shouldThrow(); |
877 |
|
} catch (NullPointerException success) { |
832 |
– |
} catch (Exception ex) { |
833 |
– |
unexpectedException(); |
878 |
|
} finally { |
879 |
+ |
latch.countDown(); |
880 |
|
joinPool(e); |
881 |
|
} |
882 |
|
} |
884 |
|
/** |
885 |
|
* invokeAny(c) throws ExecutionException if no task completes |
886 |
|
*/ |
887 |
< |
public void testInvokeAny4() { |
887 |
> |
public void testInvokeAny4() throws Exception { |
888 |
|
ExecutorService e = new CustomExecutor(2); |
889 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
+ |
l.add(new NPETask()); |
891 |
|
try { |
845 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
846 |
– |
l.add(new NPETask()); |
892 |
|
e.invokeAny(l); |
893 |
+ |
shouldThrow(); |
894 |
|
} catch (ExecutionException success) { |
895 |
< |
} catch (Exception ex) { |
850 |
< |
unexpectedException(); |
895 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
896 |
|
} finally { |
897 |
|
joinPool(e); |
898 |
|
} |
901 |
|
/** |
902 |
|
* invokeAny(c) returns result of some task |
903 |
|
*/ |
904 |
< |
public void testInvokeAny5() { |
904 |
> |
public void testInvokeAny5() throws Exception { |
905 |
|
ExecutorService e = new CustomExecutor(2); |
906 |
|
try { |
907 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
907 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
908 |
|
l.add(new StringTask()); |
909 |
|
l.add(new StringTask()); |
910 |
|
String result = e.invokeAny(l); |
911 |
|
assertSame(TEST_STRING, result); |
867 |
– |
} catch (ExecutionException success) { |
868 |
– |
} catch (Exception ex) { |
869 |
– |
unexpectedException(); |
912 |
|
} finally { |
913 |
|
joinPool(e); |
914 |
|
} |
917 |
|
/** |
918 |
|
* invokeAll(null) throws NPE |
919 |
|
*/ |
920 |
< |
public void testInvokeAll1() { |
920 |
> |
public void testInvokeAll1() throws Exception { |
921 |
|
ExecutorService e = new CustomExecutor(2); |
922 |
|
try { |
923 |
|
e.invokeAll(null); |
924 |
+ |
shouldThrow(); |
925 |
|
} catch (NullPointerException success) { |
883 |
– |
} catch (Exception ex) { |
884 |
– |
unexpectedException(); |
926 |
|
} finally { |
927 |
|
joinPool(e); |
928 |
|
} |
931 |
|
/** |
932 |
|
* invokeAll(empty collection) returns empty collection |
933 |
|
*/ |
934 |
< |
public void testInvokeAll2() { |
934 |
> |
public void testInvokeAll2() throws Exception { |
935 |
|
ExecutorService e = new CustomExecutor(2); |
936 |
|
try { |
937 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
938 |
|
assertTrue(r.isEmpty()); |
898 |
– |
} catch (Exception ex) { |
899 |
– |
unexpectedException(); |
939 |
|
} finally { |
940 |
|
joinPool(e); |
941 |
|
} |
944 |
|
/** |
945 |
|
* invokeAll(c) throws NPE if c has null elements |
946 |
|
*/ |
947 |
< |
public void testInvokeAll3() { |
947 |
> |
public void testInvokeAll3() throws Exception { |
948 |
|
ExecutorService e = new CustomExecutor(2); |
949 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
950 |
+ |
l.add(new StringTask()); |
951 |
+ |
l.add(null); |
952 |
|
try { |
911 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
912 |
– |
l.add(new StringTask()); |
913 |
– |
l.add(null); |
953 |
|
e.invokeAll(l); |
954 |
+ |
shouldThrow(); |
955 |
|
} catch (NullPointerException success) { |
916 |
– |
} catch (Exception ex) { |
917 |
– |
unexpectedException(); |
956 |
|
} finally { |
957 |
|
joinPool(e); |
958 |
|
} |
961 |
|
/** |
962 |
|
* get of invokeAll(c) throws exception on failed task |
963 |
|
*/ |
964 |
< |
public void testInvokeAll4() { |
964 |
> |
public void testInvokeAll4() throws Exception { |
965 |
|
ExecutorService e = new CustomExecutor(2); |
966 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
+ |
l.add(new NPETask()); |
968 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
969 |
+ |
assertEquals(1, futures.size()); |
970 |
|
try { |
971 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
972 |
< |
l.add(new NPETask()); |
931 |
< |
List<Future<String>> result = e.invokeAll(l); |
932 |
< |
assertEquals(1, result.size()); |
933 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
934 |
< |
it.next().get(); |
971 |
> |
futures.get(0).get(); |
972 |
> |
shouldThrow(); |
973 |
|
} catch (ExecutionException success) { |
974 |
< |
} catch (Exception ex) { |
937 |
< |
unexpectedException(); |
974 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
975 |
|
} finally { |
976 |
|
joinPool(e); |
977 |
|
} |
980 |
|
/** |
981 |
|
* invokeAll(c) returns results of all completed tasks |
982 |
|
*/ |
983 |
< |
public void testInvokeAll5() { |
983 |
> |
public void testInvokeAll5() throws Exception { |
984 |
|
ExecutorService e = new CustomExecutor(2); |
985 |
|
try { |
986 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
986 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
987 |
|
l.add(new StringTask()); |
988 |
|
l.add(new StringTask()); |
989 |
< |
List<Future<String>> result = e.invokeAll(l); |
990 |
< |
assertEquals(2, result.size()); |
991 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
992 |
< |
assertSame(TEST_STRING, it.next().get()); |
956 |
< |
} catch (ExecutionException success) { |
957 |
< |
} catch (Exception ex) { |
958 |
< |
unexpectedException(); |
989 |
> |
List<Future<String>> futures = e.invokeAll(l); |
990 |
> |
assertEquals(2, futures.size()); |
991 |
> |
for (Future<String> future : futures) |
992 |
> |
assertSame(TEST_STRING, future.get()); |
993 |
|
} finally { |
994 |
|
joinPool(e); |
995 |
|
} |
998 |
|
/** |
999 |
|
* timed invokeAny(null) throws NPE |
1000 |
|
*/ |
1001 |
< |
public void testTimedInvokeAny1() { |
1001 |
> |
public void testTimedInvokeAny1() throws Exception { |
1002 |
|
ExecutorService e = new CustomExecutor(2); |
1003 |
|
try { |
1004 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1004 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1005 |
> |
shouldThrow(); |
1006 |
|
} catch (NullPointerException success) { |
972 |
– |
} catch (Exception ex) { |
973 |
– |
unexpectedException(); |
1007 |
|
} finally { |
1008 |
|
joinPool(e); |
1009 |
|
} |
1012 |
|
/** |
1013 |
|
* timed invokeAny(,,null) throws NPE |
1014 |
|
*/ |
1015 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
1015 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1016 |
|
ExecutorService e = new CustomExecutor(2); |
1017 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1018 |
+ |
l.add(new StringTask()); |
1019 |
|
try { |
985 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
986 |
– |
l.add(new StringTask()); |
1020 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1021 |
+ |
shouldThrow(); |
1022 |
|
} catch (NullPointerException success) { |
989 |
– |
} catch (Exception ex) { |
990 |
– |
unexpectedException(); |
1023 |
|
} finally { |
1024 |
|
joinPool(e); |
1025 |
|
} |
1028 |
|
/** |
1029 |
|
* timed invokeAny(empty collection) throws IAE |
1030 |
|
*/ |
1031 |
< |
public void testTimedInvokeAny2() { |
1031 |
> |
public void testTimedInvokeAny2() throws Exception { |
1032 |
|
ExecutorService e = new CustomExecutor(2); |
1033 |
|
try { |
1034 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1034 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1035 |
> |
shouldThrow(); |
1036 |
|
} catch (IllegalArgumentException success) { |
1004 |
– |
} catch (Exception ex) { |
1005 |
– |
unexpectedException(); |
1037 |
|
} finally { |
1038 |
|
joinPool(e); |
1039 |
|
} |
1042 |
|
/** |
1043 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1044 |
|
*/ |
1045 |
< |
public void testTimedInvokeAny3() { |
1045 |
> |
public void testTimedInvokeAny3() throws Exception { |
1046 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1047 |
|
ExecutorService e = new CustomExecutor(2); |
1048 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1049 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1050 |
+ |
l.add(null); |
1051 |
|
try { |
1052 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1053 |
< |
l.add(new StringTask()); |
1019 |
< |
l.add(null); |
1020 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1052 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1053 |
> |
shouldThrow(); |
1054 |
|
} catch (NullPointerException success) { |
1022 |
– |
} catch (Exception ex) { |
1023 |
– |
ex.printStackTrace(); |
1024 |
– |
unexpectedException(); |
1055 |
|
} finally { |
1056 |
+ |
latch.countDown(); |
1057 |
|
joinPool(e); |
1058 |
|
} |
1059 |
|
} |
1061 |
|
/** |
1062 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1063 |
|
*/ |
1064 |
< |
public void testTimedInvokeAny4() { |
1064 |
> |
public void testTimedInvokeAny4() throws Exception { |
1065 |
|
ExecutorService e = new CustomExecutor(2); |
1066 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1067 |
+ |
l.add(new NPETask()); |
1068 |
|
try { |
1069 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1070 |
< |
l.add(new NPETask()); |
1038 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1069 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1070 |
> |
shouldThrow(); |
1071 |
|
} catch (ExecutionException success) { |
1072 |
< |
} catch (Exception ex) { |
1041 |
< |
unexpectedException(); |
1072 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1073 |
|
} finally { |
1074 |
|
joinPool(e); |
1075 |
|
} |
1078 |
|
/** |
1079 |
|
* timed invokeAny(c) returns result of some task |
1080 |
|
*/ |
1081 |
< |
public void testTimedInvokeAny5() { |
1081 |
> |
public void testTimedInvokeAny5() throws Exception { |
1082 |
|
ExecutorService e = new CustomExecutor(2); |
1083 |
|
try { |
1084 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1084 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1085 |
|
l.add(new StringTask()); |
1086 |
|
l.add(new StringTask()); |
1087 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1087 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1088 |
|
assertSame(TEST_STRING, result); |
1058 |
– |
} catch (ExecutionException success) { |
1059 |
– |
} catch (Exception ex) { |
1060 |
– |
unexpectedException(); |
1089 |
|
} finally { |
1090 |
|
joinPool(e); |
1091 |
|
} |
1094 |
|
/** |
1095 |
|
* timed invokeAll(null) throws NPE |
1096 |
|
*/ |
1097 |
< |
public void testTimedInvokeAll1() { |
1097 |
> |
public void testTimedInvokeAll1() throws Exception { |
1098 |
|
ExecutorService e = new CustomExecutor(2); |
1099 |
|
try { |
1100 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1100 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1101 |
> |
shouldThrow(); |
1102 |
|
} catch (NullPointerException success) { |
1074 |
– |
} catch (Exception ex) { |
1075 |
– |
unexpectedException(); |
1103 |
|
} finally { |
1104 |
|
joinPool(e); |
1105 |
|
} |
1108 |
|
/** |
1109 |
|
* timed invokeAll(,,null) throws NPE |
1110 |
|
*/ |
1111 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
1111 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1112 |
|
ExecutorService e = new CustomExecutor(2); |
1113 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1114 |
+ |
l.add(new StringTask()); |
1115 |
|
try { |
1087 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1088 |
– |
l.add(new StringTask()); |
1116 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1117 |
+ |
shouldThrow(); |
1118 |
|
} catch (NullPointerException success) { |
1091 |
– |
} catch (Exception ex) { |
1092 |
– |
unexpectedException(); |
1119 |
|
} finally { |
1120 |
|
joinPool(e); |
1121 |
|
} |
1124 |
|
/** |
1125 |
|
* timed invokeAll(empty collection) returns empty collection |
1126 |
|
*/ |
1127 |
< |
public void testTimedInvokeAll2() { |
1127 |
> |
public void testTimedInvokeAll2() throws Exception { |
1128 |
|
ExecutorService e = new CustomExecutor(2); |
1129 |
|
try { |
1130 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1130 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1131 |
|
assertTrue(r.isEmpty()); |
1106 |
– |
} catch (Exception ex) { |
1107 |
– |
unexpectedException(); |
1132 |
|
} finally { |
1133 |
|
joinPool(e); |
1134 |
|
} |
1137 |
|
/** |
1138 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1139 |
|
*/ |
1140 |
< |
public void testTimedInvokeAll3() { |
1140 |
> |
public void testTimedInvokeAll3() throws Exception { |
1141 |
|
ExecutorService e = new CustomExecutor(2); |
1142 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1143 |
+ |
l.add(new StringTask()); |
1144 |
+ |
l.add(null); |
1145 |
|
try { |
1146 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1147 |
< |
l.add(new StringTask()); |
1121 |
< |
l.add(null); |
1122 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1146 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1147 |
> |
shouldThrow(); |
1148 |
|
} catch (NullPointerException success) { |
1124 |
– |
} catch (Exception ex) { |
1125 |
– |
unexpectedException(); |
1149 |
|
} finally { |
1150 |
|
joinPool(e); |
1151 |
|
} |
1154 |
|
/** |
1155 |
|
* get of element of invokeAll(c) throws exception on failed task |
1156 |
|
*/ |
1157 |
< |
public void testTimedInvokeAll4() { |
1157 |
> |
public void testTimedInvokeAll4() throws Exception { |
1158 |
|
ExecutorService e = new CustomExecutor(2); |
1159 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1160 |
+ |
l.add(new NPETask()); |
1161 |
+ |
List<Future<String>> futures = |
1162 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1163 |
+ |
assertEquals(1, futures.size()); |
1164 |
|
try { |
1165 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1166 |
< |
l.add(new NPETask()); |
1139 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1140 |
< |
assertEquals(1, result.size()); |
1141 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1142 |
< |
it.next().get(); |
1165 |
> |
futures.get(0).get(); |
1166 |
> |
shouldThrow(); |
1167 |
|
} catch (ExecutionException success) { |
1168 |
< |
} catch (Exception ex) { |
1145 |
< |
unexpectedException(); |
1168 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1169 |
|
} finally { |
1170 |
|
joinPool(e); |
1171 |
|
} |
1174 |
|
/** |
1175 |
|
* timed invokeAll(c) returns results of all completed tasks |
1176 |
|
*/ |
1177 |
< |
public void testTimedInvokeAll5() { |
1177 |
> |
public void testTimedInvokeAll5() throws Exception { |
1178 |
|
ExecutorService e = new CustomExecutor(2); |
1179 |
|
try { |
1180 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1180 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1181 |
|
l.add(new StringTask()); |
1182 |
|
l.add(new StringTask()); |
1183 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1184 |
< |
assertEquals(2, result.size()); |
1185 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1186 |
< |
assertSame(TEST_STRING, it.next().get()); |
1187 |
< |
} catch (ExecutionException success) { |
1165 |
< |
} catch (Exception ex) { |
1166 |
< |
unexpectedException(); |
1183 |
> |
List<Future<String>> futures = |
1184 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1185 |
> |
assertEquals(2, futures.size()); |
1186 |
> |
for (Future<String> future : futures) |
1187 |
> |
assertSame(TEST_STRING, future.get()); |
1188 |
|
} finally { |
1189 |
|
joinPool(e); |
1190 |
|
} |
1193 |
|
/** |
1194 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1195 |
|
*/ |
1196 |
< |
public void testTimedInvokeAll6() { |
1196 |
> |
public void testTimedInvokeAll6() throws Exception { |
1197 |
|
ExecutorService e = new CustomExecutor(2); |
1198 |
|
try { |
1199 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1199 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1200 |
|
l.add(new StringTask()); |
1201 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1202 |
|
l.add(new StringTask()); |
1203 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1204 |
< |
assertEquals(3, result.size()); |
1205 |
< |
Iterator<Future<String>> it = result.iterator(); |
1203 |
> |
List<Future<String>> futures = |
1204 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1205 |
> |
assertEquals(3, futures.size()); |
1206 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1207 |
|
Future<String> f1 = it.next(); |
1208 |
|
Future<String> f2 = it.next(); |
1209 |
|
Future<String> f3 = it.next(); |
1212 |
|
assertTrue(f3.isDone()); |
1213 |
|
assertFalse(f1.isCancelled()); |
1214 |
|
assertTrue(f2.isCancelled()); |
1193 |
– |
} catch (Exception ex) { |
1194 |
– |
unexpectedException(); |
1215 |
|
} finally { |
1216 |
|
joinPool(e); |
1217 |
|
} |
1218 |
|
} |
1219 |
|
|
1200 |
– |
|
1220 |
|
} |