Skip to content

Commit d65d394

Browse files
committed
xds: Increase speed of fallback test
These changes reduce connect_then_mainServerDown_fallbackServerUp test time from 20 seconds to 5 s by faking time for the the does-no-exist timer. XdsClientImpl only uses the TimeProvider for CSDS cache details, so any implementation should be fine. FakeXdsClient provides an implementation, so might as well use it as it is one less clock to think about.
1 parent 82403b9 commit d65d394

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

xds/src/test/java/io/grpc/xds/XdsClientFallbackTest.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,13 @@ private static void verifyNoSubscribers(ControlPlaneRule rule) {
335335

336336
// This test takes a long time because of the 16 sec timeout for non-existent resource
337337
@Test
338-
public void connect_then_mainServerDown_fallbackServerUp() throws InterruptedException {
338+
public void connect_then_mainServerDown_fallbackServerUp() throws Exception {
339339
mainXdsServer.restartXdsServer();
340340
fallbackServer.restartXdsServer();
341-
xdsClient = xdsClientPool.getObject();
341+
XdsClientImpl xdsClient = CommonBootstrapperTestUtils.createXdsClient(
342+
new GrpcBootstrapperImpl().bootstrap(defaultBootstrapOverride()),
343+
DEFAULT_XDS_TRANSPORT_FACTORY, fakeClock, new ExponentialBackoffPolicy.Provider(),
344+
MessagePrinter.INSTANCE, xdsClientMetricReporter);
342345

343346
xdsClient.watchXdsResource(XdsListenerResource.getInstance(), MAIN_SERVER, ldsWatcher);
344347

@@ -349,7 +352,12 @@ public void connect_then_mainServerDown_fallbackServerUp() throws InterruptedExc
349352
verify(rdsWatcher, timeout(5000)).onChanged(any());
350353

351354
mainXdsServer.getServer().shutdownNow();
352-
TimeUnit.SECONDS.sleep(5); // TODO(lsafran) Use FakeClock so test runs faster
355+
// Sleep for the ADS stream disconnect to be processed and for the retry to fail. Between those
356+
// two sleeps we need the fakeClock to progress by 1 second to restart the ADS stream.
357+
for (int i = 0; i < 5; i++) {
358+
fakeClock.forwardTime(1000, TimeUnit.MILLISECONDS);
359+
TimeUnit.SECONDS.sleep(1);
360+
}
353361

354362
// Shouldn't do fallback since all watchers are loaded
355363
verify(ldsWatcher, never()).onChanged(
@@ -372,7 +380,7 @@ public void connect_then_mainServerDown_fallbackServerUp() throws InterruptedExc
372380
XdsListenerResource.LdsUpdate.forApiListener(FALLBACK_HTTP_CONNECTION_MANAGER));
373381
verify(ldsWatcher2, timeout(5000)).onChanged(
374382
XdsListenerResource.LdsUpdate.forApiListener(FALLBACK_HTTP_CONNECTION_MANAGER));
375-
verify(cdsWatcher, timeout(16000)).onChanged(any());
383+
verify(cdsWatcher, timeout(5000)).onChanged(any());
376384

377385
xdsClient.watchXdsResource(
378386
XdsRouteConfigureResource.getInstance(), FALLBACK_RDS_NAME, rdsWatcher3);
@@ -381,7 +389,10 @@ public void connect_then_mainServerDown_fallbackServerUp() throws InterruptedExc
381389
// Test that resource defined in main but not fallback is handled correctly
382390
xdsClient.watchXdsResource(
383391
XdsClusterResource.getInstance(), CLUSTER_NAME, cdsWatcher2);
384-
verify(cdsWatcher2, timeout(16000)).onResourceDoesNotExist(eq(CLUSTER_NAME));
392+
verify(cdsWatcher2, never()).onResourceDoesNotExist(eq(CLUSTER_NAME));
393+
fakeClock.forwardTime(15000, TimeUnit.MILLISECONDS); // Does not exist timer
394+
verify(cdsWatcher2, timeout(5000)).onResourceDoesNotExist(eq(CLUSTER_NAME));
395+
xdsClient.shutdown();
385396
}
386397

387398
@Test

xds/src/test/java/io/grpc/xds/client/CommonBootstrapperTestUtils.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.grpc.internal.BackoffPolicy;
2424
import io.grpc.internal.FakeClock;
2525
import io.grpc.internal.JsonParser;
26-
import io.grpc.internal.TimeProvider;
2726
import io.grpc.xds.client.Bootstrapper.ServerInfo;
2827
import io.grpc.xds.internal.security.CommonTlsContextTestsUtil;
2928
import io.grpc.xds.internal.security.TlsContextManagerImpl;
@@ -32,28 +31,12 @@
3231
import java.util.HashMap;
3332
import java.util.List;
3433
import java.util.Map;
35-
import java.util.concurrent.TimeUnit;
3634
import javax.annotation.Nullable;
3735

3836
public class CommonBootstrapperTestUtils {
3937
private static final ChannelCredentials CHANNEL_CREDENTIALS = InsecureChannelCredentials.create();
4038
private static final String SERVER_URI_CUSTOM_AUTHORITY = "trafficdirector2.googleapis.com";
4139
private static final String SERVER_URI_EMPTY_AUTHORITY = "trafficdirector3.googleapis.com";
42-
43-
private static final long TIME_INCREMENT = TimeUnit.SECONDS.toNanos(1);
44-
45-
/** Fake time provider increments time TIME_INCREMENT each call. */
46-
private static TimeProvider newTimeProvider() {
47-
return new TimeProvider() {
48-
private long count;
49-
50-
@Override
51-
public long currentTimeNanos() {
52-
return ++count * TIME_INCREMENT;
53-
}
54-
};
55-
}
56-
5740
private static final String FILE_WATCHER_CONFIG = "{\"path\": \"/etc/secret/certs\"}";
5841
private static final String MESHCA_CONFIG =
5942
"{\n"
@@ -183,14 +166,28 @@ public static XdsClientImpl createXdsClient(List<String> serverUris,
183166
BackoffPolicy.Provider backoffPolicyProvider,
184167
MessagePrettyPrinter messagePrinter,
185168
XdsClientMetricReporter xdsClientMetricReporter) {
186-
Bootstrapper.BootstrapInfo bootstrapInfo = buildBootStrap(serverUris);
169+
return createXdsClient(
170+
buildBootStrap(serverUris),
171+
xdsTransportFactory,
172+
fakeClock,
173+
backoffPolicyProvider,
174+
messagePrinter,
175+
xdsClientMetricReporter);
176+
}
177+
178+
public static XdsClientImpl createXdsClient(Bootstrapper.BootstrapInfo bootstrapInfo,
179+
XdsTransportFactory xdsTransportFactory,
180+
FakeClock fakeClock,
181+
BackoffPolicy.Provider backoffPolicyProvider,
182+
MessagePrettyPrinter messagePrinter,
183+
XdsClientMetricReporter xdsClientMetricReporter) {
187184
return new XdsClientImpl(
188185
xdsTransportFactory,
189186
bootstrapInfo,
190187
fakeClock.getScheduledExecutorService(),
191188
backoffPolicyProvider,
192189
fakeClock.getStopwatchSupplier(),
193-
newTimeProvider(),
190+
fakeClock.getTimeProvider(),
194191
messagePrinter,
195192
new TlsContextManagerImpl(bootstrapInfo),
196193
xdsClientMetricReporter);

0 commit comments

Comments
 (0)