Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions api/src/main/java/io/grpc/EquivalentAddressGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
public final class EquivalentAddressGroup {
private static final int MAX_ADDRESSES_TO_STRING = 100;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make it package-private and access it directly from the tests without the use of reflection.


/**
* The authority to be used when constructing Subchannels for this EquivalentAddressGroup.
Expand Down Expand Up @@ -113,8 +114,24 @@ public Attributes getAttributes() {

@Override
public String toString() {
// TODO(zpencer): Summarize return value if addr is very large
return "[" + addrs + "/" + attrs + "]";
StringBuilder sb = new StringBuilder();
sb.append('[');
if (addrs.size() <= MAX_ADDRESSES_TO_STRING) {
sb.append(addrs);
} else {
sb.append('[');
for (int i = 0; i < MAX_ADDRESSES_TO_STRING; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(addrs.get(i));
}
sb.append(", ... ");
sb.append(addrs.size() - MAX_ADDRESSES_TO_STRING);
sb.append(" more]");
}
sb.append('/').append(attrs).append(']');
return sb.toString();
}

@Override
Expand Down
92 changes: 92 additions & 0 deletions api/src/test/java/io/grpc/EquivalentAddressGroupTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2026 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc;

import static com.google.common.truth.Truth.assertThat;

import java.lang.reflect.Field;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Unit tests for {@link EquivalentAddressGroup}.
*/
@RunWith(JUnit4.class)
public class EquivalentAddressGroupTest {

@Test
public void toString_summarizesLargeAddressList() {
int maxAddressesToString = maxAddressesToString();
List<SocketAddress> addrs = new ArrayList<>();
for (int i = 0; i <= maxAddressesToString; i++) {
addrs.add(new FakeSocketAddress("addr" + i));
}
EquivalentAddressGroup eag = new EquivalentAddressGroup(addrs);

StringBuilder expected = new StringBuilder();
expected.append('[').append('[');
for (int i = 0; i < maxAddressesToString; i++) {
if (i > 0) {
expected.append(", ");
}
expected.append(addrs.get(i));
}
expected.append(", ... 1 more]/{}]");
assertThat(eag.toString()).isEqualTo(expected.toString());
}

@Test
public void toString_doesNotSummarizeAtMaxAddressCount() {
int maxAddressesToString = maxAddressesToString();
List<SocketAddress> addrs = new ArrayList<>();
for (int i = 0; i < maxAddressesToString; i++) {
addrs.add(new FakeSocketAddress("addr" + i));
}
EquivalentAddressGroup eag = new EquivalentAddressGroup(addrs);

String expected = "[" + addrs + "/{}]";
assertThat(eag.toString()).isEqualTo(expected);
}

private static int maxAddressesToString() {
try {
Field field = EquivalentAddressGroup.class.getDeclaredField("MAX_ADDRESSES_TO_STRING");
field.setAccessible(true);
return (int) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new LinkageError("Unable to read MAX_ADDRESSES_TO_STRING", e);
}
}

private static final class FakeSocketAddress extends SocketAddress {

private final String name;

FakeSocketAddress(String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}
}