-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathsync_test.go
More file actions
91 lines (74 loc) · 1.61 KB
/
sync_test.go
File metadata and controls
91 lines (74 loc) · 1.61 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build go1.25
// +build go1.25
package statsviz
import (
"context"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"testing/synctest"
"github.com/gorilla/websocket"
)
func TestWsConcurrent(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
srv := newServer(t)
srv.Register(http.NewServeMux())
li := fakeNetListen()
s := &httptest.Server{
Listener: li,
Config: &http.Server{Handler: srv.Ws()},
}
s.Start()
// Build a "ws://" url using the httptest server URL.
u, err := url.Parse(s.URL)
if err != nil {
t.Fatal(err)
}
u.Scheme = "ws"
const numConns = 10
const numMessages = 200
errCh := make(chan error, numConns)
synctestWsDialer := websocket.Dialer{
NetDialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
c := li.connect()
return c, nil
},
}
for i := range numConns {
go func(connID int) {
ws, _, err := synctestWsDialer.Dial(u.String(), nil)
if err != nil {
errCh <- err
return
}
defer ws.Close()
// First message is the plots configuration
var cfg map[string]any
if err := ws.ReadJSON(&cfg); err != nil {
errCh <- err
return
}
// Read multiple data messages to ensure state is being accessed
for range numMessages {
var msg map[string]any
if err := ws.ReadJSON(&msg); err != nil {
errCh <- err
return
}
}
errCh <- nil
}(i)
}
for i := range numConns {
if err := <-errCh; err != nil {
t.Fatalf("connection %d failed: %v", i, err)
}
}
srv.Close()
s.Close()
synctest.Wait()
})
}