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