Commit 00383464 authored by Stein Eldar Johnsen's avatar Stein Eldar Johnsen 💬

FakeClock slight improvement when no listeners.

parent 27da08f0
......@@ -57,22 +57,26 @@ public class FakeClock extends Clock {
*/
public void tick(final long tickMs) {
untilTimeUTC.updateAndGet(d -> d.plus(max(1, tickMs), ChronoUnit.MILLIS));
if (inTick.get()) {
boolean wasInTick = inTick.getAndSet(true);
if (wasInTick) {
// avoid recursion. Just let the other call (currently in the
// block below) take care of the extra time.
return;
}
inTick.set(true);
try {
// Tick the clock along in 100 millis blocks. This is to be able to
// spread out the 'now' timestamps seen while ticking along.
while (untilTimeUTC.get().isAfter(currentTimeUTC.get())) {
final long now = currentTimeUTC.get().toInstant(ZoneOffset.UTC).toEpochMilli();
final long until = untilTimeUTC.get().toInstant(ZoneOffset.UTC).toEpochMilli();
final long skip = min(100, until - now);
currentTimeUTC.updateAndGet(d -> d.plus(skip, ChronoUnit.MILLIS));
listeners.forEach(l -> l.newCurrentTimeUTC(now + skip));
if (listeners.size() > 0) {
// Tick the clock along in 57 millis blocks. This is to be able to
// spread out the 'now' timestamps seen while ticking along.
while (untilTimeUTC.get().isAfter(currentTimeUTC.get())) {
final long now = currentTimeUTC.get().toInstant(ZoneOffset.UTC).toEpochMilli();
final long until = untilTimeUTC.get().toInstant(ZoneOffset.UTC).toEpochMilli();
final long skip = min(100, until - now);
currentTimeUTC.updateAndGet(d -> d.plus(skip, ChronoUnit.MILLIS));
listeners.forEach(l -> l.newCurrentTimeUTC(now + skip));
}
} else {
currentTimeUTC.set(untilTimeUTC.get());
}
} finally {
inTick.set(false);
......
......@@ -418,7 +418,7 @@ public class FakeScheduledExecutorTest {
}
@Test
public void testScheduledAtFixedDelay() {
public void testScheduledWithFixedDelay() {
AtomicInteger calls = new AtomicInteger();
Runnable task = () -> {
calls.incrementAndGet();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment