xds: override bootstrap for xds server (#8575)

added xdsServerBuilder method `overrideBootstrapForTest()`. Fix issue https://github.com/grpc/grpc-java/issues/7819
This commit is contained in:
yifeizhuang 2021-10-07 16:17:08 -07:00 committed by GitHub
parent 83d36104e1
commit a2e2f56565
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -37,6 +37,7 @@ import io.grpc.netty.InternalProtocolNegotiator;
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;
@ -136,6 +137,18 @@ public final class XdsServerBuilder extends ForwardingServerBuilder<XdsServerBui
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.

View File

@ -40,7 +40,9 @@ import java.io.IOException;
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;
@ -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 {
@ -77,7 +80,7 @@ public class XdsServerBuilderTest {
builder =
XdsServerBuilder.forPort(
port, XdsServerCredentials.create(InsecureServerCredentials.create()));
builder.xdsClientPoolFactory(new FakeXdsClientPoolFactory(xdsClient));
builder.xdsClientPoolFactory(xdsClientPoolFactory);
if (xdsServingStatusListener != null) {
builder.xdsServingStatusListener(xdsServingStatusListener);
}
@ -292,4 +295,12 @@ public class XdsServerBuilderTest {
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);
}
}

View File

@ -113,6 +113,7 @@ public class XdsServerTestHelper {
implements XdsNameResolverProvider.XdsClientPoolFactory {
private XdsClient xdsClient;
Map<String, ?> savedBootstrap;
FakeXdsClientPoolFactory(XdsClient xdsClient) {
this.xdsClient = xdsClient;
@ -120,7 +121,7 @@ public class XdsServerTestHelper {
@Override
public void setBootstrapOverride(Map<String, ?> bootstrap) {
throw new UnsupportedOperationException("Should not be called");
this.savedBootstrap = bootstrap;
}
@Override