Skip to content

Commit ebe4c48

Browse files
jdcormieAgraVator
authored andcommitted
Add a basic SocketStats with just the local and remote addresses. (#12349)
This will make channelz more useful.
1 parent f4ed575 commit ebe4c48

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

binder/src/main/java/io/grpc/binder/internal/BinderTransport.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import com.google.common.util.concurrent.ListenableFuture;
3232
import com.google.errorprone.annotations.concurrent.GuardedBy;
3333
import io.grpc.Attributes;
34+
import io.grpc.Grpc;
3435
import io.grpc.Internal;
36+
import io.grpc.InternalChannelz;
3537
import io.grpc.InternalChannelz.SocketStats;
3638
import io.grpc.InternalLogId;
3739
import io.grpc.Status;
@@ -205,7 +207,15 @@ public final ScheduledExecutorService getScheduledExecutorService() {
205207

206208
// Override in child class.
207209
public final ListenableFuture<SocketStats> getStats() {
208-
return immediateFuture(null);
210+
Attributes attributes = getAttributes();
211+
return immediateFuture(
212+
new InternalChannelz.SocketStats(
213+
/* data= */ null, // TODO: Keep track of these stats with TransportTracer or similar.
214+
/* local= */ attributes.get(Grpc.TRANSPORT_ATTR_LOCAL_ADDR),
215+
/* remote= */ attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR),
216+
// TODO: SocketOptions are meaningless for binder but we're still forced to provide one.
217+
new InternalChannelz.SocketOptions.Builder().build(),
218+
/* security= */ null));
209219
}
210220

211221
// Override in child class.

binder/src/test/java/io/grpc/binder/internal/RobolectricBinderTransportTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import androidx.test.core.content.pm.PackageInfoBuilder;
4040
import com.google.common.collect.ImmutableList;
4141
import io.grpc.Attributes;
42+
import io.grpc.InternalChannelz.SocketStats;
4243
import io.grpc.ServerStreamTracer;
4344
import io.grpc.Status;
4445
import io.grpc.binder.AndroidComponentAddress;
@@ -52,6 +53,7 @@
5253
import io.grpc.internal.GrpcUtil;
5354
import io.grpc.internal.InternalServer;
5455
import io.grpc.internal.ManagedClientTransport;
56+
import io.grpc.internal.MockServerTransportListener;
5557
import io.grpc.internal.ObjectPool;
5658
import io.grpc.internal.SharedResourcePool;
5759
import java.util.List;
@@ -301,13 +303,30 @@ public void clientIgnoresDuplicateSetupTransaction() throws Exception {
301303
}
302304

303305
assertThat(((ConnectionClientTransport) client).getAttributes().get(REMOTE_UID))
304-
.isEqualTo(myUid());
306+
.isEqualTo(myUid());
305307
}
306308

307309
@Test
308-
@Ignore("See BinderTransportTest#socketStats.")
309310
@Override
310-
public void socketStats() {}
311+
// We don't quite pass the official/abstract version of this test yet because
312+
// today's binder client and server transports have different ideas of each others' address.
313+
// TODO(#12347): Remove this @Override once this difference is resolved.
314+
public void socketStats() throws Exception {
315+
server.start(serverListener);
316+
ManagedClientTransport client = newClientTransport(server);
317+
startTransport(client, mockClientTransportListener);
318+
319+
SocketStats clientSocketStats = client.getStats().get();
320+
assertThat(clientSocketStats.local).isInstanceOf(AndroidComponentAddress.class);
321+
assertThat(((AndroidComponentAddress) clientSocketStats.remote).getPackage())
322+
.isEqualTo(((AndroidComponentAddress) server.getListenSocketAddress()).getPackage());
323+
324+
MockServerTransportListener serverTransportListener =
325+
serverListener.takeListenerOrFail(TIMEOUT_MS, MILLISECONDS);
326+
SocketStats serverSocketStats = serverTransportListener.transport.getStats().get();
327+
assertThat(serverSocketStats.local).isEqualTo(server.getListenSocketAddress());
328+
assertThat(serverSocketStats.remote).isEqualTo(new BoundClientAddress(myUid()));
329+
}
311330

312331
@Test
313332
@Ignore("See BinderTransportTest#flowControlPushBack")

0 commit comments

Comments
 (0)