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 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.math.BigInteger; |
14 |
|
import java.security.*; |
15 |
|
|
16 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
28 |
|
static class DirectExecutorService extends AbstractExecutorService { |
29 |
|
public void execute(Runnable r) { r.run(); } |
30 |
|
public void shutdown() { shutdown = true; } |
31 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
31 |
> |
public List<Runnable> shutdownNow() { |
32 |
> |
shutdown = true; |
33 |
> |
return Collections.EMPTY_LIST; |
34 |
> |
} |
35 |
|
public boolean isShutdown() { return shutdown; } |
36 |
|
public boolean isTerminated() { return isShutdown(); } |
37 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
37 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
38 |
> |
return isShutdown(); |
39 |
> |
} |
40 |
|
private volatile boolean shutdown = false; |
41 |
|
} |
42 |
|
|
45 |
|
*/ |
46 |
|
public void testExecuteRunnable() throws Exception { |
47 |
|
ExecutorService e = new DirectExecutorService(); |
48 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
49 |
< |
assertFalse(task.done); |
48 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
49 |
> |
CheckedRunnable task = new CheckedRunnable() { |
50 |
> |
public void realRun() { |
51 |
> |
done.set(true); |
52 |
> |
}}; |
53 |
|
Future<?> future = e.submit(task); |
54 |
< |
future.get(); |
55 |
< |
assertTrue(task.done); |
54 |
> |
assertNull(future.get()); |
55 |
> |
assertNull(future.get(0, MILLISECONDS)); |
56 |
> |
assertTrue(done.get()); |
57 |
> |
assertTrue(future.isDone()); |
58 |
> |
assertFalse(future.isCancelled()); |
59 |
|
} |
60 |
|
|
51 |
– |
|
61 |
|
/** |
62 |
|
* Completed submit(callable) returns result |
63 |
|
*/ |
88 |
|
assertSame(TEST_STRING, result); |
89 |
|
} |
90 |
|
|
82 |
– |
|
91 |
|
/** |
92 |
|
* A submitted privileged action runs to completion |
93 |
|
*/ |
160 |
|
} catch (NullPointerException success) {} |
161 |
|
} |
162 |
|
|
155 |
– |
|
163 |
|
/** |
164 |
|
* submit(null callable) throws NPE |
165 |
|
*/ |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* 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. |
175 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
176 |
|
*/ |
177 |
< |
public void testExecute2() { |
178 |
< |
ThreadPoolExecutor p = |
179 |
< |
new ThreadPoolExecutor(1, 1, |
180 |
< |
60, TimeUnit.SECONDS, |
181 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
177 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
178 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
179 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
180 |
> |
final ExecutorService p |
181 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
182 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
183 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
184 |
> |
public Void realCall() throws InterruptedException { |
185 |
> |
quittingTime.await(); |
186 |
> |
return null; |
187 |
> |
}}; |
188 |
|
try { |
189 |
< |
for (int i = 0; i < 2; ++i) |
190 |
< |
p.submit(new MediumRunnable()); |
191 |
< |
for (int i = 0; i < 2; ++i) { |
192 |
< |
try { |
193 |
< |
p.submit(new SmallCallable()); |
194 |
< |
shouldThrow(); |
195 |
< |
} catch (RejectedExecutionException success) {} |
196 |
< |
} |
189 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
190 |
> |
public void realRun() throws Exception { |
191 |
> |
Future<Void> future = p.submit(awaiter); |
192 |
> |
submitted.countDown(); |
193 |
> |
future.get(); |
194 |
> |
}}); |
195 |
> |
t.start(); |
196 |
> |
submitted.await(); |
197 |
> |
t.interrupt(); |
198 |
> |
t.join(); |
199 |
|
} finally { |
200 |
+ |
quittingTime.countDown(); |
201 |
|
joinPool(p); |
202 |
|
} |
203 |
|
} |
204 |
|
|
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 |
– |
|
205 |
|
/** |
206 |
< |
* get of submitted callable throws InterruptedException if callable |
207 |
< |
* 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 |
< |
|
258 |
< |
/** |
259 |
< |
* get of submit(callable) throws ExecutionException if callable |
260 |
< |
* throws exception |
206 |
> |
* get of submit(callable) throws ExecutionException if callable |
207 |
> |
* throws exception |
208 |
|
*/ |
209 |
|
public void testSubmitEE() throws InterruptedException { |
210 |
|
ThreadPoolExecutor p = |
227 |
|
/** |
228 |
|
* invokeAny(null) throws NPE |
229 |
|
*/ |
230 |
< |
public void testInvokeAny1() |
284 |
< |
throws InterruptedException, ExecutionException { |
230 |
> |
public void testInvokeAny1() throws Exception { |
231 |
|
ExecutorService e = new DirectExecutorService(); |
232 |
|
try { |
233 |
|
e.invokeAny(null); |
241 |
|
/** |
242 |
|
* invokeAny(empty collection) throws IAE |
243 |
|
*/ |
244 |
< |
public void testInvokeAny2() |
299 |
< |
throws InterruptedException, ExecutionException { |
244 |
> |
public void testInvokeAny2() throws Exception { |
245 |
|
ExecutorService e = new DirectExecutorService(); |
246 |
|
try { |
247 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
386 |
|
} |
387 |
|
} |
388 |
|
|
444 |
– |
|
389 |
|
/** |
390 |
|
* timed invokeAny(null) throws NPE |
391 |
|
*/ |
590 |
|
try { |
591 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
592 |
|
l.add(new StringTask()); |
593 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
593 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
594 |
|
l.add(new StringTask()); |
595 |
|
List<Future<String>> futures = |
596 |
< |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
597 |
< |
assertEquals(3, futures.size()); |
598 |
< |
Iterator<Future<String>> it = futures.iterator(); |
599 |
< |
Future<String> f1 = it.next(); |
600 |
< |
Future<String> f2 = it.next(); |
601 |
< |
Future<String> f3 = it.next(); |
602 |
< |
assertTrue(f1.isDone()); |
659 |
< |
assertFalse(f1.isCancelled()); |
660 |
< |
assertTrue(f2.isDone()); |
661 |
< |
assertTrue(f3.isDone()); |
662 |
< |
assertTrue(f3.isCancelled()); |
596 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
597 |
> |
assertEquals(l.size(), futures.size()); |
598 |
> |
for (Future future : futures) |
599 |
> |
assertTrue(future.isDone()); |
600 |
> |
assertFalse(futures.get(0).isCancelled()); |
601 |
> |
assertFalse(futures.get(1).isCancelled()); |
602 |
> |
assertTrue(futures.get(2).isCancelled()); |
603 |
|
} finally { |
604 |
|
joinPool(e); |
605 |
|
} |