Make websocket heartbeat test timing less flaky

Kubernetes-commit: 26484df2108eff8ad6e06dfc960eae3bdfbf4663
This commit is contained in:
Jordan Liggitt 2024-02-29 15:10:28 -05:00 committed by Kubernetes Publisher
parent d99a76c51e
commit 36a771f98c

View File

@ -817,6 +817,8 @@ func TestWebSocketClient_BadHandshake(t *testing.T) {
// TestWebSocketClient_HeartbeatTimeout tests the heartbeat by forcing a // TestWebSocketClient_HeartbeatTimeout tests the heartbeat by forcing a
// timeout by setting the ping period greater than the deadline. // timeout by setting the ping period greater than the deadline.
func TestWebSocketClient_HeartbeatTimeout(t *testing.T) { func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
blockRequestCtx, unblockRequest := context.WithCancel(context.Background())
defer unblockRequest()
// Create fake WebSocket server which blocks. // Create fake WebSocket server which blocks.
websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req)) conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req))
@ -824,8 +826,7 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
t.Fatalf("error on webSocketServerStreams: %v", err) t.Fatalf("error on webSocketServerStreams: %v", err)
} }
defer conns.conn.Close() defer conns.conn.Close()
// Block server; heartbeat timeout (or test timeout) will fire before this returns. <-blockRequestCtx.Done()
time.Sleep(1 * time.Second)
})) }))
defer websocketServer.Close() defer websocketServer.Close()
// Create websocket client connecting to fake server. // Create websocket client connecting to fake server.
@ -840,8 +841,8 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
} }
streamExec := exec.(*wsStreamExecutor) streamExec := exec.(*wsStreamExecutor)
// Ping period is greater than the ping deadline, forcing the timeout to fire. // Ping period is greater than the ping deadline, forcing the timeout to fire.
pingPeriod := 20 * time.Millisecond pingPeriod := wait.ForeverTestTimeout // this lets the heartbeat deadline expire without renewing it
pingDeadline := 5 * time.Millisecond pingDeadline := time.Second // this gives setup 1 second to establish streams
streamExec.heartbeatPeriod = pingPeriod streamExec.heartbeatPeriod = pingPeriod
streamExec.heartbeatDeadline = pingDeadline streamExec.heartbeatDeadline = pingDeadline
// Send some random data to the websocket server through STDIN. // Send some random data to the websocket server through STDIN.
@ -859,8 +860,7 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
}() }()
select { select {
case <-time.After(pingPeriod * 5): case <-time.After(wait.ForeverTestTimeout):
// Give up after about five ping attempts
t.Fatalf("expected heartbeat timeout, got none.") t.Fatalf("expected heartbeat timeout, got none.")
case err := <-errorChan: case err := <-errorChan:
// Expecting heartbeat timeout error. // Expecting heartbeat timeout error.