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