Debian Codegraph @

browsertests/testutils.go

raw symbols SCIP
1 package browsertests
2
3 import (
4 "fmt"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "regexp"
9 "runtime"
10 "testing"
11 "time"
12
13 "github.com/google/pprof/driver"
14 "github.com/google/pprof/profile"
15 )
16
17 func makeTestServer(t testing.TB, prof *profile.Profile) *httptest.Server {
18 if runtime.GOOS == "nacl" || runtime.GOOS == "js" {
19 t.Skip("test assumes tcp available")
20 }
21
22 // Custom http server creator
23 var server *httptest.Server
24 serverCreated := make(chan bool)
25 creator := func(a *driver.HTTPServerArgs) error {
26 server = httptest.NewServer(http.HandlerFunc(
27 func(w http.ResponseWriter, r *http.Request) {
28 if h := a.Handlers[r.URL.Path]; h != nil {
29 h.ServeHTTP(w, r)
30 }
31 }))
32 serverCreated <- true
33 return nil
34 }
35
36 // Start server and wait for it to be initialized
37 go func() {
38 err := driver.PProf(&driver.Options{
39 Obj: fakeObjTool{},
40 UI: testUI{t},
41 Fetch: testFetcher{prof},
42 HTTPServer: creator,
43 Flagset: testFlags{
44 "http": "unused:1234",
45 "no_browser": true,
46 },
47 })
48 if err != nil {
49 panic(err)
50 }
51 }()
52 <-serverCreated
53
54 // Close the server when the test is done.
55 t.Cleanup(server.Close)
56
57 return server
58 }
59
60 // Fake test implementations of types needed by pprof driver.
61
62 const addrBase = 0x1000
63 const fakeSource = "testdata/file1000.src"
64
65 type fakeObj struct{}
66
67 func (f fakeObj) Close() error { return nil }
68 func (f fakeObj) Name() string { return "testbin" }
69 func (f fakeObj) ObjAddr(addr uint64) (uint64, error) { return addr, nil }
70 func (f fakeObj) BuildID() string { return "" }
71 func (f fakeObj) SourceLine(addr uint64) ([]driver.Frame, error) {
72 return nil, fmt.Errorf("SourceLine unimplemented")
73 }
74 func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*driver.Sym, error) {
75 return []*driver.Sym{
76 {
77 Name: []string{"F1"}, File: fakeSource,
78 Start: addrBase, End: addrBase + 10,
79 },
80 {
81 Name: []string{"F2"}, File: fakeSource,
82 Start: addrBase + 10, End: addrBase + 20,
83 },
84 {
85 Name: []string{"F3"}, File: fakeSource,
86 Start: addrBase + 20, End: addrBase + 30,
87 },
88 }, nil
89 }
90
91 type fakeObjTool struct{}
92
93 func (obj fakeObjTool) Open(file string, start, limit, offset uint64, relocationSymbol string) (driver.ObjFile, error) {
94 return fakeObj{}, nil
95 }
96
97 func (obj fakeObjTool) Disasm(file string, start, end uint64, intelSyntax bool) ([]driver.Inst, error) {
98 return []driver.Inst{
99 {Addr: addrBase + 10, Text: "f1:asm", Function: "F1", Line: 3},
100 {Addr: addrBase + 20, Text: "f2:asm", Function: "F2", Line: 11},
101 {Addr: addrBase + 30, Text: "d3:asm", Function: "F3", Line: 22},
102 }, nil
103 }
104
105 func makeFakeProfile() *profile.Profile {
106 // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
107 funcs := []*profile.Function{
108 {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
109 {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
110 {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
111 }
112 lines := []profile.Line{
113 {Function: funcs[0], Line: 11},
114 {Function: funcs[1], Line: 22},
115 {Function: funcs[2], Line: 33},
116 }
117 mapping := []*profile.Mapping{
118 {
119 ID: 1,
120 Start: addrBase,
121 Limit: addrBase + 100,
122 Offset: 0,
123 File: "testbin",
124 HasFunctions: true,
125 HasFilenames: true,
126 HasLineNumbers: true,
127 },
128 }
129
130 // Three interesting addresses: base+{10,20,30}
131 locs := []*profile.Location{
132 {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
133 {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
134 {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
135 }
136
137 // Two stack traces.
138 return &profile.Profile{
139 PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
140 Period: 1,
141 DurationNanos: 10e9,
142 SampleType: []*profile.ValueType{
143 {Type: "cpu", Unit: "milliseconds"},
144 },
145 Sample: []*profile.Sample{
146 {
147 Location: []*profile.Location{locs[2], locs[1], locs[0]},
148 Value: []int64{100},
149 },
150 {
151 Location: []*profile.Location{locs[1], locs[0]},
152 Value: []int64{200},
153 },
154 },
155 Location: locs,
156 Function: funcs,
157 Mapping: mapping,
158 }
159 }
160
161 type testFlags map[string]any
162
163 func (flags testFlags) Bool(name string, def bool, usage string) *bool {
164 return getFlag(flags, name, def)
165 }
166 func (flags testFlags) Int(name string, def int, usage string) *int {
167 return getFlag(flags, name, def)
168 }
169 func (flags testFlags) Float64(name string, def float64, usage string) *float64 {
170 return getFlag(flags, name, def)
171 }
172 func (flags testFlags) String(name string, def string, usage string) *string {
173 return getFlag(flags, name, def)
174 }
175 func (flags testFlags) StringList(name string, def string, usage string) *[]*string {
176 return getFlag(flags, name, []*string{}) // Not supported, so return an empty list.
177 }
178 func (flags testFlags) ExtraUsage() string { return "" }
179 func (flags testFlags) AddExtraUsage(eu string) {}
180 func (flags testFlags) Parse(usage func()) []string { return []string{"test", "bin"} }
181
182 var _ driver.FlagSet = testFlags{}
183
184 func getFlag[T any](flags testFlags, name string, def T) *T {
185 result := &def
186 if v, ok := flags[name]; ok {
187 *result = v.(T)
188 }
189 return result
190 }
191
192 type testUI struct {
193 T testing.TB
194 }
195
196 func (ui testUI) ReadLine(_ string) (string, error) { return "", io.EOF }
197 func (ui testUI) IsTerminal() bool { return false }
198 func (ui testUI) WantBrowser() bool { return false }
199 func (ui testUI) SetAutoComplete(_ func(string) string) {}
200 func (ui testUI) Print(args ...interface{}) {} // discard
201 func (ui testUI) PrintErr(args ...interface{}) {
202 ui.T.Error("unexpected error: " + fmt.Sprint(args...))
203 }
204
205 var _ driver.UI = testUI{}
206
207 type testFetcher struct {
208 profile *profile.Profile
209 }
210
211 func (f testFetcher) Fetch(source string, duration, timeout time.Duration) (*profile.Profile, string, error) {
212 // http://pproftest.local prevents file from being saved.
213 return f.profile, "http://pproftest.local", nil
214 }
215
216 var _ driver.Fetcher = testFetcher{}