@@ -1165,25 +1165,32 @@ func (s) TestCZClientAndServerSocketMetricsFlowControl(t *testing.T) {
1165
1165
}
1166
1166
1167
1167
func (s ) TestCZClientSocketMetricsKeepAlive (t * testing.T ) {
1168
+ const keepaliveRate = 50 * time .Millisecond
1168
1169
czCleanup := channelz .NewChannelzStorageForTesting ()
1169
1170
defer czCleanupWrapper (czCleanup , t )
1170
1171
defer func (t time.Duration ) { internal .KeepaliveMinPingTime = t }(internal .KeepaliveMinPingTime )
1171
- internal .KeepaliveMinPingTime = time . Second
1172
+ internal .KeepaliveMinPingTime = keepaliveRate
1172
1173
e := tcpClearRREnv
1173
1174
te := newTest (t , e )
1174
1175
te .customDialOptions = append (te .customDialOptions , grpc .WithKeepaliveParams (
1175
1176
keepalive.ClientParameters {
1176
- Time : time . Second ,
1177
+ Time : keepaliveRate ,
1177
1178
Timeout : 500 * time .Millisecond ,
1178
1179
PermitWithoutStream : true ,
1179
1180
}))
1180
1181
te .customServerOptions = append (te .customServerOptions , grpc .KeepaliveEnforcementPolicy (
1181
1182
keepalive.EnforcementPolicy {
1182
- MinTime : 500 * time . Millisecond ,
1183
+ MinTime : keepaliveRate ,
1183
1184
PermitWithoutStream : true ,
1184
1185
}))
1185
1186
te .startServer (& testServer {security : e .security })
1186
- te .clientConn () // Dial the server
1187
+ cc := te .clientConn () // Dial the server
1188
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1189
+ defer cancel ()
1190
+ awaitState (ctx , t , cc , connectivity .Ready )
1191
+ start := time .Now ()
1192
+ // Wait for at least two keepalives to be able to occur.
1193
+ time .Sleep (2 * keepaliveRate )
1187
1194
defer te .tearDown ()
1188
1195
if err := verifyResultWithDelay (func () (bool , error ) {
1189
1196
tchan , _ := channelz .GetTopChannels (0 , 0 )
@@ -1208,8 +1215,9 @@ func (s) TestCZClientSocketMetricsKeepAlive(t *testing.T) {
1208
1215
break
1209
1216
}
1210
1217
skt := channelz .GetSocket (id )
1211
- if skt .SocketData .KeepAlivesSent != 2 {
1212
- return false , fmt .Errorf ("there should be 2 KeepAlives sent, not %d" , skt .SocketData .KeepAlivesSent )
1218
+ want := int64 (time .Since (start ) / keepaliveRate )
1219
+ if skt .SocketData .KeepAlivesSent != want {
1220
+ return false , fmt .Errorf ("there should be %v KeepAlives sent, not %d" , want , skt .SocketData .KeepAlivesSent )
1213
1221
}
1214
1222
return true , nil
1215
1223
}); err != nil {
0 commit comments