-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathwebstack_test.go
More file actions
133 lines (122 loc) · 3.28 KB
/
webstack_test.go
File metadata and controls
133 lines (122 loc) · 3.28 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
// Copyright 2020 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package webstack
import (
"context"
"net/http/httptest"
"sync"
"testing"
)
func TestSnapshotHandler(t *testing.T) {
data := []string{
"/debug",
"/debug?augment=1",
"/debug?maxmem=1",
"/debug?maxmem=2097152",
"/debug?similarity=exactflags",
"/debug?similarity=exactlines",
"/debug?similarity=anypointer",
"/debug?similarity=anyvalue",
}
for _, url := range data {
url := url
t.Run(url, func(t *testing.T) {
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
SnapshotHandler(w, req)
if w.Code != 200 {
t.Fatalf("%s: %d\n%s", url, w.Code, w.Body.String())
}
})
}
}
func TestSnapshotHandler_Err(t *testing.T) {
t.Parallel()
data := []string{
"/debug?augment=2",
"/debug?maxmem=abc",
"/debug?similarity=alike",
}
for _, url := range data {
url := url
t.Run(url, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
SnapshotHandler(w, req)
if w.Code != 400 {
t.Fatalf("%s: %d\n%s", url, w.Code, w.Body.String())
}
})
}
}
func TestSnapshotHandler_Method_POST(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("POST", "/debug", nil)
w := httptest.NewRecorder()
SnapshotHandler(w, req)
if w.Code != 405 {
t.Fatalf("%d\n%s", w.Code, w.Body.String())
}
}
func TestSnapshotHandler_LargeMemory(t *testing.T) {
// Try to create a stack frame over 1MiB in size when serialized to string.
// This is tricky since this is dependent on many factors out of our control.
// Do this by starting a lot of callbacks with a lot of arguments.
wg := sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a := 0
alive := make(chan struct{})
// Assuming >400 bytes per goroutine, 2500 parallel goroutines is enough to
// use more than 1MiB of call stack. We must not put it too high or it'll
// crash on Travis.
const parallel = 2500
for i := 0; i < parallel; i++ {
wg.Add(1)
go func() {
defer wg.Done()
alive <- struct{}{}
dummy(ctx, &a, &a, &a, &a, &a, &a, &a, &a, &a)
}()
}
for i := 0; i < parallel; i++ {
<-alive
}
// Normal.
req := httptest.NewRequest("GET", "/debug", nil)
w := httptest.NewRecorder()
SnapshotHandler(w, req)
if w.Code != 200 {
t.Fatalf("%d\n%s", w.Code, w.Body.String())
}
// Cut off. That's 1<<20 + 1
req = httptest.NewRequest("GET", "/debug?maxmem=1048577", nil)
w = httptest.NewRecorder()
SnapshotHandler(w, req)
// It can result in a 500 because the cut off is arbitrary, making parsing to
// fail.
if w.Code != 200 && w.Code != 500 {
t.Fatalf("%d\n%s", w.Code, w.Body.String())
}
cancel()
wg.Wait()
}
func BenchmarkSnapshotHandle(b *testing.B) {
// TODO(maruel): We should hook runtime.Stack() to make it a deterministic
// output with internaltest.StaticPanicwebOutput().
b.ReportAllocs()
req := httptest.NewRequest("GET", "/", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
SnapshotHandler(w, req)
if w.Code != 200 {
b.Fatalf("%d\n%s", w.Code, w.Body.String())
}
}
}
func dummy(ctx context.Context, a1, a2, a3, a4, a5, a6, a7, a8, a9 *int) {
<-ctx.Done()
}