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.CountDownLatch; |
14 |
+ |
import java.util.concurrent.Delayed; |
15 |
+ |
import java.util.concurrent.ExecutionException; |
16 |
+ |
import java.util.concurrent.Executors; |
17 |
+ |
import java.util.concurrent.ExecutorService; |
18 |
+ |
import java.util.concurrent.Future; |
19 |
+ |
import java.util.concurrent.RejectedExecutionException; |
20 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
21 |
+ |
import java.util.concurrent.RunnableScheduledFuture; |
22 |
+ |
import java.util.concurrent.ScheduledFuture; |
23 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
24 |
+ |
import java.util.concurrent.ThreadFactory; |
25 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
26 |
+ |
import java.util.concurrent.TimeoutException; |
27 |
+ |
import java.util.concurrent.TimeUnit; |
28 |
|
import java.util.concurrent.atomic.AtomicInteger; |
29 |
|
|
30 |
+ |
import junit.framework.Test; |
31 |
+ |
import junit.framework.TestSuite; |
32 |
+ |
|
33 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
|
junit.textui.TestRunner.run(suite()); |
213 |
|
*/ |
214 |
|
public void testFixedRateSequence() throws InterruptedException { |
215 |
|
CustomExecutor p = new CustomExecutor(1); |
216 |
< |
RunnableCounter counter = new RunnableCounter(); |
217 |
< |
ScheduledFuture h = |
218 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
219 |
< |
delay(SMALL_DELAY_MS); |
220 |
< |
h.cancel(true); |
221 |
< |
int c = counter.count.get(); |
222 |
< |
// By time scaling conventions, we must have at least |
223 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
224 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
225 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
226 |
< |
joinPool(p); |
216 |
> |
try { |
217 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
218 |
> |
long startTime = System.nanoTime(); |
219 |
> |
int cycles = 10; |
220 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
221 |
> |
Runnable task = new CheckedRunnable() { |
222 |
> |
public void realRun() { done.countDown(); }}; |
223 |
> |
ScheduledFuture h = |
224 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
225 |
> |
done.await(); |
226 |
> |
h.cancel(true); |
227 |
> |
double normalizedTime = |
228 |
> |
(double) millisElapsedSince(startTime) / delay; |
229 |
> |
if (normalizedTime >= cycles - 1 && |
230 |
> |
normalizedTime <= cycles) |
231 |
> |
return; |
232 |
> |
} |
233 |
> |
throw new AssertionError("unexpected execution rate"); |
234 |
> |
} finally { |
235 |
> |
joinPool(p); |
236 |
> |
} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
241 |
|
*/ |
242 |
|
public void testFixedDelaySequence() throws InterruptedException { |
243 |
|
CustomExecutor p = new CustomExecutor(1); |
244 |
< |
RunnableCounter counter = new RunnableCounter(); |
245 |
< |
ScheduledFuture h = |
246 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
247 |
< |
delay(SMALL_DELAY_MS); |
248 |
< |
h.cancel(true); |
249 |
< |
int c = counter.count.get(); |
250 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
251 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
252 |
< |
joinPool(p); |
244 |
> |
try { |
245 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
246 |
> |
long startTime = System.nanoTime(); |
247 |
> |
int cycles = 10; |
248 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
249 |
> |
Runnable task = new CheckedRunnable() { |
250 |
> |
public void realRun() { done.countDown(); }}; |
251 |
> |
ScheduledFuture h = |
252 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
253 |
> |
done.await(); |
254 |
> |
h.cancel(true); |
255 |
> |
double normalizedTime = |
256 |
> |
(double) millisElapsedSince(startTime) / delay; |
257 |
> |
if (normalizedTime >= cycles - 1 && |
258 |
> |
normalizedTime <= cycles) |
259 |
> |
return; |
260 |
> |
} |
261 |
> |
throw new AssertionError("unexpected execution rate"); |
262 |
> |
} finally { |
263 |
> |
joinPool(p); |
264 |
> |
} |
265 |
|
} |
266 |
|
|
267 |
|
/** |