Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions experimental/ssh/internal/client/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Comment thread
pietern marked this conversation as resolved.
Resolved
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()
Expand All @@ -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
Expand Down
Loading