-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathws_test.go
More file actions
77 lines (64 loc) · 2.14 KB
/
Copy pathws_test.go
File metadata and controls
77 lines (64 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package hyperliquid
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
)
func TestNewWebsocketClient(t *testing.T) {
require.PanicsWithValue(t,
"baseURL must have a scheme set, either wss or ws",
func() { _ = NewWebsocketClient("foobar.com") },
)
require.NotPanics(t,
func() { _ = NewWebsocketClient(MainnetAPIURL) },
"Mainnet should always work",
)
require.NotPanics(t,
func() { _ = NewWebsocketClient("") },
"empty URL should default to Mainnet",
)
}
func TestWsOptReadTimeout(t *testing.T) {
client := NewWebsocketClient(MainnetAPIURL, WsOptReadTimeout(42*time.Second))
require.Equal(t, 42*time.Second, client.readTimeout)
}
// TestReadPumpReconnectsOnTimeout spins up a WebSocket server that accepts
// connections but never sends a message. The client should time out and
// reconnect, resulting in more than one TCP-level upgrade.
func TestReadPumpReconnectsOnTimeout(t *testing.T) {
upgrader := websocket.Upgrader{
CheckOrigin: func(*http.Request) bool { return true },
}
var connectCount atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
connectCount.Add(1)
// Hold the connection open; drain any frames the client sends (e.g. ping)
// so the TCP link itself stays alive — only application-layer data is absent.
for {
if _, _, err := conn.ReadMessage(); err != nil {
_ = conn.Close()
return
}
}
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// 200 ms timeout gives us several reconnect cycles within the 3 s context.
client := NewWebsocketClient(server.URL, WsOptReadTimeout(200*time.Millisecond))
require.NoError(t, client.Connect(ctx))
// Allow enough wall-clock time for multiple timeout → reconnect cycles.
time.Sleep(2 * time.Second)
require.GreaterOrEqual(t, int(connectCount.Load()), 2,
"client should have reconnected at least once after read timeout")
require.NoError(t, client.Close())
}