7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
26 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
27 |
|
import java.util.concurrent.ThreadFactory; |
28 |
|
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
32 |
|
|
33 |
|
import junit.framework.Test; |
34 |
|
import junit.framework.TestSuite; |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
149 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
150 |
> |
* Eventually, it must hold that: |
151 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
152 |
|
*/ |
153 |
|
public void testFixedRateSequence() throws InterruptedException { |
154 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
155 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
156 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
157 |
< |
long startTime = System.nanoTime(); |
158 |
< |
int cycles = 10; |
157 |
> |
final long startTime = System.nanoTime(); |
158 |
> |
final int cycles = 8; |
159 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
160 |
< |
Runnable task = new CheckedRunnable() { |
160 |
> |
final Runnable task = new CheckedRunnable() { |
161 |
|
public void realRun() { done.countDown(); }}; |
162 |
< |
ScheduledFuture h = |
162 |
> |
final ScheduledFuture periodicTask = |
163 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
164 |
< |
await(done); |
165 |
< |
h.cancel(true); |
166 |
< |
double normalizedTime = |
167 |
< |
(double) millisElapsedSince(startTime) / delay; |
168 |
< |
if (normalizedTime >= cycles - 1 && |
169 |
< |
normalizedTime <= cycles) |
164 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
165 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
166 |
> |
periodicTask.cancel(true); |
167 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
168 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
169 |
> |
if (elapsedMillis <= cycles * delay) |
170 |
|
return; |
171 |
+ |
// else retry with longer delay |
172 |
|
} |
173 |
|
fail("unexpected execution rate"); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
178 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
179 |
> |
* Eventually, it must hold that each task starts at least delay and at |
180 |
> |
* most 2 * delay after the termination of the previous task. |
181 |
|
*/ |
182 |
|
public void testFixedDelaySequence() throws InterruptedException { |
183 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
184 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
185 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
186 |
< |
long startTime = System.nanoTime(); |
187 |
< |
int cycles = 10; |
186 |
> |
final long startTime = System.nanoTime(); |
187 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
188 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
189 |
> |
final int cycles = 8; |
190 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
191 |
< |
Runnable task = new CheckedRunnable() { |
192 |
< |
public void realRun() { done.countDown(); }}; |
193 |
< |
ScheduledFuture h = |
191 |
> |
final int d = delay; |
192 |
> |
final Runnable task = new CheckedRunnable() { |
193 |
> |
public void realRun() { |
194 |
> |
long now = System.nanoTime(); |
195 |
> |
long elapsedMillis |
196 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
197 |
> |
if (done.getCount() == cycles) { // first execution |
198 |
> |
if (elapsedMillis >= d) |
199 |
> |
tryLongerDelay.set(true); |
200 |
> |
} else { |
201 |
> |
assertTrue(elapsedMillis >= d); |
202 |
> |
if (elapsedMillis >= 2 * d) |
203 |
> |
tryLongerDelay.set(true); |
204 |
> |
} |
205 |
> |
previous.set(now); |
206 |
> |
done.countDown(); |
207 |
> |
}}; |
208 |
> |
final ScheduledFuture periodicTask = |
209 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
210 |
< |
await(done); |
211 |
< |
h.cancel(true); |
212 |
< |
double normalizedTime = |
213 |
< |
(double) millisElapsedSince(startTime) / delay; |
214 |
< |
if (normalizedTime >= cycles - 1 && |
215 |
< |
normalizedTime <= cycles) |
210 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
211 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
212 |
> |
periodicTask.cancel(true); |
213 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
214 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
215 |
> |
if (!tryLongerDelay.get()) |
216 |
|
return; |
217 |
+ |
// else retry with longer delay |
218 |
|
} |
219 |
|
fail("unexpected execution rate"); |
220 |
|
} |