Skip to content

Commit

Permalink
Make ManualClock thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmv committed Sep 30, 2020
1 parent c068007 commit dc4f516
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
13 changes: 7 additions & 6 deletions testutil/src/main/java/com/yahoo/test/ManualClock.java
Expand Up @@ -9,6 +9,7 @@
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAmount;
import java.util.concurrent.atomic.AtomicReference;

/**
* A clock which initially has the time of its creation but can only be advanced by calling advance
Expand All @@ -17,7 +18,7 @@
*/
public class ManualClock extends Clock {

private Instant currentTime = Instant.now();
private AtomicReference<Instant> currentTime = new AtomicReference<>(Instant.now());

@Inject
public ManualClock() {}
Expand All @@ -27,19 +28,19 @@ public ManualClock(String utcIsoTime) {
}

public ManualClock(Instant currentTime) {
this.currentTime = currentTime;
setInstant(currentTime);
}

public void advance(TemporalAmount temporal) {
currentTime = currentTime.plus(temporal);
currentTime.updateAndGet(time -> time.plus(temporal));
}

public void setInstant(Instant time) {
currentTime = time;
currentTime.set(time);
}

@Override
public Instant instant() { return currentTime; }
public Instant instant() { return currentTime.get(); }

@Override
public ZoneId getZone() { return null; }
Expand All @@ -48,7 +49,7 @@ public void setInstant(Instant time) {
public Clock withZone(ZoneId zone) { return null; }

@Override
public long millis() { return currentTime.toEpochMilli(); }
public long millis() { return instant().toEpochMilli(); }

public static Instant at(String utcIsoTime) {
return LocalDateTime.parse(utcIsoTime, DateTimeFormatter.ISO_DATE_TIME).atZone(ZoneOffset.UTC).toInstant();
Expand Down
Expand Up @@ -203,32 +203,32 @@ public void testTimeout() throws InterruptedException {
clock.advance(Duration.ofMillis(20));
executor.put(new DocumentPut(doc2), parameters(), operationContext());
executor.notifyMaintainers();
assertEquals(List.of(), received);
assertEquals(List.of(), errors);
assertEquals(List.of(), received);

clock.advance(Duration.ofMillis(990));
executor.notifyMaintainers(); // Let doc1 time out.
phaser.arrive(); // Let doc2 arrive.
phaser.arriveAndAwaitAdvance(); // Wait for responses to be delivered. <3 Phaser <3
assertEquals(List.of(doc2), received);
phaser.arriveAndAwaitAdvance(); // Wait for responses to be delivered.
assertEquals(List.of(TIMEOUT), errors);
assertEquals(List.of(doc2), received);

session().setResultType(Result.ResultType.TRANSIENT_ERROR);
executor.put(new DocumentPut(doc3), parameters(), operationContext());
clock.advance(Duration.ofMillis(990));
executor.notifyMaintainers(); // Retry throttled operation.
clock.advance(Duration.ofMillis(20));
executor.notifyMaintainers(); // Time out throttled operation.
assertEquals(List.of(doc2), received);
assertEquals(List.of(TIMEOUT, TIMEOUT), errors);
assertEquals(List.of(doc2), received);

session().setResultType(Result.ResultType.SUCCESS);
clock.advance(Duration.ofMillis(20));
executor.notifyMaintainers(); // Retry not attempted since operation already timed out.
phaser.arrive();
phaser.arriveAndAwaitAdvance();
assertEquals(List.of(doc2), received);
assertEquals(List.of(TIMEOUT, TIMEOUT), errors);
assertEquals(List.of(doc2), received);
}

@Test
Expand Down

0 comments on commit dc4f516

Please sign in to comment.