Skip to content

Commit

Permalink
xds: override bootstrap for xds server (#8575)
Browse files Browse the repository at this point in the history
added xdsServerBuilder method `overrideBootstrapForTest()`. Fix issue #7819
  • Loading branch information
YifeiZhuang committed Oct 7, 2021
1 parent 83d3610 commit a2e2f56
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
13 changes: 13 additions & 0 deletions xds/src/main/java/io/grpc/xds/XdsServerBuilder.java
Expand Up @@ -37,6 +37,7 @@
import io.grpc.netty.NettyServerBuilder;
import io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingNegotiatorServerFactory;
import io.grpc.xds.XdsNameResolverProvider.XdsClientPoolFactory;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
Expand Down Expand Up @@ -136,6 +137,18 @@ XdsServerBuilder xdsClientPoolFactory(XdsClientPoolFactory xdsClientPoolFactory)
return this;
}

/**
* Allows providing bootstrap override, useful for testing.
*/
public XdsServerBuilder overrideBootstrapForTest(Map<String, ?> bootstrapOverride) {
checkNotNull(bootstrapOverride, "bootstrapOverride");
if (this.xdsClientPoolFactory == SharedXdsClientPoolProvider.getDefaultProvider()) {
this.xdsClientPoolFactory = new SharedXdsClientPoolProvider();
}
this.xdsClientPoolFactory.setBootstrapOverride(bootstrapOverride);
return this;
}

/**
* Returns the delegate {@link NettyServerBuilder} to allow experimental level
* transport-specific configuration. Note this API will always be experimental.
Expand Down
13 changes: 12 additions & 1 deletion xds/src/test/java/io/grpc/xds/XdsServerBuilderTest.java
Expand Up @@ -40,7 +40,9 @@
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand All @@ -65,6 +67,7 @@ public class XdsServerBuilderTest {
private int port;
private TlsContextManager tlsContextManager;
private FakeXdsClient xdsClient = new FakeXdsClient();
private FakeXdsClientPoolFactory xdsClientPoolFactory = new FakeXdsClientPoolFactory(xdsClient);

private void buildServer(XdsServerBuilder.XdsServingStatusListener xdsServingStatusListener)
throws IOException {
Expand All @@ -77,7 +80,7 @@ private void buildBuilder(XdsServerBuilder.XdsServingStatusListener xdsServingSt
builder =
XdsServerBuilder.forPort(
port, XdsServerCredentials.create(InsecureServerCredentials.create()));
builder.xdsClientPoolFactory(new FakeXdsClientPoolFactory(xdsClient));
builder.xdsClientPoolFactory(xdsClientPoolFactory);
if (xdsServingStatusListener != null) {
builder.xdsServingStatusListener(xdsServingStatusListener);
}
Expand Down Expand Up @@ -292,4 +295,12 @@ public void drainGraceTime_negativeThrows() throws IOException {
assertThat(expected).hasMessageThat().contains("drain grace time");
}
}

@Test
public void testOverrideBootstrap() throws Exception {
Map<String, Object> b = new HashMap<>();
buildBuilder(null);
builder.overrideBootstrapForTest(b);
assertThat(xdsClientPoolFactory.savedBootstrap).isEqualTo(b);
}
}
3 changes: 2 additions & 1 deletion xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java
Expand Up @@ -113,14 +113,15 @@ static final class FakeXdsClientPoolFactory
implements XdsNameResolverProvider.XdsClientPoolFactory {

private XdsClient xdsClient;
Map<String, ?> savedBootstrap;

FakeXdsClientPoolFactory(XdsClient xdsClient) {
this.xdsClient = xdsClient;
}

@Override
public void setBootstrapOverride(Map<String, ?> bootstrap) {
throw new UnsupportedOperationException("Should not be called");
this.savedBootstrap = bootstrap;
}

@Override
Expand Down

0 comments on commit a2e2f56

Please sign in to comment.