Debian Codegraph @

stack/webstack/webstack_test.go

raw
1 // Copyright 2020 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package webstack
6
7 import (
8 "context"
9 "net/http/httptest"
10 "sync"
11 "testing"
12 )
13
14 func TestSnapshotHandler(t *testing.T) {
15 data := []string{
16 "/debug",
17 "/debug?augment=1",
18 "/debug?maxmem=1",
19 "/debug?maxmem=2097152",
20 "/debug?similarity=exactflags",
21 "/debug?similarity=exactlines",
22 "/debug?similarity=anypointer",
23 "/debug?similarity=anyvalue",
24 }
25 for _, url := range data {
26 url := url
27 t.Run(url, func(t *testing.T) {
28 req := httptest.NewRequest("GET", url, nil)
29 w := httptest.NewRecorder()
30 SnapshotHandler(w, req)
31 if w.Code != 200 {
32 t.Fatalf("%s: %d\n%s", url, w.Code, w.Body.String())
33 }
34 })
35 }
36 }
37
38 func TestSnapshotHandler_Err(t *testing.T) {
39 t.Parallel()
40 data := []string{
41 "/debug?augment=2",
42 "/debug?maxmem=abc",
43 "/debug?similarity=alike",
44 }
45 for _, url := range data {
46 url := url
47 t.Run(url, func(t *testing.T) {
48 t.Parallel()
49 req := httptest.NewRequest("GET", url, nil)
50 w := httptest.NewRecorder()
51 SnapshotHandler(w, req)
52 if w.Code != 400 {
53 t.Fatalf("%s: %d\n%s", url, w.Code, w.Body.String())
54 }
55 })
56 }
57 }
58
59 func TestSnapshotHandler_Method_POST(t *testing.T) {
60 t.Parallel()
61 req := httptest.NewRequest("POST", "/debug", nil)
62 w := httptest.NewRecorder()
63 SnapshotHandler(w, req)
64 if w.Code != 405 {
65 t.Fatalf("%d\n%s", w.Code, w.Body.String())
66 }
67 }
68
69 func TestSnapshotHandler_LargeMemory(t *testing.T) {
70 // Try to create a stack frame over 1MiB in size when serialized to string.
71 // This is tricky since this is dependent on many factors out of our control.
72 // Do this by starting a lot of callbacks with a lot of arguments.
73 wg := sync.WaitGroup{}
74 ctx, cancel := context.WithCancel(context.Background())
75 defer cancel()
76 a := 0
77 alive := make(chan struct{})
78 // Assuming >400 bytes per goroutine, 2500 parallel goroutines is enough to
79 // use more than 1MiB of call stack. We must not put it too high or it'll
80 // crash on Travis.
81 const parallel = 2500
82 for i := 0; i < parallel; i++ {
83 wg.Add(1)
84 go func() {
85 defer wg.Done()
86 alive <- struct{}{}
87 dummy(ctx, &a, &a, &a, &a, &a, &a, &a, &a, &a)
88 }()
89 }
90 for i := 0; i < parallel; i++ {
91 <-alive
92 }
93
94 // Normal.
95 req := httptest.NewRequest("GET", "/debug", nil)
96 w := httptest.NewRecorder()
97 SnapshotHandler(w, req)
98 if w.Code != 200 {
99 t.Fatalf("%d\n%s", w.Code, w.Body.String())
100 }
101
102 // Cut off. That's 1<<20 + 1
103 req = httptest.NewRequest("GET", "/debug?maxmem=1048577", nil)
104 w = httptest.NewRecorder()
105 SnapshotHandler(w, req)
106 // It can result in a 500 because the cut off is arbitrary, making parsing to
107 // fail.
108 if w.Code != 200 && w.Code != 500 {
109 t.Fatalf("%d\n%s", w.Code, w.Body.String())
110 }
111
112 cancel()
113 wg.Wait()
114 }
115
116 func BenchmarkSnapshotHandle(b *testing.B) {
117 // TODO(maruel): We should hook runtime.Stack() to make it a deterministic
118 // output with internaltest.StaticPanicwebOutput().
119 b.ReportAllocs()
120 req := httptest.NewRequest("GET", "/", nil)
121 b.ResetTimer()
122 for i := 0; i < b.N; i++ {
123 w := httptest.NewRecorder()
124 SnapshotHandler(w, req)
125 if w.Code != 200 {
126 b.Fatalf("%d\n%s", w.Code, w.Body.String())
127 }
128 }
129 }
130
131 func dummy(ctx context.Context, a1, a2, a3, a4, a5, a6, a7, a8, a9 *int) {
132 <-ctx.Done()
133 }