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(){ |
445 |
< |
public void run(){} |
446 |
< |
}, 100, 100, TimeUnit.SECONDS); |
250 |
< |
fail("should throw"); |
251 |
< |
}catch(RejectedExecutionException e){} |
440 |
> |
public void testGetThreadFactory() throws InterruptedException { |
441 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
442 |
> |
final ScheduledThreadPoolExecutor p = |
443 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
444 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
445 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
446 |
> |
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* 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 |
450 |
> |
* setThreadFactory sets the thread factory returned by getThreadFactory |
451 |
|
*/ |
452 |
< |
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
453 |
< |
try{ |
454 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
455 |
< |
se.shutdown(); |
456 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
457 |
< |
public void run(){} |
458 |
< |
}, 1, 100, TimeUnit.SECONDS); |
266 |
< |
fail("should throw"); |
267 |
< |
}catch(RejectedExecutionException e){} |
452 |
> |
public void testSetThreadFactory() throws InterruptedException { |
453 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
454 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
455 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
456 |
> |
p.setThreadFactory(threadFactory); |
457 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
458 |
> |
} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* Test to verify execute throws RejectedExecutionException |
272 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
462 |
> |
* setThreadFactory(null) throws NPE |
463 |
|
*/ |
464 |
< |
public void testExecute_RejectedExecutionException(){ |
465 |
< |
try{ |
466 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
467 |
< |
se.shutdown(); |
468 |
< |
se.execute(new Runnable(){ |
469 |
< |
public void run(){} |
470 |
< |
}); |
471 |
< |
fail("should throw"); |
282 |
< |
}catch(RejectedExecutionException e){} |
464 |
> |
public void testSetThreadFactoryNull() throws InterruptedException { |
465 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
466 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
467 |
> |
try { |
468 |
> |
p.setThreadFactory(null); |
469 |
> |
shouldThrow(); |
470 |
> |
} catch (NullPointerException success) {} |
471 |
> |
} |
472 |
|
} |
473 |
|
|
474 |
+ |
/** |
475 |
+ |
* isShutdown is false before shutdown, true after |
476 |
+ |
*/ |
477 |
+ |
public void testIsShutdown() { |
478 |
|
|
479 |
+ |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
480 |
+ |
try { |
481 |
+ |
assertFalse(p.isShutdown()); |
482 |
+ |
} |
483 |
+ |
finally { |
484 |
+ |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
485 |
+ |
} |
486 |
+ |
assertTrue(p.isShutdown()); |
487 |
+ |
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* Test to verify getActiveCount gives correct values |
490 |
> |
* isTerminated is false before termination, true after |
491 |
|
*/ |
492 |
< |
public void testGetActiveCount(){ |
493 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
494 |
< |
try { |
495 |
< |
assertEquals(0, two.getActiveCount()); |
496 |
< |
two.execute(newRunnable()); |
497 |
< |
try{ |
498 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
499 |
< |
} catch(Exception e){ |
500 |
< |
fail("unexpected exception"); |
492 |
> |
public void testIsTerminated() throws InterruptedException { |
493 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
494 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
495 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
496 |
> |
final CountDownLatch done = new CountDownLatch(1); |
497 |
> |
assertFalse(p.isTerminated()); |
498 |
> |
p.execute(new CheckedRunnable() { |
499 |
> |
public void realRun() throws InterruptedException { |
500 |
> |
assertFalse(p.isTerminated()); |
501 |
> |
threadStarted.countDown(); |
502 |
> |
done.await(); |
503 |
> |
}}); |
504 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
505 |
> |
assertFalse(p.isTerminating()); |
506 |
> |
done.countDown(); |
507 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
508 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
509 |
> |
assertTrue(p.isTerminated()); |
510 |
> |
} |
511 |
> |
} |
512 |
> |
|
513 |
> |
/** |
514 |
> |
* isTerminating is not true when running or when terminated |
515 |
> |
*/ |
516 |
> |
public void testIsTerminating() throws InterruptedException { |
517 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
518 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
519 |
> |
final CountDownLatch done = new CountDownLatch(1); |
520 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
521 |
> |
assertFalse(p.isTerminating()); |
522 |
> |
p.execute(new CheckedRunnable() { |
523 |
> |
public void realRun() throws InterruptedException { |
524 |
> |
assertFalse(p.isTerminating()); |
525 |
> |
threadStarted.countDown(); |
526 |
> |
done.await(); |
527 |
> |
}}); |
528 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
529 |
> |
assertFalse(p.isTerminating()); |
530 |
> |
done.countDown(); |
531 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
532 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
533 |
> |
assertTrue(p.isTerminated()); |
534 |
> |
assertFalse(p.isTerminating()); |
535 |
> |
} |
536 |
> |
} |
537 |
> |
|
538 |
> |
/** |
539 |
> |
* getQueue returns the work queue, which contains queued tasks |
540 |
> |
*/ |
541 |
> |
public void testGetQueue() throws InterruptedException { |
542 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
543 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
544 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
545 |
> |
final CountDownLatch done = new CountDownLatch(1); |
546 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
547 |
> |
for (int i = 0; i < tasks.length; i++) { |
548 |
> |
Runnable r = new CheckedRunnable() { |
549 |
> |
public void realRun() throws InterruptedException { |
550 |
> |
threadStarted.countDown(); |
551 |
> |
done.await(); |
552 |
> |
}}; |
553 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
554 |
|
} |
555 |
< |
assertEquals(1, two.getActiveCount()); |
556 |
< |
} finally { |
557 |
< |
two.shutdown(); |
555 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
556 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
557 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
558 |
> |
assertFalse(q.contains(tasks[0])); |
559 |
> |
done.countDown(); |
560 |
|
} |
561 |
|
} |
562 |
< |
|
562 |
> |
|
563 |
|
/** |
564 |
< |
* Test to verify getCompleteTaskCount gives correct values |
565 |
< |
*/ |
566 |
< |
public void testGetCompletedTaskCount(){ |
567 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
568 |
< |
try { |
569 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
570 |
< |
two.execute(newRunnable()); |
571 |
< |
try{ |
572 |
< |
Thread.sleep(MEDIUM_DELAY_MS/2); |
573 |
< |
} catch(Exception e){ |
574 |
< |
fail("unexpected exception"); |
564 |
> |
* remove(task) removes queued task, and fails to remove active task |
565 |
> |
*/ |
566 |
> |
public void testRemove() throws InterruptedException { |
567 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
568 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
569 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
570 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
571 |
> |
final CountDownLatch done = new CountDownLatch(1); |
572 |
> |
for (int i = 0; i < tasks.length; i++) { |
573 |
> |
Runnable r = new CheckedRunnable() { |
574 |
> |
public void realRun() throws InterruptedException { |
575 |
> |
threadStarted.countDown(); |
576 |
> |
done.await(); |
577 |
> |
}}; |
578 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
579 |
|
} |
580 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
581 |
< |
} finally { |
582 |
< |
two.shutdown(); |
580 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
581 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
582 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
583 |
> |
assertTrue(q.contains((Runnable)tasks[4])); |
584 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
585 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
586 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
587 |
> |
assertFalse(q.contains((Runnable)tasks[4])); |
588 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
589 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
590 |
> |
assertFalse(q.contains((Runnable)tasks[3])); |
591 |
> |
done.countDown(); |
592 |
|
} |
593 |
|
} |
594 |
< |
|
594 |
> |
|
595 |
|
/** |
596 |
< |
* Test to verify getCorePoolSize gives correct values |
597 |
< |
*/ |
598 |
< |
public void testGetCorePoolSize(){ |
599 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
596 |
> |
* purge eventually removes cancelled tasks from the queue |
597 |
> |
*/ |
598 |
> |
public void testPurge() throws InterruptedException { |
599 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
600 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
601 |
> |
for (int i = 0; i < tasks.length; i++) |
602 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
603 |
> |
LONG_DELAY_MS, MILLISECONDS); |
604 |
|
try { |
605 |
< |
assertEquals(1, one.getCorePoolSize()); |
605 |
> |
int max = tasks.length; |
606 |
> |
if (tasks[4].cancel(true)) --max; |
607 |
> |
if (tasks[3].cancel(true)) --max; |
608 |
> |
// There must eventually be an interference-free point at |
609 |
> |
// which purge will not fail. (At worst, when queue is empty.) |
610 |
> |
long startTime = System.nanoTime(); |
611 |
> |
do { |
612 |
> |
p.purge(); |
613 |
> |
long count = p.getTaskCount(); |
614 |
> |
if (count == max) |
615 |
> |
return; |
616 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
617 |
> |
fail("Purge failed to remove cancelled tasks"); |
618 |
|
} finally { |
619 |
< |
one.shutdown(); |
619 |
> |
for (ScheduledFuture task : tasks) |
620 |
> |
task.cancel(true); |
621 |
> |
joinPool(p); |
622 |
|
} |
623 |
|
} |
624 |
< |
|
624 |
> |
|
625 |
|
/** |
626 |
< |
* Test to verify getLargestPoolSize gives correct values |
627 |
< |
*/ |
628 |
< |
public void testGetLargestPoolSize(){ |
629 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
626 |
> |
* shutdownNow returns a list containing tasks that were not run, |
627 |
> |
* and those tasks are drained from the queue |
628 |
> |
*/ |
629 |
> |
public void testShutdownNow() throws InterruptedException { |
630 |
> |
final int poolSize = 2; |
631 |
> |
final int count = 5; |
632 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
633 |
> |
final ScheduledThreadPoolExecutor p = |
634 |
> |
new ScheduledThreadPoolExecutor(poolSize); |
635 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
636 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
637 |
> |
threadsStarted.countDown(); |
638 |
> |
try { |
639 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
640 |
> |
} catch (InterruptedException success) {} |
641 |
> |
ran.getAndIncrement(); |
642 |
> |
}}; |
643 |
> |
for (int i = 0; i < count; i++) |
644 |
> |
p.execute(waiter); |
645 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
646 |
> |
assertEquals(poolSize, p.getActiveCount()); |
647 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
648 |
> |
final List<Runnable> queuedTasks; |
649 |
|
try { |
650 |
< |
assertEquals(0, two.getLargestPoolSize()); |
651 |
< |
two.execute(newRunnable()); |
652 |
< |
two.execute(newRunnable()); |
653 |
< |
try{ |
654 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
655 |
< |
} catch(Exception e){ |
656 |
< |
fail("unexpected exception"); |
650 |
> |
queuedTasks = p.shutdownNow(); |
651 |
> |
} catch (SecurityException ok) { |
652 |
> |
return; // Allowed in case test doesn't have privs |
653 |
> |
} |
654 |
> |
assertTrue(p.isShutdown()); |
655 |
> |
assertTrue(p.getQueue().isEmpty()); |
656 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
657 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
658 |
> |
assertTrue(p.isTerminated()); |
659 |
> |
assertEquals(poolSize, ran.get()); |
660 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
661 |
> |
} |
662 |
> |
|
663 |
> |
/** |
664 |
> |
* shutdownNow returns a list containing tasks that were not run, |
665 |
> |
* and those tasks are drained from the queue |
666 |
> |
*/ |
667 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
668 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
669 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
670 |
> |
for (int i = 0; i < 3; i++) { |
671 |
> |
Runnable r = new NoOpRunnable(); |
672 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
673 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
674 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
675 |
> |
} |
676 |
> |
if (testImplementationDetails) |
677 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
678 |
> |
final List<Runnable> queuedTasks; |
679 |
> |
try { |
680 |
> |
queuedTasks = p.shutdownNow(); |
681 |
> |
} catch (SecurityException ok) { |
682 |
> |
return; // Allowed in case test doesn't have privs |
683 |
> |
} |
684 |
> |
assertTrue(p.isShutdown()); |
685 |
> |
assertTrue(p.getQueue().isEmpty()); |
686 |
> |
if (testImplementationDetails) |
687 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
688 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
689 |
> |
for (ScheduledFuture task : tasks) { |
690 |
> |
assertFalse(task.isDone()); |
691 |
> |
assertFalse(task.isCancelled()); |
692 |
> |
} |
693 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
694 |
> |
assertTrue(p.isTerminated()); |
695 |
> |
} |
696 |
> |
|
697 |
> |
/** |
698 |
> |
* By default, periodic tasks are cancelled at shutdown. |
699 |
> |
* By default, delayed tasks keep running after shutdown. |
700 |
> |
* Check that changing the default values work: |
701 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
702 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
703 |
> |
*/ |
704 |
> |
public void testShutdown_cancellation() throws Exception { |
705 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
706 |
> |
for (Boolean policy : allBooleans) |
707 |
> |
{ |
708 |
> |
final int poolSize = 2; |
709 |
> |
final ScheduledThreadPoolExecutor p |
710 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
711 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
712 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
713 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
714 |
> |
if (policy != null) { |
715 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
716 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
717 |
> |
p.setRemoveOnCancelPolicy(policy); |
718 |
> |
} |
719 |
> |
assertEquals(effectiveDelayedPolicy, |
720 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
721 |
> |
assertEquals(effectivePeriodicPolicy, |
722 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
723 |
> |
assertEquals(effectiveRemovePolicy, |
724 |
> |
p.getRemoveOnCancelPolicy()); |
725 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
726 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
727 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
728 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
729 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
730 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
731 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
732 |
> |
throws InterruptedException { |
733 |
> |
poolBlocked.countDown(); |
734 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
735 |
> |
ran.getAndIncrement(); |
736 |
> |
}}; |
737 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
738 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
739 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
740 |
> |
for (int i = 0; i < poolSize; i++) |
741 |
> |
blockers.add(p.submit(task)); |
742 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
743 |
> |
|
744 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
745 |
> |
1, 1, MILLISECONDS)); |
746 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
747 |
> |
1, 1, MILLISECONDS)); |
748 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
749 |
> |
|
750 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
751 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
752 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
753 |
> |
assertTrue(p.isShutdown()); |
754 |
> |
assertFalse(p.isTerminated()); |
755 |
> |
for (Future<?> periodic : periodics) { |
756 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
757 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
758 |
> |
} |
759 |
> |
for (Future<?> delayed : delayeds) { |
760 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
761 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
762 |
> |
} |
763 |
> |
if (testImplementationDetails) { |
764 |
> |
assertEquals(effectivePeriodicPolicy, |
765 |
> |
p.getQueue().containsAll(periodics)); |
766 |
> |
assertEquals(effectiveDelayedPolicy, |
767 |
> |
p.getQueue().containsAll(delayeds)); |
768 |
> |
} |
769 |
> |
// Release all pool threads |
770 |
> |
unblock.countDown(); |
771 |
> |
|
772 |
> |
for (Future<?> delayed : delayeds) { |
773 |
> |
if (effectiveDelayedPolicy) { |
774 |
> |
assertNull(delayed.get()); |
775 |
|
} |
776 |
< |
assertEquals(2, two.getLargestPoolSize()); |
777 |
< |
} finally { |
778 |
< |
two.shutdown(); |
776 |
> |
} |
777 |
> |
if (effectivePeriodicPolicy) { |
778 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
779 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
780 |
> |
for (Future<?> periodic : periodics) { |
781 |
> |
assertTrue(periodic.cancel(false)); |
782 |
> |
assertTrue(periodic.isCancelled()); |
783 |
> |
assertTrue(periodic.isDone()); |
784 |
> |
} |
785 |
> |
} |
786 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(p.isTerminated()); |
788 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
789 |
> |
}} |
790 |
> |
|
791 |
> |
/** |
792 |
> |
* completed submit of callable returns result |
793 |
> |
*/ |
794 |
> |
public void testSubmitCallable() throws Exception { |
795 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
796 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
797 |
> |
Future<String> future = e.submit(new StringTask()); |
798 |
> |
String result = future.get(); |
799 |
> |
assertSame(TEST_STRING, result); |
800 |
|
} |
801 |
|
} |
802 |
< |
|
802 |
> |
|
803 |
|
/** |
804 |
< |
* Test to verify getPoolSize gives correct values |
804 |
> |
* completed submit of runnable returns successfully |
805 |
|
*/ |
806 |
< |
public void testGetPoolSize(){ |
807 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
808 |
< |
try { |
809 |
< |
assertEquals(0, one.getPoolSize()); |
810 |
< |
one.execute(newRunnable()); |
811 |
< |
assertEquals(1, one.getPoolSize()); |
366 |
< |
} finally { |
367 |
< |
one.shutdown(); |
806 |
> |
public void testSubmitRunnable() throws Exception { |
807 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
808 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
809 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
810 |
> |
future.get(); |
811 |
> |
assertTrue(future.isDone()); |
812 |
|
} |
813 |
|
} |
814 |
< |
|
814 |
> |
|
815 |
|
/** |
816 |
< |
* Test to verify getTaskCount gives correct values |
816 |
> |
* completed submit of (runnable, result) returns result |
817 |
|
*/ |
818 |
< |
public void testGetTaskCount(){ |
819 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
820 |
< |
try { |
821 |
< |
assertEquals(0, one.getTaskCount()); |
822 |
< |
for(int i = 0; i < 5; i++) |
823 |
< |
one.execute(newRunnable()); |
380 |
< |
try{ |
381 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
382 |
< |
} catch(Exception e){ |
383 |
< |
fail("unexpected exception"); |
384 |
< |
} |
385 |
< |
assertEquals(5, one.getTaskCount()); |
386 |
< |
} finally { |
387 |
< |
one.shutdown(); |
818 |
> |
public void testSubmitRunnable2() throws Exception { |
819 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
820 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
821 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
822 |
> |
String result = future.get(); |
823 |
> |
assertSame(TEST_STRING, result); |
824 |
|
} |
825 |
|
} |
826 |
< |
|
826 |
> |
|
827 |
|
/** |
828 |
< |
* Test to verify isShutDown gives correct values |
828 |
> |
* invokeAny(null) throws NPE |
829 |
|
*/ |
830 |
< |
public void testIsShutdown(){ |
831 |
< |
|
832 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
833 |
< |
try { |
834 |
< |
assertFalse(one.isShutdown()); |
830 |
> |
public void testInvokeAny1() throws Exception { |
831 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
832 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
833 |
> |
try { |
834 |
> |
e.invokeAny(null); |
835 |
> |
shouldThrow(); |
836 |
> |
} catch (NullPointerException success) {} |
837 |
|
} |
838 |
< |
finally { |
839 |
< |
one.shutdown(); |
838 |
> |
} |
839 |
> |
|
840 |
> |
/** |
841 |
> |
* invokeAny(empty collection) throws IAE |
842 |
> |
*/ |
843 |
> |
public void testInvokeAny2() throws Exception { |
844 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
845 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
846 |
> |
try { |
847 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
848 |
> |
shouldThrow(); |
849 |
> |
} catch (IllegalArgumentException success) {} |
850 |
|
} |
403 |
– |
assertTrue(one.isShutdown()); |
851 |
|
} |
852 |
|
|
406 |
– |
|
853 |
|
/** |
854 |
< |
* Test to verify isTerminated gives correct values |
409 |
< |
* Makes sure termination does not take an innapropriate |
410 |
< |
* amount of time |
854 |
> |
* invokeAny(c) throws NPE if c has null elements |
855 |
|
*/ |
856 |
< |
public void testIsTerminated(){ |
857 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
858 |
< |
try { |
859 |
< |
one.execute(newRunnable()); |
860 |
< |
} finally { |
861 |
< |
one.shutdown(); |
856 |
> |
public void testInvokeAny3() throws Exception { |
857 |
> |
CountDownLatch latch = new CountDownLatch(1); |
858 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
859 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
860 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
861 |
> |
l.add(latchAwaitingStringTask(latch)); |
862 |
> |
l.add(null); |
863 |
> |
try { |
864 |
> |
e.invokeAny(l); |
865 |
> |
shouldThrow(); |
866 |
> |
} catch (NullPointerException success) {} |
867 |
> |
latch.countDown(); |
868 |
|
} |
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"); |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
< |
* Test to verify that purge correctly removes cancelled tasks |
432 |
< |
* from the queue |
872 |
> |
* invokeAny(c) throws ExecutionException if no task completes |
873 |
|
*/ |
874 |
< |
public void testPurge(){ |
875 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
876 |
< |
try { |
877 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
878 |
< |
for(int i = 0; i < 5; i++){ |
879 |
< |
tasks[i] = one.schedule(newRunnable(), 1, TimeUnit.MILLISECONDS); |
874 |
> |
public void testInvokeAny4() throws Exception { |
875 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
876 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
877 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
878 |
> |
l.add(new NPETask()); |
879 |
> |
try { |
880 |
> |
e.invokeAny(l); |
881 |
> |
shouldThrow(); |
882 |
> |
} catch (ExecutionException success) { |
883 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
884 |
|
} |
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(); |
885 |
|
} |
886 |
|
} |
887 |
|
|
888 |
|
/** |
889 |
< |
* Test to verify shutDownNow returns a list |
454 |
< |
* containing the correct number of elements |
889 |
> |
* invokeAny(c) returns result of some task |
890 |
|
*/ |
891 |
< |
public void testShutDownNow(){ |
892 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
893 |
< |
for(int i = 0; i < 5; i++) |
894 |
< |
one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
895 |
< |
List l = one.shutdownNow(); |
896 |
< |
assertTrue(one.isShutdown()); |
897 |
< |
assertTrue(l.size() > 0 && l.size() <= 5); |
891 |
> |
public void testInvokeAny5() throws Exception { |
892 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
893 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
894 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
895 |
> |
l.add(new StringTask()); |
896 |
> |
l.add(new StringTask()); |
897 |
> |
String result = e.invokeAny(l); |
898 |
> |
assertSame(TEST_STRING, result); |
899 |
> |
} |
900 |
|
} |
901 |
|
|
902 |
< |
public void testShutDown1(){ |
903 |
< |
try { |
904 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
905 |
< |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
906 |
< |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
902 |
> |
/** |
903 |
> |
* invokeAll(null) throws NPE |
904 |
> |
*/ |
905 |
> |
public void testInvokeAll1() throws Exception { |
906 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
907 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
908 |
> |
try { |
909 |
> |
e.invokeAll(null); |
910 |
> |
shouldThrow(); |
911 |
> |
} catch (NullPointerException success) {} |
912 |
> |
} |
913 |
> |
} |
914 |
|
|
915 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
916 |
< |
for(int i = 0; i < 5; i++) |
917 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
918 |
< |
one.shutdown(); |
919 |
< |
BlockingQueue q = one.getQueue(); |
920 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
921 |
< |
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
922 |
< |
assertFalse(t.isCancelled()); |
923 |
< |
} |
924 |
< |
assertTrue(one.isShutdown()); |
925 |
< |
Thread.sleep(SHORT_DELAY_MS); |
926 |
< |
for (int i = 0; i < 5; ++i) { |
927 |
< |
assertTrue(tasks[i].isDone()); |
928 |
< |
assertFalse(tasks[i].isCancelled()); |
915 |
> |
/** |
916 |
> |
* invokeAll(empty collection) returns empty collection |
917 |
> |
*/ |
918 |
> |
public void testInvokeAll2() throws Exception { |
919 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
920 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
921 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
922 |
> |
assertTrue(r.isEmpty()); |
923 |
> |
} |
924 |
> |
} |
925 |
> |
|
926 |
> |
/** |
927 |
> |
* invokeAll(c) throws NPE if c has null elements |
928 |
> |
*/ |
929 |
> |
public void testInvokeAll3() throws Exception { |
930 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
931 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
932 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
933 |
> |
l.add(new StringTask()); |
934 |
> |
l.add(null); |
935 |
> |
try { |
936 |
> |
e.invokeAll(l); |
937 |
> |
shouldThrow(); |
938 |
> |
} catch (NullPointerException success) {} |
939 |
> |
} |
940 |
> |
} |
941 |
> |
|
942 |
> |
/** |
943 |
> |
* get of invokeAll(c) throws exception on failed task |
944 |
> |
*/ |
945 |
> |
public void testInvokeAll4() throws Exception { |
946 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
947 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
948 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
949 |
> |
l.add(new NPETask()); |
950 |
> |
List<Future<String>> futures = e.invokeAll(l); |
951 |
> |
assertEquals(1, futures.size()); |
952 |
> |
try { |
953 |
> |
futures.get(0).get(); |
954 |
> |
shouldThrow(); |
955 |
> |
} catch (ExecutionException success) { |
956 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
957 |
|
} |
486 |
– |
|
958 |
|
} |
959 |
< |
catch(Exception ex) { |
960 |
< |
fail("unexpected exception"); |
959 |
> |
} |
960 |
> |
|
961 |
> |
/** |
962 |
> |
* invokeAll(c) returns results of all completed tasks |
963 |
> |
*/ |
964 |
> |
public void testInvokeAll5() throws Exception { |
965 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
966 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
967 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
968 |
> |
l.add(new StringTask()); |
969 |
> |
l.add(new StringTask()); |
970 |
> |
List<Future<String>> futures = e.invokeAll(l); |
971 |
> |
assertEquals(2, futures.size()); |
972 |
> |
for (Future<String> future : futures) |
973 |
> |
assertSame(TEST_STRING, future.get()); |
974 |
|
} |
975 |
|
} |
976 |
|
|
977 |
+ |
/** |
978 |
+ |
* timed invokeAny(null) throws NPE |
979 |
+ |
*/ |
980 |
+ |
public void testTimedInvokeAny1() throws Exception { |
981 |
+ |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
982 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
983 |
+ |
try { |
984 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
985 |
+ |
shouldThrow(); |
986 |
+ |
} catch (NullPointerException success) {} |
987 |
+ |
} |
988 |
+ |
} |
989 |
|
|
990 |
< |
public void testShutDown2(){ |
991 |
< |
try { |
992 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
993 |
< |
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
994 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
995 |
< |
for(int i = 0; i < 5; i++) |
996 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
997 |
< |
one.shutdown(); |
998 |
< |
assertTrue(one.isShutdown()); |
999 |
< |
BlockingQueue q = one.getQueue(); |
1000 |
< |
assertTrue(q.isEmpty()); |
1001 |
< |
Thread.sleep(SHORT_DELAY_MS); |
506 |
< |
assertTrue(one.isTerminated()); |
990 |
> |
/** |
991 |
> |
* timed invokeAny(,,null) throws NPE |
992 |
> |
*/ |
993 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
994 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
995 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
996 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
997 |
> |
l.add(new StringTask()); |
998 |
> |
try { |
999 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1000 |
> |
shouldThrow(); |
1001 |
> |
} catch (NullPointerException success) {} |
1002 |
|
} |
1003 |
< |
catch(Exception ex) { |
1004 |
< |
fail("unexpected exception"); |
1003 |
> |
} |
1004 |
> |
|
1005 |
> |
/** |
1006 |
> |
* timed invokeAny(empty collection) throws IAE |
1007 |
> |
*/ |
1008 |
> |
public void testTimedInvokeAny2() throws Exception { |
1009 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1010 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1011 |
> |
try { |
1012 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1013 |
> |
shouldThrow(); |
1014 |
> |
} catch (IllegalArgumentException success) {} |
1015 |
|
} |
1016 |
|
} |
1017 |
|
|
1018 |
+ |
/** |
1019 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1020 |
+ |
*/ |
1021 |
+ |
public void testTimedInvokeAny3() throws Exception { |
1022 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
1023 |
+ |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1024 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
1025 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1026 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1027 |
+ |
l.add(null); |
1028 |
+ |
try { |
1029 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1030 |
+ |
shouldThrow(); |
1031 |
+ |
} catch (NullPointerException success) {} |
1032 |
+ |
latch.countDown(); |
1033 |
+ |
} |
1034 |
+ |
} |
1035 |
|
|
1036 |
< |
public void testShutDown3(){ |
1037 |
< |
try { |
1038 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
1039 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
1040 |
< |
ScheduledCancellable task = |
1041 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
1042 |
< |
one.shutdown(); |
1043 |
< |
assertTrue(one.isShutdown()); |
1044 |
< |
BlockingQueue q = one.getQueue(); |
1045 |
< |
assertTrue(q.isEmpty()); |
1046 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1047 |
< |
assertTrue(one.isTerminated()); |
1036 |
> |
/** |
1037 |
> |
* timed invokeAny(c) throws ExecutionException if no task completes |
1038 |
> |
*/ |
1039 |
> |
public void testTimedInvokeAny4() throws Exception { |
1040 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1041 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1042 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1043 |
> |
l.add(new NPETask()); |
1044 |
> |
try { |
1045 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1046 |
> |
shouldThrow(); |
1047 |
> |
} catch (ExecutionException success) { |
1048 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1049 |
> |
} |
1050 |
|
} |
1051 |
< |
catch(Exception ex) { |
1052 |
< |
fail("unexpected exception"); |
1051 |
> |
} |
1052 |
> |
|
1053 |
> |
/** |
1054 |
> |
* timed invokeAny(c) returns result of some task |
1055 |
> |
*/ |
1056 |
> |
public void testTimedInvokeAny5() throws Exception { |
1057 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1058 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1059 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1060 |
> |
l.add(new StringTask()); |
1061 |
> |
l.add(new StringTask()); |
1062 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1063 |
> |
assertSame(TEST_STRING, result); |
1064 |
|
} |
1065 |
|
} |
1066 |
|
|
1067 |
< |
public void testShutDown4(){ |
1068 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
1069 |
< |
try { |
1070 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
1071 |
< |
ScheduledCancellable task = |
1072 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
1073 |
< |
assertFalse(task.isCancelled()); |
1074 |
< |
one.shutdown(); |
1075 |
< |
assertFalse(task.isCancelled()); |
1076 |
< |
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()); |
1067 |
> |
/** |
1068 |
> |
* timed invokeAll(null) throws NPE |
1069 |
> |
*/ |
1070 |
> |
public void testTimedInvokeAll1() throws Exception { |
1071 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1072 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1073 |
> |
try { |
1074 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1075 |
> |
shouldThrow(); |
1076 |
> |
} catch (NullPointerException success) {} |
1077 |
|
} |
1078 |
< |
catch(Exception ex) { |
1079 |
< |
fail("unexpected exception"); |
1078 |
> |
} |
1079 |
> |
|
1080 |
> |
/** |
1081 |
> |
* timed invokeAll(,,null) throws NPE |
1082 |
> |
*/ |
1083 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1084 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1085 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1086 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1087 |
> |
l.add(new StringTask()); |
1088 |
> |
try { |
1089 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1090 |
> |
shouldThrow(); |
1091 |
> |
} catch (NullPointerException success) {} |
1092 |
|
} |
1093 |
< |
finally { |
1094 |
< |
one.shutdownNow(); |
1093 |
> |
} |
1094 |
> |
|
1095 |
> |
/** |
1096 |
> |
* timed invokeAll(empty collection) returns empty collection |
1097 |
> |
*/ |
1098 |
> |
public void testTimedInvokeAll2() throws Exception { |
1099 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1100 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1101 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1102 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1103 |
> |
assertTrue(r.isEmpty()); |
1104 |
> |
} |
1105 |
> |
} |
1106 |
> |
|
1107 |
> |
/** |
1108 |
> |
* timed invokeAll(c) throws NPE if c has null elements |
1109 |
> |
*/ |
1110 |
> |
public void testTimedInvokeAll3() throws Exception { |
1111 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1112 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1113 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1114 |
> |
l.add(new StringTask()); |
1115 |
> |
l.add(null); |
1116 |
> |
try { |
1117 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1118 |
> |
shouldThrow(); |
1119 |
> |
} catch (NullPointerException success) {} |
1120 |
> |
} |
1121 |
> |
} |
1122 |
> |
|
1123 |
> |
/** |
1124 |
> |
* get of element of invokeAll(c) throws exception on failed task |
1125 |
> |
*/ |
1126 |
> |
public void testTimedInvokeAll4() throws Exception { |
1127 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1128 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1129 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
> |
l.add(new NPETask()); |
1131 |
> |
List<Future<String>> futures = |
1132 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1133 |
> |
assertEquals(1, futures.size()); |
1134 |
> |
try { |
1135 |
> |
futures.get(0).get(); |
1136 |
> |
shouldThrow(); |
1137 |
> |
} catch (ExecutionException success) { |
1138 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1139 |
> |
} |
1140 |
> |
} |
1141 |
> |
} |
1142 |
> |
|
1143 |
> |
/** |
1144 |
> |
* timed invokeAll(c) returns results of all completed tasks |
1145 |
> |
*/ |
1146 |
> |
public void testTimedInvokeAll5() throws Exception { |
1147 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1148 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1149 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1150 |
> |
l.add(new StringTask()); |
1151 |
> |
l.add(new StringTask()); |
1152 |
> |
List<Future<String>> futures = |
1153 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1154 |
> |
assertEquals(2, futures.size()); |
1155 |
> |
for (Future<String> future : futures) |
1156 |
> |
assertSame(TEST_STRING, future.get()); |
1157 |
> |
} |
1158 |
> |
} |
1159 |
> |
|
1160 |
> |
/** |
1161 |
> |
* timed invokeAll(c) cancels tasks not completed by timeout |
1162 |
> |
*/ |
1163 |
> |
public void testTimedInvokeAll6() throws Exception { |
1164 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1165 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1166 |
> |
for (long timeout = timeoutMillis();;) { |
1167 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1168 |
> |
tasks.add(new StringTask("0")); |
1169 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1170 |
> |
tasks.add(new StringTask("2")); |
1171 |
> |
long startTime = System.nanoTime(); |
1172 |
> |
List<Future<String>> futures = |
1173 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1174 |
> |
assertEquals(tasks.size(), futures.size()); |
1175 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1176 |
> |
for (Future future : futures) |
1177 |
> |
assertTrue(future.isDone()); |
1178 |
> |
assertTrue(futures.get(1).isCancelled()); |
1179 |
> |
try { |
1180 |
> |
assertEquals("0", futures.get(0).get()); |
1181 |
> |
assertEquals("2", futures.get(2).get()); |
1182 |
> |
break; |
1183 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1184 |
> |
timeout *= 2; |
1185 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1186 |
> |
fail("expected exactly one task to be cancelled"); |
1187 |
> |
} |
1188 |
> |
} |
1189 |
|
} |
1190 |
|
} |
1191 |
|
|