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