From 318fef809ac115e509d09a22257c03cd8e2c83c0 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Tue, 18 Jan 2022 12:38:04 -0800 Subject: [PATCH] xds: Handle negative random numbers in c2p resolver 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 '-'. --- .../java/io/grpc/xds/GoogleCloudToProdNameResolver.java | 2 +- .../io/grpc/xds/GoogleCloudToProdNameResolverTest.java | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/xds/src/main/java/io/grpc/xds/GoogleCloudToProdNameResolver.java b/xds/src/main/java/io/grpc/xds/GoogleCloudToProdNameResolver.java index 2845d0a00e8..3ec0434a22a 100644 --- a/xds/src/main/java/io/grpc/xds/GoogleCloudToProdNameResolver.java +++ b/xds/src/main/java/io/grpc/xds/GoogleCloudToProdNameResolver.java @@ -175,7 +175,7 @@ public void run() { private ImmutableMap generateBootstrap(String zone, boolean supportIpv6) { ImmutableMap.Builder 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)); } diff --git a/xds/src/test/java/io/grpc/xds/GoogleCloudToProdNameResolverTest.java b/xds/src/test/java/io/grpc/xds/GoogleCloudToProdNameResolverTest.java index 58be7108405..7c777b84bf7 100644 --- a/xds/src/test/java/io/grpc/xds/GoogleCloudToProdNameResolverTest.java +++ b/xds/src/test/java/io/grpc/xds/GoogleCloudToProdNameResolverTest.java @@ -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 errorCaptor; private boolean originalIsOnGcp; @@ -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; @@ -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); } @@ -183,7 +181,7 @@ public void onGcpAndNoProvidedBootstrapDelegateToXds() { Map bootstrap = fakeXdsClientPoolFactory.bootstrapRef.get(); Map node = (Map) 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 server = Iterables.getOnlyElement(