193 |
|
*/ |
194 |
|
public void testFixedRateSequence() throws InterruptedException { |
195 |
|
CustomExecutor p = new CustomExecutor(1); |
196 |
< |
RunnableCounter counter = new RunnableCounter(); |
197 |
< |
ScheduledFuture h = |
198 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
199 |
< |
delay(SMALL_DELAY_MS); |
200 |
< |
h.cancel(true); |
201 |
< |
int c = counter.count.get(); |
202 |
< |
// By time scaling conventions, we must have at least |
203 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
204 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
205 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
206 |
< |
joinPool(p); |
196 |
> |
try { |
197 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
198 |
> |
long startTime = System.nanoTime(); |
199 |
> |
int cycles = 10; |
200 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
201 |
> |
CheckedRunnable task = new CheckedRunnable() { |
202 |
> |
public void realRun() { done.countDown(); }}; |
203 |
> |
ScheduledFuture h = |
204 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
205 |
> |
done.await(); |
206 |
> |
h.cancel(true); |
207 |
> |
double normalizedTime = |
208 |
> |
(double) millisElapsedSince(startTime) / delay; |
209 |
> |
if (normalizedTime >= cycles - 1 && |
210 |
> |
normalizedTime <= cycles) |
211 |
> |
return; |
212 |
> |
} |
213 |
> |
throw new AssertionError("unexpected execution rate"); |
214 |
> |
} finally { |
215 |
> |
joinPool(p); |
216 |
> |
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
221 |
|
*/ |
222 |
|
public void testFixedDelaySequence() throws InterruptedException { |
223 |
|
CustomExecutor p = new CustomExecutor(1); |
224 |
< |
RunnableCounter counter = new RunnableCounter(); |
225 |
< |
ScheduledFuture h = |
226 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
227 |
< |
delay(SMALL_DELAY_MS); |
228 |
< |
h.cancel(true); |
229 |
< |
int c = counter.count.get(); |
230 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
231 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
232 |
< |
joinPool(p); |
224 |
> |
try { |
225 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
226 |
> |
long startTime = System.nanoTime(); |
227 |
> |
int cycles = 10; |
228 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
229 |
> |
CheckedRunnable task = new CheckedRunnable() { |
230 |
> |
public void realRun() { done.countDown(); }}; |
231 |
> |
ScheduledFuture h = |
232 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
233 |
> |
done.await(); |
234 |
> |
h.cancel(true); |
235 |
> |
double normalizedTime = |
236 |
> |
(double) millisElapsedSince(startTime) / delay; |
237 |
> |
if (normalizedTime >= cycles - 1 && |
238 |
> |
normalizedTime <= cycles) |
239 |
> |
return; |
240 |
> |
} |
241 |
> |
throw new AssertionError("unexpected execution rate"); |
242 |
> |
} finally { |
243 |
> |
joinPool(p); |
244 |
> |
} |
245 |
|
} |
246 |
|
|
247 |
|
/** |