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 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
+ |
|
10 |
+ |
import java.util.ArrayList; |
11 |
+ |
import java.util.HashSet; |
12 |
+ |
import java.util.List; |
13 |
+ |
import java.util.concurrent.BlockingQueue; |
14 |
+ |
import java.util.concurrent.Callable; |
15 |
+ |
import java.util.concurrent.CancellationException; |
16 |
+ |
import java.util.concurrent.CountDownLatch; |
17 |
+ |
import java.util.concurrent.Delayed; |
18 |
+ |
import java.util.concurrent.ExecutionException; |
19 |
+ |
import java.util.concurrent.Executors; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
+ |
import java.util.concurrent.Future; |
22 |
+ |
import java.util.concurrent.RejectedExecutionException; |
23 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
24 |
+ |
import java.util.concurrent.RunnableScheduledFuture; |
25 |
+ |
import java.util.concurrent.ScheduledFuture; |
26 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
+ |
import java.util.concurrent.ThreadFactory; |
28 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.TimeoutException; |
30 |
+ |
import java.util.concurrent.TimeUnit; |
31 |
|
import java.util.concurrent.atomic.AtomicInteger; |
32 |
|
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
35 |
+ |
|
36 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
public static Test suite() { |
41 |
|
return new TestSuite(ScheduledExecutorSubclassTest.class); |
221 |
|
long startTime = System.nanoTime(); |
222 |
|
int cycles = 10; |
223 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
224 |
< |
CheckedRunnable task = new CheckedRunnable() { |
224 |
> |
Runnable task = new CheckedRunnable() { |
225 |
|
public void realRun() { done.countDown(); }}; |
203 |
– |
|
226 |
|
ScheduledFuture h = |
227 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
228 |
|
done.await(); |
249 |
|
long startTime = System.nanoTime(); |
250 |
|
int cycles = 10; |
251 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
252 |
< |
CheckedRunnable task = new CheckedRunnable() { |
252 |
> |
Runnable task = new CheckedRunnable() { |
253 |
|
public void realRun() { done.countDown(); }}; |
232 |
– |
|
254 |
|
ScheduledFuture h = |
255 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
256 |
|
done.await(); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* shutdownNow returns a list containing tasks that were not run |
710 |
> |
* shutdownNow returns a list containing tasks that were not run, |
711 |
> |
* and those tasks are drained from the queue |
712 |
|
*/ |
713 |
< |
public void testShutdownNow() { |
713 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
714 |
|
CustomExecutor p = new CustomExecutor(1); |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
717 |
< |
LONG_DELAY_MS, MILLISECONDS); |
715 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
716 |
> |
for (int i = 0; i < 3; i++) { |
717 |
> |
Runnable r = new NoOpRunnable(); |
718 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
719 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
720 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
721 |
> |
} |
722 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
723 |
> |
final List<Runnable> queuedTasks; |
724 |
|
try { |
725 |
< |
List<Runnable> l = p.shutdownNow(); |
698 |
< |
assertTrue(p.isShutdown()); |
699 |
< |
assertEquals(5, l.size()); |
725 |
> |
queuedTasks = p.shutdownNow(); |
726 |
|
} catch (SecurityException ok) { |
727 |
< |
// Allowed in case test doesn't have privs |
728 |
< |
} finally { |
729 |
< |
joinPool(p); |
727 |
> |
return; // Allowed in case test doesn't have privs |
728 |
> |
} |
729 |
> |
assertTrue(p.isShutdown()); |
730 |
> |
assertTrue(p.getQueue().isEmpty()); |
731 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
732 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
733 |
> |
for (ScheduledFuture task : tasks) { |
734 |
> |
assertFalse(((CustomTask)task).ran); |
735 |
> |
assertFalse(task.isDone()); |
736 |
> |
assertFalse(task.isCancelled()); |
737 |
|
} |
738 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
739 |
+ |
assertTrue(p.isTerminated()); |
740 |
|
} |
741 |
|
|
742 |
|
/** |
1257 |
|
public void testTimedInvokeAll6() throws Exception { |
1258 |
|
ExecutorService e = new CustomExecutor(2); |
1259 |
|
try { |
1260 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1261 |
< |
l.add(new StringTask()); |
1262 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1263 |
< |
l.add(new StringTask()); |
1264 |
< |
List<Future<String>> futures = |
1265 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1266 |
< |
assertEquals(l.size(), futures.size()); |
1267 |
< |
for (Future future : futures) |
1268 |
< |
assertTrue(future.isDone()); |
1269 |
< |
assertFalse(futures.get(0).isCancelled()); |
1270 |
< |
assertTrue(futures.get(1).isCancelled()); |
1260 |
> |
for (long timeout = timeoutMillis();;) { |
1261 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1262 |
> |
tasks.add(new StringTask("0")); |
1263 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1264 |
> |
tasks.add(new StringTask("2")); |
1265 |
> |
long startTime = System.nanoTime(); |
1266 |
> |
List<Future<String>> futures = |
1267 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1268 |
> |
assertEquals(tasks.size(), futures.size()); |
1269 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1270 |
> |
for (Future future : futures) |
1271 |
> |
assertTrue(future.isDone()); |
1272 |
> |
assertTrue(futures.get(1).isCancelled()); |
1273 |
> |
try { |
1274 |
> |
assertEquals("0", futures.get(0).get()); |
1275 |
> |
assertEquals("2", futures.get(2).get()); |
1276 |
> |
break; |
1277 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1278 |
> |
timeout *= 2; |
1279 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1280 |
> |
fail("expected exactly one task to be cancelled"); |
1281 |
> |
} |
1282 |
> |
} |
1283 |
|
} finally { |
1284 |
|
joinPool(e); |
1285 |
|
} |