summaryrefslogtreecommitdiff
path: root/internal/flamegraph/liveserver_test.go
blob: 59a37825bf1f05d4a9beb7d7469e066dbcf1da80 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package flamegraph

import (
	"bufio"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"sync"
	"testing"
	"time"
)

func TestHandleSSEContentTypeFormatAndEmptyTrie(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	srv := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	defer srv.Close()

	resp := connectSSE(t, srv.URL)
	defer resp.Body.Close()

	contentType := resp.Header.Get("Content-Type")
	if !strings.HasPrefix(contentType, "text/event-stream") {
		t.Fatalf("Content-Type = %q, want text/event-stream", contentType)
	}

	data := readFirstSSEData(t, resp.Body)
	snap := decodeSSESnapshot(t, data)
	if snap.Total != 0 {
		t.Fatalf("empty trie snapshot total = %d, want 0", snap.Total)
	}
}

func TestHandleSSEMultipleClientsReceiveInitialSnapshot(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	lt.Ingest(newTestPair("multi", 42, 1001, "/tmp/multi", 1, 1, 1))
	srv := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	defer srv.Close()

	const clients = 4
	var wg sync.WaitGroup
	errCh := make(chan error, clients)

	wg.Add(clients)
	for i := 0; i < clients; i++ {
		go func() {
			defer wg.Done()
			resp := connectSSE(t, srv.URL)
			defer resp.Body.Close()
			data := readFirstSSEData(t, resp.Body)
			snap := decodeSSESnapshot(t, data)
			if snap.Total == 0 {
				errCh <- fmt.Errorf("received empty snapshot")
			}
		}()
	}

	wg.Wait()
	close(errCh)
	for err := range errCh {
		t.Fatal(err)
	}
}

func TestHandleSSEReconnectAfterDisconnectGetsLatestSnapshot(t *testing.T) {
	lt := NewLiveTrie([]string{"path"}, "count")
	lt.Ingest(newTestPair("reconnect", 1, 1001, "/tmp/a", 1, 1, 1))
	srv := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	defer srv.Close()

	resp1 := connectSSE(t, srv.URL)
	first := decodeSSESnapshot(t, readFirstSSEData(t, resp1.Body))
	_ = resp1.Body.Close()
	if first.Total != 1 {
		t.Fatalf("first snapshot total = %d, want 1", first.Total)
	}

	lt.Ingest(newTestPair("reconnect", 1, 1002, "/tmp/b", 1, 1, 1))

	resp2 := connectSSE(t, srv.URL)
	defer resp2.Body.Close()
	second := decodeSSESnapshot(t, readFirstSSEData(t, resp2.Body))
	if second.Total != 2 {
		t.Fatalf("reconnected snapshot total = %d, want 2", second.Total)
	}
}

func TestHandleSSERestartedServerAcceptsNewConnection(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	lt.Ingest(newTestPair("restart", 1, 1001, "/tmp/a", 1, 1, 1))

	srv1 := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	resp1 := connectSSE(t, srv1.URL)
	first := decodeSSESnapshot(t, readFirstSSEData(t, resp1.Body))
	_ = resp1.Body.Close()
	srv1.Close()
	if first.Total != 1 {
		t.Fatalf("first server snapshot total = %d, want 1", first.Total)
	}

	lt.Ingest(newTestPair("restart", 1, 1002, "/tmp/b", 1, 1, 1))

	srv2 := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	defer srv2.Close()
	resp2 := connectSSE(t, srv2.URL)
	defer resp2.Body.Close()
	second := decodeSSESnapshot(t, readFirstSSEData(t, resp2.Body))
	if second.Total != 2 {
		t.Fatalf("second server snapshot total = %d, want 2", second.Total)
	}
}

func TestHandleSSEDelayedClientLargeTrieGetsValidSnapshot(t *testing.T) {
	lt := NewLiveTrie([]string{"path"}, "count")
	const events = 12000
	for i := 0; i < events; i++ {
		lt.Ingest(newTestPair("late", 7, uint32(10000+i), fmt.Sprintf("/late/%05d", i), 1, 1, 1))
	}

	srv := httptest.NewServer(handleSSE(lt, 5*time.Millisecond))
	defer srv.Close()

	resp := connectSSE(t, srv.URL)
	defer resp.Body.Close()
	snap := decodeSSESnapshot(t, readFirstSSEData(t, resp.Body))
	if snap.Total != events {
		t.Fatalf("late client snapshot total = %d, want %d", snap.Total, events)
	}
}

func TestHandleResetRequiresPost(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	req := httptest.NewRequest(http.MethodGet, "/reset", nil)
	rec := httptest.NewRecorder()

	handleReset(lt).ServeHTTP(rec, req)

	if rec.Code != http.StatusMethodNotAllowed {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusMethodNotAllowed)
	}
	if allow := rec.Header().Get("Allow"); allow != http.MethodPost {
		t.Fatalf("allow = %q, want %q", allow, http.MethodPost)
	}
}

func TestHandleResetClearsTrieAndReturnsEmptySnapshot(t *testing.T) {
	lt := NewLiveTrie([]string{"path"}, "count")
	lt.Ingest(newTestPair("reset", 1, 1001, "/tmp/a", 1, 1, 1))
	lt.Ingest(newTestPair("reset", 1, 1002, "/tmp/b", 1, 1, 1))
	if before := decodeLiveSnapshot(t, lt); before.Total == 0 {
		t.Fatalf("expected non-empty trie before reset")
	}

	req := httptest.NewRequest(http.MethodPost, "/reset", nil)
	rec := httptest.NewRecorder()
	handleReset(lt).ServeHTTP(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
	}
	if ctype := rec.Header().Get("Content-Type"); !strings.Contains(ctype, "application/json") {
		t.Fatalf("content-type = %q, want application/json", ctype)
	}
	var snap trieSnapshot
	if err := json.Unmarshal(rec.Body.Bytes(), &snap); err != nil {
		t.Fatalf("decode reset snapshot: %v", err)
	}
	if snap.Total != 0 {
		t.Fatalf("reset snapshot total = %d, want 0", snap.Total)
	}

	after := decodeLiveSnapshot(t, lt)
	if after.Total != 0 {
		t.Fatalf("trie total after reset = %d, want 0", after.Total)
	}
}

func TestHandleOrderGetReturnsCurrentFields(t *testing.T) {
	lt := NewLiveTrie([]string{"comm", "path", "tracepoint"}, "count")
	req := httptest.NewRequest(http.MethodGet, "/order", nil)
	rec := httptest.NewRecorder()
	handleOrder(lt).ServeHTTP(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
	}
	var resp orderResponse
	if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if strings.Join(resp.Fields, ",") != "comm,path,tracepoint" {
		t.Fatalf("fields = %v, want [comm path tracepoint]", resp.Fields)
	}
}

func TestHandleOrderPostReconfiguresAndResets(t *testing.T) {
	lt := NewLiveTrie([]string{"comm", "path", "tracepoint"}, "count")
	lt.Ingest(newTestPair("svc", 42, 1001, "/tmp/a", 1, 1, 1))

	req := httptest.NewRequest(http.MethodPost, "/order", strings.NewReader(`{"fields":["path","tracepoint","comm"]}`))
	rec := httptest.NewRecorder()
	handleOrder(lt).ServeHTTP(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
	}
	var resp orderResponse
	if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if strings.Join(resp.Fields, ",") != "path,tracepoint,comm" {
		t.Fatalf("fields = %v, want [path tracepoint comm]", resp.Fields)
	}
	var snap trieSnapshot
	if err := json.Unmarshal(resp.Snapshot, &snap); err != nil {
		t.Fatalf("decode snapshot: %v", err)
	}
	if snap.Total != 0 {
		t.Fatalf("snapshot total after reconfigure = %d, want 0", snap.Total)
	}
}

func TestHandleOrderPostRejectsInvalidRequest(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")

	req := httptest.NewRequest(http.MethodPost, "/order", strings.NewReader(`{"fields":["comm","bogus"]}`))
	rec := httptest.NewRecorder()
	handleOrder(lt).ServeHTTP(rec, req)
	if rec.Code != http.StatusBadRequest {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
	}

	req = httptest.NewRequest(http.MethodPost, "/order", strings.NewReader(`{"fields":[}`))
	rec = httptest.NewRecorder()
	handleOrder(lt).ServeHTTP(rec, req)
	if rec.Code != http.StatusBadRequest {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
	}
}

func TestHandleOrderRequiresGetOrPost(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	req := httptest.NewRequest(http.MethodPut, "/order", nil)
	rec := httptest.NewRecorder()
	handleOrder(lt).ServeHTTP(rec, req)

	if rec.Code != http.StatusMethodNotAllowed {
		t.Fatalf("status = %d, want %d", rec.Code, http.StatusMethodNotAllowed)
	}
	if allow := rec.Header().Get("Allow"); allow != http.MethodGet+", "+http.MethodPost {
		t.Fatalf("allow = %q, want %q", allow, http.MethodGet+", "+http.MethodPost)
	}
}

func TestServeLivePrintsURLAndStopsOnCancel(t *testing.T) {
	lt := NewLiveTrie([]string{"comm"}, "count")
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	output := captureStdout(t, func() {
		errCh := make(chan error, 1)
		go func() {
			errCh <- ServeLive(ctx, lt, 5*time.Millisecond)
		}()

		time.Sleep(40 * time.Millisecond)
		cancel()

		select {
		case err := <-errCh:
			if err != nil {
				t.Fatalf("ServeLive returned error: %v", err)
			}
		case <-time.After(2 * time.Second):
			t.Fatalf("timeout waiting for ServeLive to return")
		}
	})

	if !strings.Contains(output, "Live flamegraph available at http://") {
		t.Fatalf("expected live URL in output, got %q", output)
	}
}

func connectSSE(t *testing.T, url string) *http.Response {
	t.Helper()
	client := &http.Client{Timeout: 5 * time.Second}
	resp, err := client.Get(url)
	if err != nil {
		t.Fatalf("connect sse: %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		_ = resp.Body.Close()
		t.Fatalf("unexpected status: %s", resp.Status)
	}
	return resp
}

func readFirstSSEData(t *testing.T, body io.ReadCloser) string {
	t.Helper()
	type result struct {
		data string
		err  error
	}
	ch := make(chan result, 1)

	go func() {
		reader := bufio.NewReader(body)
		line, err := reader.ReadString('\n')
		if err != nil {
			ch <- result{err: err}
			return
		}
		if !strings.HasPrefix(line, "data: ") {
			ch <- result{err: fmt.Errorf("invalid sse data line: %q", line)}
			return
		}
		separator, err := reader.ReadString('\n')
		if err != nil {
			ch <- result{err: err}
			return
		}
		if separator != "\n" {
			ch <- result{err: fmt.Errorf("missing sse blank-line separator: %q", separator)}
			return
		}
		ch <- result{data: strings.TrimSuffix(strings.TrimPrefix(line, "data: "), "\n")}
	}()

	select {
	case out := <-ch:
		if out.err != nil {
			t.Fatalf("read sse event: %v", out.err)
		}
		return out.data
	case <-time.After(3 * time.Second):
		_ = body.Close()
		t.Fatalf("timeout waiting for first sse event")
		return ""
	}
}

func decodeSSESnapshot(t *testing.T, data string) trieSnapshot {
	t.Helper()
	var snap trieSnapshot
	if err := json.Unmarshal([]byte(data), &snap); err != nil {
		t.Fatalf("invalid snapshot json: %v", err)
	}
	return snap
}

func captureStdout(t *testing.T, fn func()) string {
	t.Helper()

	oldStdout := os.Stdout
	reader, writer, err := os.Pipe()
	if err != nil {
		t.Fatalf("create stdout pipe: %v", err)
	}

	os.Stdout = writer
	defer func() { os.Stdout = oldStdout }()

	outCh := make(chan string, 1)
	go func() {
		var b strings.Builder
		_, _ = io.Copy(&b, reader)
		outCh <- b.String()
	}()

	fn()

	_ = writer.Close()
	out := <-outCh
	_ = reader.Close()
	return out
}