Skip to content

Commit

Permalink
xds: replace mock(ScheduledFuture) with a TestScheduledFuture impleme…
Browse files Browse the repository at this point in the history
…ntation (#7346)
  • Loading branch information
sanjaypujare committed Aug 21, 2020
1 parent 3945076 commit f8bd84c
Showing 1 changed file with 69 additions and 17 deletions.
Expand Up @@ -63,14 +63,14 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Delayed;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.bouncycastle.operator.OperatorCreationException;
import org.junit.Before;
Expand Down Expand Up @@ -276,7 +276,7 @@ ManagedChannel createChannel(String serverUri) {

@Test
public void startAndClose() {
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture)
.when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
Expand Down Expand Up @@ -304,7 +304,7 @@ public void startAndClose() {

@Test
public void startTwice_noException() {
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture)
.when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
Expand All @@ -327,7 +327,7 @@ public void getCertificate()
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture)
.when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
Expand Down Expand Up @@ -355,7 +355,7 @@ public void getCertificate_withError()
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
responsesToSend
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
provider.refreshCertificate();
Expand Down Expand Up @@ -384,7 +384,7 @@ public void getCertificate_withError_withExistingCert()
responsesToSend
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
provider.refreshCertificate();
Expand Down Expand Up @@ -414,7 +414,7 @@ public void getCertificate_withError_withExistingExpiredCert()
responsesToSend
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
provider.refreshCertificate();
Expand All @@ -432,7 +432,7 @@ public void getCertificate_withError_withExistingExpiredCert()
@Test
public void getCertificate_retriesWithErrors()
throws IOException, CertificateException, OperatorCreationException,
NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
NoSuchAlgorithmException {
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
Expand All @@ -445,10 +445,10 @@ public void getCertificate_retriesWithErrors()
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
ScheduledFuture<?> scheduledFutureSleep = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFutureSleep = new TestScheduledFuture<>();
doReturn(scheduledFutureSleep).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
provider.refreshCertificate();
Expand All @@ -465,7 +465,7 @@ public void getCertificate_retriesWithErrors()
@Test
public void getCertificate_retriesWithTimeouts()
throws IOException, CertificateException, OperatorCreationException,
NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
NoSuchAlgorithmException {
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
Expand All @@ -478,10 +478,10 @@ public void getCertificate_retriesWithTimeouts()
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
doReturn(scheduledFuture).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
ScheduledFuture<?> scheduledFutureSleep = mock(ScheduledFuture.class);
TestScheduledFuture<?> scheduledFutureSleep = new TestScheduledFuture<>();
doReturn(scheduledFutureSleep).when(timeService)
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
provider.refreshCertificate();
Expand All @@ -495,14 +495,15 @@ public void getCertificate_retriesWithTimeouts()
verifyReceivedMetadataValues(4);
}

private void verifyRetriesWithBackoff(ScheduledFuture<?> scheduledFutureSleep, int numOfRetries)
throws InterruptedException, ExecutionException, TimeoutException {
private void verifyRetriesWithBackoff(
TestScheduledFuture<?> scheduledFutureSleep, int numOfRetries) {
for (int i = 0; i < numOfRetries; i++) {
long delayValue = DELAY_VALUES[i];
verify(timeService, times(1)).schedule(any(Runnable.class),
eq(delayValue),
eq(TimeUnit.NANOSECONDS));
verify(scheduledFutureSleep, times(1)).get(eq(delayValue), eq(TimeUnit.NANOSECONDS));
assertThat(scheduledFutureSleep.calls.get(i).timeout).isEqualTo(delayValue);
assertThat(scheduledFutureSleep.calls.get(i).unit).isEqualTo(TimeUnit.NANOSECONDS);
}
}

Expand Down Expand Up @@ -536,4 +537,55 @@ private void verifyReceivedMetadataValues(int count) {
assertThat(receivedZoneValues.poll()).isEqualTo("us-west2-a");
}
}

private static class TestScheduledFuture<V> implements ScheduledFuture<V> {

static class Record {
long timeout;
TimeUnit unit;

Record(long timeout, TimeUnit unit) {
this.timeout = timeout;
this.unit = unit;
}
}

ArrayList<Record> calls = new ArrayList<>();

@Override
public long getDelay(TimeUnit unit) {
return 0;
}

@Override
public int compareTo(Delayed o) {
return 0;
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}

@Override
public boolean isCancelled() {
return false;
}

@Override
public boolean isDone() {
return false;
}

@Override
public V get() {
return null;
}

@Override
public V get(long timeout, TimeUnit unit) {
calls.add(new Record(timeout, unit));
return null;
}
}
}

0 comments on commit f8bd84c

Please sign in to comment.