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