xref: /aosp_15_r20/external/grpc-grpc-java/services/src/test/java/io/grpc/protobuf/services/ChannelzTestHelper.java (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1 /*
2  * Copyright 2018 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.protobuf.services;
18 
19 import com.google.common.base.MoreObjects;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
22 import io.grpc.ConnectivityState;
23 import io.grpc.InternalChannelz;
24 import io.grpc.InternalChannelz.ChannelStats;
25 import io.grpc.InternalChannelz.Security;
26 import io.grpc.InternalChannelz.ServerStats;
27 import io.grpc.InternalChannelz.SocketOptions;
28 import io.grpc.InternalChannelz.SocketStats;
29 import io.grpc.InternalChannelz.TransportStats;
30 import io.grpc.InternalInstrumented;
31 import io.grpc.InternalLogId;
32 import io.grpc.InternalWithLogId;
33 import java.net.InetSocketAddress;
34 import java.net.SocketAddress;
35 import java.util.Collections;
36 
37 /**
38  * Test class definitions that will be used in the proto utils test as well as
39  * channelz service test.
40  */
41 final class ChannelzTestHelper {
42 
43   static final class TestSocket implements InternalInstrumented<SocketStats> {
44     private final InternalLogId id = InternalLogId.allocate("socket", /*details=*/ null);
45     TransportStats transportStats = new TransportStats(
46         /*streamsStarted=*/ 1,
47         /*lastLocalStreamCreatedTimeNanos=*/ 2,
48         /*lastRemoteStreamCreatedTimeNanos=*/ 3,
49         /*streamsSucceeded=*/ 4,
50         /*streamsFailed=*/ 5,
51         /*messagesSent=*/ 6,
52         /*messagesReceived=*/ 7,
53         /*keepAlivesSent=*/ 8,
54         /*lastMessageSentTimeNanos=*/ 9,
55         /*lastMessageReceivedTimeNanos=*/ 10,
56         /*localFlowControlWindow=*/ 11,
57         /*remoteFlowControlWindow=*/ 12);
58     SocketAddress local = new InetSocketAddress("10.0.0.1", 1000);
59     SocketAddress remote = new InetSocketAddress("10.0.0.2", 1000);
60     InternalChannelz.SocketOptions socketOptions
61         = new InternalChannelz.SocketOptions.Builder().build();
62     Security security = null;
63 
64     @Override
getStats()65     public ListenableFuture<SocketStats> getStats() {
66       SettableFuture<SocketStats> ret = SettableFuture.create();
67       ret.set(
68           new SocketStats(
69               transportStats,
70               local,
71               remote,
72               socketOptions,
73               security));
74       return ret;
75     }
76 
77     @Override
getLogId()78     public InternalLogId getLogId() {
79       return id;
80     }
81 
82     @Override
toString()83     public String toString() {
84       return MoreObjects.toStringHelper(this)
85           .add("logId", getLogId())
86           .toString();
87     }
88   }
89 
90   static final class TestListenSocket implements InternalInstrumented<SocketStats> {
91     private final InternalLogId id = InternalLogId.allocate("listensocket", /*details=*/ null);
92     SocketAddress listenAddress = new InetSocketAddress("10.0.0.1", 1234);
93 
94     @Override
getStats()95     public ListenableFuture<SocketStats> getStats() {
96       SettableFuture<SocketStats> ret = SettableFuture.create();
97       ret.set(
98           new SocketStats(
99               /*data=*/ null,
100               listenAddress,
101               /*remote=*/ null,
102               new SocketOptions.Builder().addOption("listen_option", "listen_option_value").build(),
103               /*security=*/ null));
104       return ret;
105     }
106 
107     @Override
getLogId()108     public InternalLogId getLogId() {
109       return id;
110     }
111 
112     @Override
toString()113     public String toString() {
114       return MoreObjects.toStringHelper(this)
115           .add("logId", getLogId())
116           .toString();
117     }
118   }
119 
120   static final class TestServer implements InternalInstrumented<ServerStats> {
121     private final InternalLogId id = InternalLogId.allocate("server", /*details=*/ null);
122     ServerStats serverStats = new ServerStats(
123         /*callsStarted=*/ 1,
124         /*callsSucceeded=*/ 2,
125         /*callsFailed=*/ 3,
126         /*lastCallStartedNanos=*/ 4,
127         Collections.<InternalInstrumented<SocketStats>>emptyList());
128 
129     @Override
getStats()130     public ListenableFuture<ServerStats> getStats() {
131       SettableFuture<ServerStats> ret = SettableFuture.create();
132       ret.set(serverStats);
133       return ret;
134     }
135 
136     @Override
getLogId()137     public InternalLogId getLogId() {
138       return id;
139     }
140 
141     @Override
toString()142     public String toString() {
143       return MoreObjects.toStringHelper(this)
144           .add("logId", getLogId())
145           .toString();
146     }
147   }
148 
149   static final class TestChannel implements InternalInstrumented<ChannelStats> {
150     private final InternalLogId id =
151         InternalLogId.allocate("channel-or-subchannel", /*details=*/ null);
152 
153     ChannelStats stats = new ChannelStats.Builder()
154         .setTarget("sometarget")
155         .setState(ConnectivityState.READY)
156         .setCallsStarted(1)
157         .setCallsSucceeded(2)
158         .setCallsFailed(3)
159         .setLastCallStartedNanos(4)
160         .setSubchannels(Collections.<InternalWithLogId>emptyList())
161         .setSockets(Collections.<InternalWithLogId>emptyList())
162         .build();
163 
164     @Override
getStats()165     public ListenableFuture<ChannelStats> getStats() {
166       SettableFuture<ChannelStats> ret = SettableFuture.create();
167       ret.set(stats);
168       return ret;
169     }
170 
171     @Override
getLogId()172     public InternalLogId getLogId() {
173       return id;
174     }
175 
176     @Override
toString()177     public String toString() {
178       return MoreObjects.toStringHelper(this)
179           .add("logId", getLogId())
180           .toString();
181     }
182   }
183 }
184