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