-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathstatsviz_test.go
More file actions
236 lines (184 loc) · 5.25 KB
/
Copy pathstatsviz_test.go
File metadata and controls
236 lines (184 loc) · 5.25 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package statsviz
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/arl/statsviz/internal/static"
)
func testIndex(t *testing.T, f http.Handler, url string) {
t.Helper()
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
resp := w.Result()
httpindex, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Errorf("http status %v, want %v", resp.StatusCode, http.StatusOK)
}
if resp.Header.Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("header[Content-Type] %s, want %s", resp.Header.Get("Content-Type"), "text/html; charset=utf-8")
}
fhtml, err := static.Assets().Open("index.html")
if err != nil {
t.Fatalf("couldn't read index.html from assets Fs: %v", err)
}
fsindex, _ := io.ReadAll(fhtml)
if !bytes.Equal(fsindex, httpindex) {
t.Errorf("read body is not that of index.html from assets")
}
}
func newServer(tb testing.TB, opts ...Option) *Server {
tb.Helper()
srv, err := NewServer(opts...)
if err != nil {
tb.Fatal(err)
}
return srv
}
func TestIndex(t *testing.T) {
t.Parallel()
srv := newServer(t)
testIndex(t, srv.Index(), "http://example.com/debug/statsviz/")
}
func TestRoot(t *testing.T) {
t.Parallel()
testIndex(t, newServer(t, Root("/debug/")).Index(), "http://example.com/debug/")
testIndex(t, newServer(t, Root("/debug")).Index(), "http://example.com/debug/")
testIndex(t, newServer(t, Root("/")).Index(), "http://example.com/")
testIndex(t, newServer(t, Root("/test/")).Index(), "http://example.com/test/")
}
func testWs(t *testing.T, f http.Handler, URL string) {
t.Helper()
s := httptest.NewServer(f)
defer s.Close()
// Build a "ws://" url using the httptest server URL and the URL argument.
u1, err := url.Parse(s.URL)
if err != nil {
t.Fatal(err)
}
u2, err := url.Parse(URL)
if err != nil {
t.Fatal(err)
}
u1.Scheme = "ws"
u1.Path = u2.Path
// Connect to the server
ws, _, err := websocket.DefaultDialer.Dial(u1.String(), nil)
if err != nil {
t.Fatalf("%v", err)
}
defer ws.Close()
// First message is the plots configuration.
var cfg map[string]any
if err := ws.ReadJSON(&cfg); err != nil {
t.Fatalf("failed reading json from websocket: %v", err)
}
// Check the content of 2 consecutive payloads.
for range 2 {
// Verifies that we've received:
// - 1 time series (cgo)
// - 1 heatmap (sizeClasses).
var msg struct {
Event string `json:"event"`
Data struct {
Series struct {
CGo []uint64 `json:"cgo"`
SizeClasses []uint64 `json:"size-classes"`
} `json:"series"`
} `json:"data"`
}
if err := ws.ReadJSON(&msg); err != nil {
t.Fatalf("failed reading json from websocket: %v", err)
}
// The time series must have one and only one element
if len(msg.Data.Series.CGo) != 1 {
t.Errorf("len(cgo) = %d, want 1", len(msg.Data.Series.CGo))
}
// Heatmaps should have many elements, check that there's more than one.
if len(msg.Data.Series.SizeClasses) <= 1 {
t.Errorf("len(sizeClasses) = %d, want > 1", len(msg.Data.Series.SizeClasses))
}
}
}
func TestWsCantUpgrade(t *testing.T) {
url := "http://example.com/debug/statsviz/ws"
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
newServer(t).Ws()(w, req)
if w.Result().StatusCode != http.StatusBadRequest {
t.Errorf("responded %v to %q with non-websocket-upgradable conn, want %v", w.Result().StatusCode, url, http.StatusBadRequest)
}
}
func testRegister(t *testing.T, f http.Handler, baseURL string) {
testIndex(t, f, baseURL)
ws := strings.TrimRight(baseURL, "/") + "/ws"
testWs(t, f, ws)
}
func TestRegister(t *testing.T) {
t.Run("defaultmux", func(t *testing.T) {
t.Parallel()
mux := http.DefaultServeMux
Register(mux)
testRegister(t, mux, "http://example.com/debug/statsviz/")
})
t.Run("default", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
newServer(t).Register(mux)
testRegister(t, mux, "http://example.com/debug/statsviz/")
})
t.Run("zero-value", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
var srv Server
srv.Register(mux)
testRegister(t, mux, "http://example.com/debug/statsviz/")
})
t.Run("root", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
srv := newServer(t, Root(""))
srv.Register(mux)
testRegister(t, mux, "http://example.com/")
})
t.Run("slash", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
srv := newServer(t, Root("/"))
srv.Register(mux)
testRegister(t, mux, "http://example.com/")
})
t.Run("root2", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
srv := newServer(t, Root("/path/to/statsviz"))
srv.Register(mux)
testRegister(t, mux, "http://example.com/path/to/statsviz/")
})
t.Run("root+frequency", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
srv := newServer(t,
Root("/path/to/statsviz"),
SendFrequency(100*time.Millisecond),
)
srv.Register(mux)
testRegister(t, mux, "http://example.com/path/to/statsviz/")
})
t.Run("non-positive frequency", func(t *testing.T) {
t.Parallel()
_, err := NewServer(
Root("/path/to/statsviz"),
SendFrequency(-1),
)
if err == nil {
t.Errorf("NewServer() should have errored")
}
})
}