6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
|
11 |
+ |
import java.util.ArrayList; |
12 |
+ |
import java.util.List; |
13 |
+ |
import java.util.concurrent.BlockingQueue; |
14 |
+ |
import java.util.concurrent.Callable; |
15 |
+ |
import java.util.concurrent.CountDownLatch; |
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.ScheduledFuture; |
22 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
23 |
+ |
import java.util.concurrent.ThreadFactory; |
24 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
|
|
27 |
+ |
import junit.framework.Test; |
28 |
+ |
import junit.framework.TestSuite; |
29 |
+ |
|
30 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
31 |
|
public static void main(String[] args) { |
32 |
< |
junit.textui.TestRunner.run(suite()); |
32 |
> |
main(suite(), args); |
33 |
|
} |
34 |
|
public static Test suite() { |
35 |
|
return new TestSuite(ScheduledExecutorTest.class); |
156 |
|
*/ |
157 |
|
public void testFixedRateSequence() throws InterruptedException { |
158 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
159 |
< |
RunnableCounter counter = new RunnableCounter(); |
160 |
< |
ScheduledFuture h = |
161 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
162 |
< |
delay(SMALL_DELAY_MS); |
163 |
< |
h.cancel(true); |
164 |
< |
int c = counter.count.get(); |
165 |
< |
// By time scaling conventions, we must have at least |
166 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
167 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
168 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
169 |
< |
joinPool(p); |
159 |
> |
try { |
160 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
161 |
> |
long startTime = System.nanoTime(); |
162 |
> |
int cycles = 10; |
163 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
164 |
> |
Runnable task = new CheckedRunnable() { |
165 |
> |
public void realRun() { done.countDown(); }}; |
166 |
> |
ScheduledFuture h = |
167 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
168 |
> |
done.await(); |
169 |
> |
h.cancel(true); |
170 |
> |
double normalizedTime = |
171 |
> |
(double) millisElapsedSince(startTime) / delay; |
172 |
> |
if (normalizedTime >= cycles - 1 && |
173 |
> |
normalizedTime <= cycles) |
174 |
> |
return; |
175 |
> |
} |
176 |
> |
throw new AssertionError("unexpected execution rate"); |
177 |
> |
} finally { |
178 |
> |
joinPool(p); |
179 |
> |
} |
180 |
|
} |
181 |
|
|
182 |
|
/** |
184 |
|
*/ |
185 |
|
public void testFixedDelaySequence() throws InterruptedException { |
186 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
187 |
< |
RunnableCounter counter = new RunnableCounter(); |
188 |
< |
ScheduledFuture h = |
189 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
190 |
< |
delay(SMALL_DELAY_MS); |
191 |
< |
h.cancel(true); |
192 |
< |
int c = counter.count.get(); |
193 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
194 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
195 |
< |
joinPool(p); |
187 |
> |
try { |
188 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
189 |
> |
long startTime = System.nanoTime(); |
190 |
> |
int cycles = 10; |
191 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
192 |
> |
Runnable task = new CheckedRunnable() { |
193 |
> |
public void realRun() { done.countDown(); }}; |
194 |
> |
ScheduledFuture h = |
195 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
196 |
> |
done.await(); |
197 |
> |
h.cancel(true); |
198 |
> |
double normalizedTime = |
199 |
> |
(double) millisElapsedSince(startTime) / delay; |
200 |
> |
if (normalizedTime >= cycles - 1 && |
201 |
> |
normalizedTime <= cycles) |
202 |
> |
return; |
203 |
> |
} |
204 |
> |
throw new AssertionError("unexpected execution rate"); |
205 |
> |
} finally { |
206 |
> |
joinPool(p); |
207 |
> |
} |
208 |
|
} |
209 |
|
|
210 |
|
/** |