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