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