MCPcopy
hub / github.com/grpc/grpc-go / TestCZClientSocketMetricsKeepAlive

Method TestCZClientSocketMetricsKeepAlive

test/channelz_test.go:1258–1317  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1256}
1257
1258func (s) TestCZClientSocketMetricsKeepAlive(t *testing.T) {
1259 const keepaliveRate = 50 * time.Millisecond
1260 defer func(t time.Duration) { internal.KeepaliveMinPingTime = t }(internal.KeepaliveMinPingTime)
1261 internal.KeepaliveMinPingTime = keepaliveRate
1262 e := tcpClearRREnv
1263 te := newTest(t, e)
1264 te.customDialOptions = append(te.customDialOptions, grpc.WithKeepaliveParams(
1265 keepalive.ClientParameters{
1266 Time: keepaliveRate,
1267 Timeout: 500 * time.Millisecond,
1268 PermitWithoutStream: true,
1269 }))
1270 te.customServerOptions = append(te.customServerOptions, grpc.KeepaliveEnforcementPolicy(
1271 keepalive.EnforcementPolicy{
1272 MinTime: keepaliveRate,
1273 PermitWithoutStream: true,
1274 }))
1275 te.startServer(&testServer{security: e.security})
1276 cc := te.clientConn() // Dial the server
1277 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1278 defer cancel()
1279 testutils.AwaitState(ctx, t, cc, connectivity.Ready)
1280 start := time.Now()
1281 // Wait for at least two keepalives to be able to occur.
1282 time.Sleep(2 * keepaliveRate)
1283 defer te.tearDown()
1284 if err := verifyResultWithDelay(func() (bool, error) {
1285 tchan, _ := channelz.GetTopChannels(0, 0)
1286 if len(tchan) != 1 {
1287 return false, fmt.Errorf("there should only be one top channel, not %d", len(tchan))
1288 }
1289 subChans := tchan[0].SubChans()
1290 if len(subChans) != 1 {
1291 return false, fmt.Errorf("there should only be one subchannel under top channel %d, not %d", tchan[0].ID, len(subChans))
1292 }
1293 var id int64
1294 for id = range subChans {
1295 break
1296 }
1297 sc := channelz.GetSubChannel(id)
1298 if sc == nil {
1299 return false, fmt.Errorf("there should only be one socket under subchannel %d, not 0", id)
1300 }
1301 skts := sc.Sockets()
1302 if len(skts) != 1 {
1303 return false, fmt.Errorf("there should only be one socket under subchannel %d, not %d", sc.ID, len(skts))
1304 }
1305 for id = range skts {
1306 break
1307 }
1308 skt := channelz.GetSocket(id)
1309 want := int64(time.Since(start) / keepaliveRate)
1310 if got := skt.SocketMetrics.KeepAlivesSent.Load(); got != want {
1311 return false, fmt.Errorf("there should be %v KeepAlives sent, not %d", want, got)
1312 }
1313 return true, nil
1314 }); err != nil {
1315 t.Fatal(err)

Callers

nothing calls this directly

Calls 15

WithKeepaliveParamsFunction · 0.92
AwaitStateFunction · 0.92
GetTopChannelsFunction · 0.92
GetSubChannelFunction · 0.92
GetSocketFunction · 0.92
NowMethod · 0.80
SubChansMethod · 0.80
SocketsMethod · 0.80
newTestFunction · 0.70
verifyResultWithDelayFunction · 0.70
ErrorfMethod · 0.65

Tested by

no test coverage detected