Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(dot/rpc/subscription): Implement WebSocket connection synchronization for testing #3399

Merged
merged 10 commits into from
Jul 25, 2023
12 changes: 11 additions & 1 deletion dot/rpc/subscription/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func setupWSConn(t *testing.T) (*WSConn, *websocket.Conn, func()) {
CheckOrigin: func(r *http.Request) bool { return true },
}

// Use a channel to notify when the WebSocket connection is ready
connReady := make(chan struct{})

h := func(w http.ResponseWriter, r *http.Request) {
c, err := up.Upgrade(w, r, nil)
if err != nil {
Expand All @@ -30,17 +33,24 @@ func setupWSConn(t *testing.T) (*WSConn, *websocket.Conn, func()) {
}

wskt.Wsconn = c

// Notify that the WebSocket connection is ready
close(connReady)
}

server := httptest.NewServer(http.HandlerFunc(h))
defer server.Close()

wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
ws, r, err := websocket.DefaultDialer.Dial(wsURL, nil)
defer r.Body.Close()
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
r.Body.Close()

require.NoError(t, err)

// Wait for the WebSocket connection to be ready before proceeding
<-connReady
require.NotNil(t, wskt.Wsconn)

cancel := func() {
server.Close()
ws.Close()
Expand Down
Loading