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); |
57 |
|
} |
58 |
|
public boolean isCancelled() { return task.isCancelled(); } |
59 |
|
public boolean isDone() { return task.isDone(); } |
60 |
< |
public V get() throws InterruptedException, ExecutionException { |
60 |
> |
public V get() throws InterruptedException, ExecutionException { |
61 |
|
V v = task.get(); |
62 |
|
assertTrue(ran); |
63 |
|
return v; |
64 |
|
} |
65 |
< |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
65 |
> |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
66 |
|
V v = task.get(time, unit); |
67 |
|
assertTrue(ran); |
68 |
|
return v; |
214 |
|
*/ |
215 |
|
public void testFixedRateSequence() throws InterruptedException { |
216 |
|
CustomExecutor p = new CustomExecutor(1); |
217 |
< |
RunnableCounter counter = new RunnableCounter(); |
218 |
< |
ScheduledFuture h = |
219 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
220 |
< |
delay(SMALL_DELAY_MS); |
221 |
< |
h.cancel(true); |
222 |
< |
int c = counter.count.get(); |
223 |
< |
// By time scaling conventions, we must have at least |
224 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
225 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
226 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
227 |
< |
joinPool(p); |
217 |
> |
try { |
218 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
219 |
> |
long startTime = System.nanoTime(); |
220 |
> |
int cycles = 10; |
221 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
222 |
> |
Runnable task = new CheckedRunnable() { |
223 |
> |
public void realRun() { done.countDown(); }}; |
224 |
> |
ScheduledFuture h = |
225 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
226 |
> |
done.await(); |
227 |
> |
h.cancel(true); |
228 |
> |
double normalizedTime = |
229 |
> |
(double) millisElapsedSince(startTime) / delay; |
230 |
> |
if (normalizedTime >= cycles - 1 && |
231 |
> |
normalizedTime <= cycles) |
232 |
> |
return; |
233 |
> |
} |
234 |
> |
throw new AssertionError("unexpected execution rate"); |
235 |
> |
} finally { |
236 |
> |
joinPool(p); |
237 |
> |
} |
238 |
|
} |
239 |
|
|
240 |
|
/** |
242 |
|
*/ |
243 |
|
public void testFixedDelaySequence() throws InterruptedException { |
244 |
|
CustomExecutor p = new CustomExecutor(1); |
245 |
< |
RunnableCounter counter = new RunnableCounter(); |
246 |
< |
ScheduledFuture h = |
247 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
248 |
< |
delay(SMALL_DELAY_MS); |
249 |
< |
h.cancel(true); |
250 |
< |
int c = counter.count.get(); |
251 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
252 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
253 |
< |
joinPool(p); |
245 |
> |
try { |
246 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
247 |
> |
long startTime = System.nanoTime(); |
248 |
> |
int cycles = 10; |
249 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
250 |
> |
Runnable task = new CheckedRunnable() { |
251 |
> |
public void realRun() { done.countDown(); }}; |
252 |
> |
ScheduledFuture h = |
253 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
254 |
> |
done.await(); |
255 |
> |
h.cancel(true); |
256 |
> |
double normalizedTime = |
257 |
> |
(double) millisElapsedSince(startTime) / delay; |
258 |
> |
if (normalizedTime >= cycles - 1 && |
259 |
> |
normalizedTime <= cycles) |
260 |
> |
return; |
261 |
> |
} |
262 |
> |
throw new AssertionError("unexpected execution rate"); |
263 |
> |
} finally { |
264 |
> |
joinPool(p); |
265 |
> |
} |
266 |
|
} |
267 |
|
|
268 |
|
/** |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* shutdownNow returns a list containing tasks that were not run |
708 |
> |
* shutdownNow returns a list containing tasks that were not run, |
709 |
> |
* and those tasks are drained from the queue |
710 |
|
*/ |
711 |
|
public void testShutdownNow() { |
712 |
|
CustomExecutor p = new CustomExecutor(1); |
716 |
|
try { |
717 |
|
List<Runnable> l = p.shutdownNow(); |
718 |
|
assertTrue(p.isShutdown()); |
719 |
+ |
assertTrue(p.getQueue().isEmpty()); |
720 |
|
assertEquals(5, l.size()); |
721 |
|
} catch (SecurityException ok) { |
722 |
|
// Allowed in case test doesn't have privs |
1243 |
|
public void testTimedInvokeAll6() throws Exception { |
1244 |
|
ExecutorService e = new CustomExecutor(2); |
1245 |
|
try { |
1246 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1247 |
< |
l.add(new StringTask()); |
1248 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1249 |
< |
l.add(new StringTask()); |
1250 |
< |
List<Future<String>> futures = |
1251 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1252 |
< |
assertEquals(l.size(), futures.size()); |
1253 |
< |
for (Future future : futures) |
1254 |
< |
assertTrue(future.isDone()); |
1255 |
< |
assertFalse(futures.get(0).isCancelled()); |
1256 |
< |
assertTrue(futures.get(1).isCancelled()); |
1246 |
> |
for (long timeout = timeoutMillis();;) { |
1247 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1248 |
> |
tasks.add(new StringTask("0")); |
1249 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1250 |
> |
tasks.add(new StringTask("2")); |
1251 |
> |
long startTime = System.nanoTime(); |
1252 |
> |
List<Future<String>> futures = |
1253 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1254 |
> |
assertEquals(tasks.size(), futures.size()); |
1255 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1256 |
> |
for (Future future : futures) |
1257 |
> |
assertTrue(future.isDone()); |
1258 |
> |
assertTrue(futures.get(1).isCancelled()); |
1259 |
> |
try { |
1260 |
> |
assertEquals("0", futures.get(0).get()); |
1261 |
> |
assertEquals("2", futures.get(2).get()); |
1262 |
> |
break; |
1263 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1264 |
> |
timeout *= 2; |
1265 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1266 |
> |
fail("expected exactly one task to be cancelled"); |
1267 |
> |
} |
1268 |
> |
} |
1269 |
|
} finally { |
1270 |
|
joinPool(e); |
1271 |
|
} |