1 |
|
/* |
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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
16 |
|
|
17 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
29 |
|
static class DirectExecutorService extends AbstractExecutorService { |
30 |
|
public void execute(Runnable r) { r.run(); } |
31 |
|
public void shutdown() { shutdown = true; } |
32 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
32 |
> |
public List<Runnable> shutdownNow() { |
33 |
> |
shutdown = true; |
34 |
> |
return Collections.EMPTY_LIST; |
35 |
> |
} |
36 |
|
public boolean isShutdown() { return shutdown; } |
37 |
|
public boolean isTerminated() { return isShutdown(); } |
38 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
38 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
39 |
> |
return isShutdown(); |
40 |
> |
} |
41 |
|
private volatile boolean shutdown = false; |
42 |
|
} |
43 |
|
|
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* submit(runnable) throws RejectedExecutionException if |
169 |
< |
* executor is saturated. |
170 |
< |
*/ |
171 |
< |
public void testExecute1() { |
172 |
< |
ThreadPoolExecutor p = |
173 |
< |
new ThreadPoolExecutor(1, 1, |
174 |
< |
60, TimeUnit.SECONDS, |
175 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
176 |
< |
try { |
177 |
< |
for (int i = 0; i < 2; ++i) |
178 |
< |
p.submit(new MediumRunnable()); |
179 |
< |
for (int i = 0; i < 2; ++i) { |
180 |
< |
try { |
181 |
< |
p.submit(new MediumRunnable()); |
182 |
< |
shouldThrow(); |
183 |
< |
} catch (RejectedExecutionException success) {} |
184 |
< |
} |
185 |
< |
} finally { |
186 |
< |
joinPool(p); |
187 |
< |
} |
188 |
< |
} |
189 |
< |
|
190 |
< |
/** |
191 |
< |
* submit(callable) throws RejectedExecutionException |
192 |
< |
* if executor is saturated. |
173 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
174 |
|
*/ |
175 |
< |
public void testExecute2() { |
176 |
< |
ThreadPoolExecutor p = |
177 |
< |
new ThreadPoolExecutor(1, 1, |
178 |
< |
60, TimeUnit.SECONDS, |
179 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
175 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
176 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
177 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
178 |
> |
final ExecutorService p |
179 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
180 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
181 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
182 |
> |
public Void realCall() throws InterruptedException { |
183 |
> |
quittingTime.await(); |
184 |
> |
return null; |
185 |
> |
}}; |
186 |
|
try { |
187 |
< |
for (int i = 0; i < 2; ++i) |
188 |
< |
p.submit(new MediumRunnable()); |
189 |
< |
for (int i = 0; i < 2; ++i) { |
190 |
< |
try { |
191 |
< |
p.submit(new SmallCallable()); |
192 |
< |
shouldThrow(); |
193 |
< |
} catch (RejectedExecutionException success) {} |
194 |
< |
} |
187 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
188 |
> |
public void realRun() throws Exception { |
189 |
> |
Future<Void> future = p.submit(awaiter); |
190 |
> |
submitted.countDown(); |
191 |
> |
future.get(); |
192 |
> |
}}); |
193 |
> |
t.start(); |
194 |
> |
submitted.await(); |
195 |
> |
t.interrupt(); |
196 |
> |
t.join(); |
197 |
|
} finally { |
198 |
+ |
quittingTime.countDown(); |
199 |
|
joinPool(p); |
200 |
|
} |
201 |
|
} |
202 |
|
|
213 |
– |
|
214 |
– |
/** |
215 |
– |
* Blocking on submit(callable) throws InterruptedException if |
216 |
– |
* caller interrupted. |
217 |
– |
*/ |
218 |
– |
public void testInterruptedSubmit() throws InterruptedException { |
219 |
– |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
220 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
221 |
– |
public void realRun() throws Exception { |
222 |
– |
p.submit(new CheckedCallable<Object>() { |
223 |
– |
public Object realCall() |
224 |
– |
throws InterruptedException { |
225 |
– |
Thread.sleep(SMALL_DELAY_MS); |
226 |
– |
return null; |
227 |
– |
}}).get(); |
228 |
– |
}}); |
229 |
– |
|
230 |
– |
t.start(); |
231 |
– |
Thread.sleep(SHORT_DELAY_MS); |
232 |
– |
t.interrupt(); |
233 |
– |
joinPool(p); |
234 |
– |
} |
235 |
– |
|
236 |
– |
/** |
237 |
– |
* get of submitted callable throws InterruptedException if callable |
238 |
– |
* interrupted |
239 |
– |
*/ |
240 |
– |
public void testSubmitIE() throws InterruptedException { |
241 |
– |
final ThreadPoolExecutor p = |
242 |
– |
new ThreadPoolExecutor(1, 1, |
243 |
– |
60, TimeUnit.SECONDS, |
244 |
– |
new ArrayBlockingQueue<Runnable>(10)); |
245 |
– |
|
246 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
247 |
– |
public void realRun() throws Exception { |
248 |
– |
p.submit(new SmallCallable()).get(); |
249 |
– |
}}); |
250 |
– |
|
251 |
– |
t.start(); |
252 |
– |
Thread.sleep(SHORT_DELAY_MS); |
253 |
– |
t.interrupt(); |
254 |
– |
t.join(); |
255 |
– |
joinPool(p); |
256 |
– |
} |
257 |
– |
|
203 |
|
/** |
204 |
< |
* get of submit(callable) throws ExecutionException if callable |
205 |
< |
* throws exception |
204 |
> |
* get of submit(callable) throws ExecutionException if callable |
205 |
> |
* throws exception |
206 |
|
*/ |
207 |
|
public void testSubmitEE() throws InterruptedException { |
208 |
|
ThreadPoolExecutor p = |
225 |
|
/** |
226 |
|
* invokeAny(null) throws NPE |
227 |
|
*/ |
228 |
< |
public void testInvokeAny1() |
284 |
< |
throws InterruptedException, ExecutionException { |
228 |
> |
public void testInvokeAny1() throws Exception { |
229 |
|
ExecutorService e = new DirectExecutorService(); |
230 |
|
try { |
231 |
|
e.invokeAny(null); |
239 |
|
/** |
240 |
|
* invokeAny(empty collection) throws IAE |
241 |
|
*/ |
242 |
< |
public void testInvokeAny2() |
299 |
< |
throws InterruptedException, ExecutionException { |
242 |
> |
public void testInvokeAny2() throws Exception { |
243 |
|
ExecutorService e = new DirectExecutorService(); |
244 |
|
try { |
245 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
589 |
|
try { |
590 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
591 |
|
l.add(new StringTask()); |
592 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
592 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
593 |
|
l.add(new StringTask()); |
594 |
|
List<Future<String>> futures = |
595 |
< |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
595 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
596 |
|
assertEquals(3, futures.size()); |
597 |
|
Iterator<Future<String>> it = futures.iterator(); |
598 |
|
Future<String> f1 = it.next(); |
601 |
|
assertTrue(f1.isDone()); |
602 |
|
assertFalse(f1.isCancelled()); |
603 |
|
assertTrue(f2.isDone()); |
604 |
+ |
assertFalse(f2.isCancelled()); |
605 |
|
assertTrue(f3.isDone()); |
606 |
|
assertTrue(f3.isCancelled()); |
607 |
|
} finally { |