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 java.util.concurrent.atomic.*; |
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); |
59 |
|
} |
60 |
|
public boolean isCancelled() { return task.isCancelled(); } |
61 |
|
public boolean isDone() { return task.isDone(); } |
62 |
< |
public V get() throws InterruptedException, ExecutionException { |
62 |
> |
public V get() throws InterruptedException, ExecutionException { |
63 |
|
V v = task.get(); |
64 |
|
assertTrue(ran); |
65 |
|
return v; |
66 |
|
} |
67 |
< |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
67 |
> |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
68 |
|
V v = task.get(time, unit); |
69 |
|
assertTrue(ran); |
70 |
|
return v; |
80 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) { |
81 |
|
return new CustomTask<V>(task); |
82 |
|
} |
83 |
< |
CustomExecutor(int corePoolSize) { super(corePoolSize);} |
83 |
> |
CustomExecutor(int corePoolSize) { super(corePoolSize); } |
84 |
|
CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { |
85 |
|
super(corePoolSize, handler); |
86 |
|
} |
216 |
|
*/ |
217 |
|
public void testFixedRateSequence() throws InterruptedException { |
218 |
|
CustomExecutor p = new CustomExecutor(1); |
219 |
< |
RunnableCounter counter = new RunnableCounter(); |
220 |
< |
ScheduledFuture h = |
221 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
222 |
< |
delay(SMALL_DELAY_MS); |
223 |
< |
h.cancel(true); |
224 |
< |
int c = counter.count.get(); |
225 |
< |
// By time scaling conventions, we must have at least |
226 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
227 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
228 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
229 |
< |
joinPool(p); |
219 |
> |
try { |
220 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
221 |
> |
long startTime = System.nanoTime(); |
222 |
> |
int cycles = 10; |
223 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
224 |
> |
Runnable task = new CheckedRunnable() { |
225 |
> |
public void realRun() { done.countDown(); }}; |
226 |
> |
ScheduledFuture h = |
227 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
228 |
> |
done.await(); |
229 |
> |
h.cancel(true); |
230 |
> |
double normalizedTime = |
231 |
> |
(double) millisElapsedSince(startTime) / delay; |
232 |
> |
if (normalizedTime >= cycles - 1 && |
233 |
> |
normalizedTime <= cycles) |
234 |
> |
return; |
235 |
> |
} |
236 |
> |
throw new AssertionError("unexpected execution rate"); |
237 |
> |
} finally { |
238 |
> |
joinPool(p); |
239 |
> |
} |
240 |
|
} |
241 |
|
|
242 |
|
/** |
244 |
|
*/ |
245 |
|
public void testFixedDelaySequence() throws InterruptedException { |
246 |
|
CustomExecutor p = new CustomExecutor(1); |
247 |
< |
RunnableCounter counter = new RunnableCounter(); |
248 |
< |
ScheduledFuture h = |
249 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
250 |
< |
delay(SMALL_DELAY_MS); |
251 |
< |
h.cancel(true); |
252 |
< |
int c = counter.count.get(); |
253 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
254 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
255 |
< |
joinPool(p); |
247 |
> |
try { |
248 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
249 |
> |
long startTime = System.nanoTime(); |
250 |
> |
int cycles = 10; |
251 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
252 |
> |
Runnable task = new CheckedRunnable() { |
253 |
> |
public void realRun() { done.countDown(); }}; |
254 |
> |
ScheduledFuture h = |
255 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
256 |
> |
done.await(); |
257 |
> |
h.cancel(true); |
258 |
> |
double normalizedTime = |
259 |
> |
(double) millisElapsedSince(startTime) / delay; |
260 |
> |
if (normalizedTime >= cycles - 1 && |
261 |
> |
normalizedTime <= cycles) |
262 |
> |
return; |
263 |
> |
} |
264 |
> |
throw new AssertionError("unexpected execution rate"); |
265 |
> |
} finally { |
266 |
> |
joinPool(p); |
267 |
> |
} |
268 |
|
} |
269 |
|
|
270 |
|
/** |
415 |
|
threadProceed.await(); |
416 |
|
threadDone.countDown(); |
417 |
|
}}); |
418 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
418 |
> |
await(threadStarted); |
419 |
|
assertEquals(0, p.getCompletedTaskCount()); |
420 |
|
threadProceed.countDown(); |
421 |
|
threadDone.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 |
> |
if (testImplementationDetails) |
723 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
724 |
> |
final List<Runnable> queuedTasks; |
725 |
|
try { |
726 |
< |
List<Runnable> l = p.shutdownNow(); |
674 |
< |
assertTrue(p.isShutdown()); |
675 |
< |
assertEquals(5, l.size()); |
726 |
> |
queuedTasks = p.shutdownNow(); |
727 |
|
} catch (SecurityException ok) { |
728 |
< |
// Allowed in case test doesn't have privs |
729 |
< |
} finally { |
730 |
< |
joinPool(p); |
728 |
> |
return; // Allowed in case test doesn't have privs |
729 |
> |
} |
730 |
> |
assertTrue(p.isShutdown()); |
731 |
> |
assertTrue(p.getQueue().isEmpty()); |
732 |
> |
if (testImplementationDetails) |
733 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
734 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
735 |
> |
for (ScheduledFuture task : tasks) { |
736 |
> |
assertFalse(((CustomTask)task).ran); |
737 |
> |
assertFalse(task.isDone()); |
738 |
> |
assertFalse(task.isCancelled()); |
739 |
|
} |
740 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
741 |
+ |
assertTrue(p.isTerminated()); |
742 |
|
} |
743 |
|
|
744 |
|
/** |
1259 |
|
public void testTimedInvokeAll6() throws Exception { |
1260 |
|
ExecutorService e = new CustomExecutor(2); |
1261 |
|
try { |
1262 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1263 |
< |
l.add(new StringTask()); |
1264 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1265 |
< |
l.add(new StringTask()); |
1266 |
< |
List<Future<String>> futures = |
1267 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1268 |
< |
assertEquals(3, futures.size()); |
1269 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1270 |
< |
Future<String> f1 = it.next(); |
1271 |
< |
Future<String> f2 = it.next(); |
1272 |
< |
Future<String> f3 = it.next(); |
1273 |
< |
assertTrue(f1.isDone()); |
1274 |
< |
assertTrue(f2.isDone()); |
1275 |
< |
assertTrue(f3.isDone()); |
1276 |
< |
assertFalse(f1.isCancelled()); |
1277 |
< |
assertTrue(f2.isCancelled()); |
1262 |
> |
for (long timeout = timeoutMillis();;) { |
1263 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1264 |
> |
tasks.add(new StringTask("0")); |
1265 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1266 |
> |
tasks.add(new StringTask("2")); |
1267 |
> |
long startTime = System.nanoTime(); |
1268 |
> |
List<Future<String>> futures = |
1269 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1270 |
> |
assertEquals(tasks.size(), futures.size()); |
1271 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1272 |
> |
for (Future future : futures) |
1273 |
> |
assertTrue(future.isDone()); |
1274 |
> |
assertTrue(futures.get(1).isCancelled()); |
1275 |
> |
try { |
1276 |
> |
assertEquals("0", futures.get(0).get()); |
1277 |
> |
assertEquals("2", futures.get(2).get()); |
1278 |
> |
break; |
1279 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1280 |
> |
timeout *= 2; |
1281 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1282 |
> |
fail("expected exactly one task to be cancelled"); |
1283 |
> |
} |
1284 |
> |
} |
1285 |
|
} finally { |
1286 |
|
joinPool(e); |
1287 |
|
} |