Skip to content

Commit

Permalink
xds: Handle negative random numbers in c2p resolver
Browse files Browse the repository at this point in the history
This was noticed because Mockito can't mock Random in Java 17, so it was
replaced with actual Random. But when doing that change it exposed that
negative numbers would cause the id to have a double '-'.
  • Loading branch information
ejona86 committed Jan 20, 2022
1 parent fcf2caf commit 318fef8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void run() {

private ImmutableMap<String, ?> generateBootstrap(String zone, boolean supportIpv6) {
ImmutableMap.Builder<String, Object> nodeBuilder = ImmutableMap.builder();
nodeBuilder.put("id", "C2P-" + rand.nextInt());
nodeBuilder.put("id", "C2P-" + (rand.nextInt() & Integer.MAX_VALUE));
if (!zone.isEmpty()) {
nodeBuilder.put("locality", ImmutableMap.of("zone", zone));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public void close(Executor instance) {}

@Mock
private NameResolver.Listener2 mockListener;
@Mock
private Random mockRandom;
private Random random = new Random(1);
@Captor
private ArgumentCaptor<Status> errorCaptor;
private boolean originalIsOnGcp;
Expand All @@ -114,7 +113,6 @@ public void close(Executor instance) {}

@Before
public void setUp() {
when(mockRandom.nextInt()).thenReturn(123456789);
nsRegistry.register(new FakeNsProvider("dns"));
nsRegistry.register(new FakeNsProvider("xds"));
originalIsOnGcp = GoogleCloudToProdNameResolver.isOnGcp;
Expand Down Expand Up @@ -146,7 +144,7 @@ public HttpURLConnection createConnection(String url) throws IOException {
}
};
resolver = new GoogleCloudToProdNameResolver(
TARGET_URI, args, fakeExecutorResource, mockRandom, fakeXdsClientPoolFactory,
TARGET_URI, args, fakeExecutorResource, random, fakeXdsClientPoolFactory,
nsRegistry.asFactory());
resolver.setHttpConnectionProvider(httpConnections);
}
Expand Down Expand Up @@ -183,7 +181,7 @@ public void onGcpAndNoProvidedBootstrapDelegateToXds() {
Map<String, ?> bootstrap = fakeXdsClientPoolFactory.bootstrapRef.get();
Map<String, ?> node = (Map<String, ?>) bootstrap.get("node");
assertThat(node).containsExactly(
"id", "C2P-123456789",
"id", "C2P-991614323",
"locality", ImmutableMap.of("zone", ZONE),
"metadata", ImmutableMap.of("TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE", true));
Map<String, ?> server = Iterables.getOnlyElement(
Expand Down

0 comments on commit 318fef8

Please sign in to comment.