Skip to content

Commit

Permalink
Make websocket heartbeat test timing less flaky
Browse files Browse the repository at this point in the history
Kubernetes-commit: 4499c3fadcd468678663fed28a2fcf066d2daafe
  • Loading branch information
liggitt authored and k8s-publishing-bot committed Feb 29, 2024
1 parent 0058eee commit 8636987
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tools/remotecommand/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,16 @@ func TestWebSocketClient_BadHandshake(t *testing.T) {
// TestWebSocketClient_HeartbeatTimeout tests the heartbeat by forcing a
// timeout by setting the ping period greater than the deadline.
func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
blockRequestCtx, unblockRequest := context.WithCancel(context.Background())
defer unblockRequest()
// Create fake WebSocket server which blocks.
websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req))
if err != nil {
t.Fatalf("error on webSocketServerStreams: %v", err)
}
defer conns.conn.Close()
// Block server; heartbeat timeout (or test timeout) will fire before this returns.
time.Sleep(1 * time.Second)
<-blockRequestCtx.Done()
}))
defer websocketServer.Close()
// Create websocket client connecting to fake server.
Expand All @@ -840,8 +841,8 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
}
streamExec := exec.(*wsStreamExecutor)
// Ping period is greater than the ping deadline, forcing the timeout to fire.
pingPeriod := 20 * time.Millisecond
pingDeadline := 5 * time.Millisecond
pingPeriod := wait.ForeverTestTimeout // this lets the heartbeat deadline expire without renewing it
pingDeadline := time.Second // this gives setup 1 second to establish streams
streamExec.heartbeatPeriod = pingPeriod
streamExec.heartbeatDeadline = pingDeadline
// Send some random data to the websocket server through STDIN.
Expand All @@ -859,8 +860,7 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
}()

select {
case <-time.After(pingPeriod * 5):
// Give up after about five ping attempts
case <-time.After(wait.ForeverTestTimeout):
t.Fatalf("expected heartbeat timeout, got none.")
case err := <-errorChan:
// Expecting heartbeat timeout error.
Expand Down

0 comments on commit 8636987

Please sign in to comment.