mirror of https://github.com/grpc/grpc-java.git
xds: replace mock(ScheduledFuture) with a TestScheduledFuture implementation (#7346)
This commit is contained in:
parent
39450766dd
commit
f8bd84c755
|
|
@ -63,14 +63,14 @@ import java.security.PrivateKey;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.Delayed;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import org.bouncycastle.operator.OperatorCreationException;
|
import org.bouncycastle.operator.OperatorCreationException;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
@ -276,7 +276,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startAndClose() {
|
public void startAndClose() {
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture)
|
doReturn(scheduledFuture)
|
||||||
.when(timeService)
|
.when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
|
|
@ -304,7 +304,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startTwice_noException() {
|
public void startTwice_noException() {
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture)
|
doReturn(scheduledFuture)
|
||||||
.when(timeService)
|
.when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
|
|
@ -327,7 +327,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
||||||
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
||||||
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture)
|
doReturn(scheduledFuture)
|
||||||
.when(timeService)
|
.when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
|
|
@ -355,7 +355,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
||||||
responsesToSend
|
responsesToSend
|
||||||
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture).when(timeService)
|
doReturn(scheduledFuture).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
provider.refreshCertificate();
|
provider.refreshCertificate();
|
||||||
|
|
@ -384,7 +384,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
responsesToSend
|
responsesToSend
|
||||||
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
||||||
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture).when(timeService)
|
doReturn(scheduledFuture).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
provider.refreshCertificate();
|
provider.refreshCertificate();
|
||||||
|
|
@ -414,7 +414,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
responsesToSend
|
responsesToSend
|
||||||
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
.offer(new ResponseThrowable(new StatusRuntimeException(Status.FAILED_PRECONDITION)));
|
||||||
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture).when(timeService)
|
doReturn(scheduledFuture).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
provider.refreshCertificate();
|
provider.refreshCertificate();
|
||||||
|
|
@ -432,7 +432,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
@Test
|
@Test
|
||||||
public void getCertificate_retriesWithErrors()
|
public void getCertificate_retriesWithErrors()
|
||||||
throws IOException, CertificateException, OperatorCreationException,
|
throws IOException, CertificateException, OperatorCreationException,
|
||||||
NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
|
NoSuchAlgorithmException {
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
|
||||||
|
|
@ -445,10 +445,10 @@ public class MeshCaCertificateProviderTest {
|
||||||
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
||||||
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
||||||
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture).when(timeService)
|
doReturn(scheduledFuture).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
ScheduledFuture<?> scheduledFutureSleep = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFutureSleep = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFutureSleep).when(timeService)
|
doReturn(scheduledFutureSleep).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
|
||||||
provider.refreshCertificate();
|
provider.refreshCertificate();
|
||||||
|
|
@ -465,7 +465,7 @@ public class MeshCaCertificateProviderTest {
|
||||||
@Test
|
@Test
|
||||||
public void getCertificate_retriesWithTimeouts()
|
public void getCertificate_retriesWithTimeouts()
|
||||||
throws IOException, CertificateException, OperatorCreationException,
|
throws IOException, CertificateException, OperatorCreationException,
|
||||||
NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
|
NoSuchAlgorithmException {
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "0");
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "1");
|
||||||
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
|
oauth2Tokens.offer(TEST_STS_TOKEN + "2");
|
||||||
|
|
@ -478,10 +478,10 @@ public class MeshCaCertificateProviderTest {
|
||||||
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
CommonTlsContextTestsUtil.getResourceContents(SERVER_1_PEM_FILE),
|
||||||
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
CommonTlsContextTestsUtil.getResourceContents(CA_PEM_FILE))));
|
||||||
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
when(timeProvider.currentTimeNanos()).thenReturn(CURRENT_TIME_NANOS);
|
||||||
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFuture = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFuture).when(timeService)
|
doReturn(scheduledFuture).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.SECONDS));
|
||||||
ScheduledFuture<?> scheduledFutureSleep = mock(ScheduledFuture.class);
|
TestScheduledFuture<?> scheduledFutureSleep = new TestScheduledFuture<>();
|
||||||
doReturn(scheduledFutureSleep).when(timeService)
|
doReturn(scheduledFutureSleep).when(timeService)
|
||||||
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
|
.schedule(any(Runnable.class), any(Long.TYPE), eq(TimeUnit.NANOSECONDS));
|
||||||
provider.refreshCertificate();
|
provider.refreshCertificate();
|
||||||
|
|
@ -495,14 +495,15 @@ public class MeshCaCertificateProviderTest {
|
||||||
verifyReceivedMetadataValues(4);
|
verifyReceivedMetadataValues(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyRetriesWithBackoff(ScheduledFuture<?> scheduledFutureSleep, int numOfRetries)
|
private void verifyRetriesWithBackoff(
|
||||||
throws InterruptedException, ExecutionException, TimeoutException {
|
TestScheduledFuture<?> scheduledFutureSleep, int numOfRetries) {
|
||||||
for (int i = 0; i < numOfRetries; i++) {
|
for (int i = 0; i < numOfRetries; i++) {
|
||||||
long delayValue = DELAY_VALUES[i];
|
long delayValue = DELAY_VALUES[i];
|
||||||
verify(timeService, times(1)).schedule(any(Runnable.class),
|
verify(timeService, times(1)).schedule(any(Runnable.class),
|
||||||
eq(delayValue),
|
eq(delayValue),
|
||||||
eq(TimeUnit.NANOSECONDS));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -536,4 +537,55 @@ public class MeshCaCertificateProviderTest {
|
||||||
assertThat(receivedZoneValues.poll()).isEqualTo("us-west2-a");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue