diff --git a/experimental/ssh/internal/client/websockets_test.go b/experimental/ssh/internal/client/websockets_test.go index 05d15636688..aa1f525993e 100644 --- a/experimental/ssh/internal/client/websockets_test.go +++ b/experimental/ssh/internal/client/websockets_test.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/http/httptest" + "sync" "sync/atomic" "testing" "time" @@ -82,8 +83,13 @@ func TestCreateWebsocketConnectionReattachRejected(t *testing.T) { func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { var reattachments atomic.Int32 + // The server does not track hijacked connections, so server.Close does not wait for the + // handlers. The test waits for them itself, so their assertions run before it returns. + var handlers sync.WaitGroup mux := http.NewServeMux() mux.HandleFunc("GET /driver-proxy-api/o/123/cluster/7772/ssh", func(w http.ResponseWriter, r *http.Request) { + handlers.Add(1) + defer handlers.Done() reattach := r.URL.Query().Get("reattach") == "1" if reattach && reattachments.Add(1) == 1 { w.WriteHeader(http.StatusRequestTimeout) @@ -94,14 +100,28 @@ func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { return } defer conn.Close() + // Read every frame the client sends before closing. Closing a socket with unread data + // makes the OS reset the connection, and on Windows the reset discards what the client + // had not read yet, so the frames written here could be lost and the test would see an + // extra drop. + assert.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) if !reattach { assert.NoError(t, conn.WriteMessage(websocket.BinaryMessage, []byte("before"))) + // Drop the connection only once the client has acknowledged "before". + waitForDelivered(t, conn, 6) return } assert.Equal(t, "6", r.URL.Query().Get("delivered")) assert.NoError(t, conn.WriteJSON(map[string]int{"delivered": 0})) assert.NoError(t, conn.WriteMessage(websocket.BinaryMessage, []byte("after"))) assert.NoError(t, conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "finished"))) + // Wait for the client's close frame, discarding its acknowledgments. + for { + if _, _, err := conn.ReadMessage(); err != nil { + assert.True(t, websocket.IsCloseError(err, websocket.CloseNormalClosure), "unexpected read error: %v", err) + return + } + } }) server := httptest.NewServer(mux) defer server.Close() @@ -116,12 +136,40 @@ func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { func(ctx context.Context, req proxy.DialRequest) (*websocket.Conn, error) { return createWebsocketConnection(ctx, client, req, "cluster", 7772, "") }) + // The client dials nothing more once RunClientProxy returns, so no handler can start after + // this Wait begins. + handlersDone := make(chan struct{}) + go func() { + handlers.Wait() + close(handlersDone) + }() + select { + case <-handlersDone: + case <-time.After(10 * time.Second): + t.Fatal("timed out waiting for the server handlers to return") + } require.NoError(t, err) assert.NoError(t, ctx.Err()) assert.Equal(t, int32(2), reattachments.Load()) assert.Equal(t, "beforeafter", output.String()) } +// waitForDelivered reads the client's acknowledgments until one reports exactly delivered bytes +// written to its output, failing the test if the read deadline passes first. +func waitForDelivered(t *testing.T, conn *websocket.Conn, delivered int) { + for { + var msg struct { + Delivered int `json:"delivered"` + } + if err := conn.ReadJSON(&msg); !assert.NoError(t, err) { + return + } + if msg.Delivered == delivered { + return + } + } +} + func TestBuildProxyWebsocketURL(t *testing.T) { tests := []struct { name string