Skip to content

Commit 8636987

Browse files
liggittk8s-publishing-bot
authored andcommitted
Make websocket heartbeat test timing less flaky
Kubernetes-commit: 4499c3fadcd468678663fed28a2fcf066d2daafe
1 parent 0058eee commit 8636987

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

tools/remotecommand/websocket_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -817,15 +817,16 @@ func TestWebSocketClient_BadHandshake(t *testing.T) {
817817
// TestWebSocketClient_HeartbeatTimeout tests the heartbeat by forcing a
818818
// timeout by setting the ping period greater than the deadline.
819819
func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
820+
blockRequestCtx, unblockRequest := context.WithCancel(context.Background())
821+
defer unblockRequest()
820822
// Create fake WebSocket server which blocks.
821823
websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
822824
conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req))
823825
if err != nil {
824826
t.Fatalf("error on webSocketServerStreams: %v", err)
825827
}
826828
defer conns.conn.Close()
827-
// Block server; heartbeat timeout (or test timeout) will fire before this returns.
828-
time.Sleep(1 * time.Second)
829+
<-blockRequestCtx.Done()
829830
}))
830831
defer websocketServer.Close()
831832
// Create websocket client connecting to fake server.
@@ -840,8 +841,8 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
840841
}
841842
streamExec := exec.(*wsStreamExecutor)
842843
// Ping period is greater than the ping deadline, forcing the timeout to fire.
843-
pingPeriod := 20 * time.Millisecond
844-
pingDeadline := 5 * time.Millisecond
844+
pingPeriod := wait.ForeverTestTimeout // this lets the heartbeat deadline expire without renewing it
845+
pingDeadline := time.Second // this gives setup 1 second to establish streams
845846
streamExec.heartbeatPeriod = pingPeriod
846847
streamExec.heartbeatDeadline = pingDeadline
847848
// Send some random data to the websocket server through STDIN.
@@ -859,8 +860,7 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
859860
}()
860861

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

0 commit comments

Comments
 (0)