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 |
< |
import java.util.concurrent.atomic.*; |
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) { |
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 |
|
/** |
357 |
|
threadProceed.await(); |
358 |
|
threadDone.countDown(); |
359 |
|
}}); |
360 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
360 |
> |
await(threadStarted); |
361 |
|
assertEquals(0, p.getCompletedTaskCount()); |
362 |
|
threadProceed.countDown(); |
363 |
|
threadDone.await(); |
1192 |
|
l.add(new StringTask()); |
1193 |
|
List<Future<String>> futures = |
1194 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1195 |
< |
assertEquals(3, futures.size()); |
1196 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1197 |
< |
Future<String> f1 = it.next(); |
1198 |
< |
Future<String> f2 = it.next(); |
1199 |
< |
Future<String> f3 = it.next(); |
1163 |
< |
assertTrue(f1.isDone()); |
1164 |
< |
assertTrue(f2.isDone()); |
1165 |
< |
assertTrue(f3.isDone()); |
1166 |
< |
assertFalse(f1.isCancelled()); |
1167 |
< |
assertTrue(f2.isCancelled()); |
1195 |
> |
assertEquals(l.size(), futures.size()); |
1196 |
> |
for (Future future : futures) |
1197 |
> |
assertTrue(future.isDone()); |
1198 |
> |
assertFalse(futures.get(0).isCancelled()); |
1199 |
> |
assertTrue(futures.get(1).isCancelled()); |
1200 |
|
} finally { |
1201 |
|
joinPool(e); |
1202 |
|
} |