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