|
46 | 46 | import java.net.InetAddress; |
47 | 47 | import java.net.InetSocketAddress; |
48 | 48 | import java.net.UnknownHostException; |
| 49 | +import java.util.ArrayList; |
49 | 50 | import java.util.Collections; |
50 | 51 | import java.util.List; |
51 | 52 | import java.util.concurrent.Executor; |
@@ -211,6 +212,40 @@ public ConfigOrError answer(InvocationOnMock invocation) { |
211 | 212 | verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName); |
212 | 213 | } |
213 | 214 |
|
| 215 | + @Test |
| 216 | + public void resolve_presentResourceResolver_manyBalancerAddressesSummarizedInToString() |
| 217 | + throws Exception { |
| 218 | + int addressCount = 1000; |
| 219 | + InetAddress backendAddr = InetAddress.getByAddress(new byte[] {127, 0, 0, 0}); |
| 220 | + String lbName = "foo.example.com."; // original name in SRV record |
| 221 | + SrvRecord srvRecord = new SrvRecord(lbName, 8080); |
| 222 | + List<InetAddress> lbAddrs = new ArrayList<>(); |
| 223 | + for (int i = 0; i < addressCount; i++) { |
| 224 | + lbAddrs.add(InetAddress.getByAddress(new byte[] {10, 1, 0, (byte) (i + 1)})); |
| 225 | + } |
| 226 | + AddressResolver mockAddressResolver = mock(AddressResolver.class); |
| 227 | + when(mockAddressResolver.resolveAddress(hostName)) |
| 228 | + .thenReturn(Collections.singletonList(backendAddr)); |
| 229 | + when(mockAddressResolver.resolveAddress(lbName)) |
| 230 | + .thenReturn(lbAddrs); |
| 231 | + ResourceResolver mockResourceResolver = mock(ResourceResolver.class); |
| 232 | + when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.<String>emptyList()); |
| 233 | + when(mockResourceResolver.resolveSrv(anyString())) |
| 234 | + .thenReturn(Collections.singletonList(srvRecord)); |
| 235 | + |
| 236 | + resolver.setAddressResolver(mockAddressResolver); |
| 237 | + resolver.setResourceResolver(mockResourceResolver); |
| 238 | + |
| 239 | + resolver.start(mockListener); |
| 240 | + assertThat(fakeClock.runDueTasks()).isEqualTo(1); |
| 241 | + verify(mockListener).onResult2(resultCaptor.capture()); |
| 242 | + ResolutionResult result = resultCaptor.getValue(); |
| 243 | + EquivalentAddressGroup resolvedBalancerAddr = |
| 244 | + Iterables.getOnlyElement(result.getAttributes().get(GrpclbConstants.ATTR_LB_ADDRS)); |
| 245 | + assertThat(resolvedBalancerAddr.getAddresses().size()).isEqualTo(addressCount); |
| 246 | + assertThat(resolvedBalancerAddr.toString()).contains("... "); |
| 247 | + } |
| 248 | + |
214 | 249 | @Test |
215 | 250 | public void resolve_nullResourceResolver() throws Exception { |
216 | 251 | InetAddress backendAddr = InetAddress.getByAddress(new byte[] {127, 0, 0, 0}); |
@@ -341,4 +376,5 @@ public void resolve_addressAndBalancersLookupFail_neverLookupServiceConfig() thr |
341 | 376 | verify(mockResourceResolver, never()).resolveTxt("_grpc_config." + hostName); |
342 | 377 | verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName); |
343 | 378 | } |
| 379 | + |
344 | 380 | } |
0 commit comments