4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
8 |
– |
import java.util.*; |
9 |
– |
import java.util.concurrent.*; |
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
+ |
|
9 |
+ |
import java.util.ArrayList; |
10 |
+ |
import java.util.List; |
11 |
+ |
import java.util.concurrent.BlockingQueue; |
12 |
+ |
import java.util.concurrent.Callable; |
13 |
+ |
import java.util.concurrent.CancellationException; |
14 |
+ |
import java.util.concurrent.CountDownLatch; |
15 |
+ |
import java.util.concurrent.Delayed; |
16 |
+ |
import java.util.concurrent.ExecutionException; |
17 |
+ |
import java.util.concurrent.Executors; |
18 |
+ |
import java.util.concurrent.ExecutorService; |
19 |
+ |
import java.util.concurrent.Future; |
20 |
+ |
import java.util.concurrent.RejectedExecutionException; |
21 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
22 |
+ |
import java.util.concurrent.RunnableScheduledFuture; |
23 |
+ |
import java.util.concurrent.ScheduledFuture; |
24 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
25 |
+ |
import java.util.concurrent.ThreadFactory; |
26 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
27 |
+ |
import java.util.concurrent.TimeoutException; |
28 |
+ |
import java.util.concurrent.TimeUnit; |
29 |
|
import java.util.concurrent.atomic.AtomicInteger; |
30 |
|
|
31 |
+ |
import junit.framework.Test; |
32 |
+ |
import junit.framework.TestSuite; |
33 |
+ |
|
34 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
35 |
|
public static void main(String[] args) { |
36 |
< |
junit.textui.TestRunner.run(suite()); |
36 |
> |
main(suite(), args); |
37 |
|
} |
38 |
|
public static Test suite() { |
39 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
1241 |
|
public void testTimedInvokeAll6() throws Exception { |
1242 |
|
ExecutorService e = new CustomExecutor(2); |
1243 |
|
try { |
1244 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1245 |
< |
l.add(new StringTask()); |
1246 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1247 |
< |
l.add(new StringTask()); |
1248 |
< |
List<Future<String>> futures = |
1249 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1250 |
< |
assertEquals(l.size(), futures.size()); |
1251 |
< |
for (Future future : futures) |
1252 |
< |
assertTrue(future.isDone()); |
1253 |
< |
assertFalse(futures.get(0).isCancelled()); |
1254 |
< |
assertTrue(futures.get(1).isCancelled()); |
1244 |
> |
for (long timeout = timeoutMillis();;) { |
1245 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1246 |
> |
tasks.add(new StringTask("0")); |
1247 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1248 |
> |
tasks.add(new StringTask("2")); |
1249 |
> |
long startTime = System.nanoTime(); |
1250 |
> |
List<Future<String>> futures = |
1251 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1252 |
> |
assertEquals(tasks.size(), futures.size()); |
1253 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1254 |
> |
for (Future future : futures) |
1255 |
> |
assertTrue(future.isDone()); |
1256 |
> |
assertTrue(futures.get(1).isCancelled()); |
1257 |
> |
try { |
1258 |
> |
assertEquals("0", futures.get(0).get()); |
1259 |
> |
assertEquals("2", futures.get(2).get()); |
1260 |
> |
break; |
1261 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1262 |
> |
timeout *= 2; |
1263 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1264 |
> |
fail("expected exactly one task to be cancelled"); |
1265 |
> |
} |
1266 |
> |
} |
1267 |
|
} finally { |
1268 |
|
joinPool(e); |
1269 |
|
} |