| 1 |
// Copyright 2018 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 stack |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"html/template" |
| 11 |
"io" |
| 12 |
"net/url" |
| 13 |
"os" |
| 14 |
"regexp" |
| 15 |
"runtime" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/maruel/panicparse/v2/internal/internaltest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestAggregated_ToHTML_2Buckets(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
buf := bytes.Buffer{} |
| 25 |
if err := getBuckets().ToHTML(&buf, ""); err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
// We expect this to be fairly static across Go versions. We want to know if |
| 29 |
// it changes significantly, thus assert the approximate size. This is being |
| 30 |
// tested on travis. |
| 31 |
if l := buf.Len(); l < 4000 || l > 10000 { |
| 32 |
t.Fatalf("unexpected length %d", l) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestAggregated_ToHTML_1Bucket(t *testing.T) { |
| 37 |
t.Parallel() |
| 38 |
// Exercise a condition when there's only one bucket. |
| 39 |
buf := bytes.Buffer{} |
| 40 |
a := getBuckets() |
| 41 |
a.Buckets = a.Buckets[:1] |
| 42 |
if err := a.ToHTML(&buf, ""); err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
// We expect this to be fairly static across Go versions. We want to know if |
| 46 |
// it changes significantly, thus assert the approximate size. This is being |
| 47 |
// tested on travis. |
| 48 |
if l := buf.Len(); l < 4000 || l > 10000 { |
| 49 |
t.Fatalf("unexpected length %d", l) |
| 50 |
} |
| 51 |
if strings.Contains(buf.String(), "foo-bar") { |
| 52 |
t.Fatal("unexpected") |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestAggregated_ToHTML_1Bucket_Footer(t *testing.T) { |
| 57 |
t.Parallel() |
| 58 |
buf := bytes.Buffer{} |
| 59 |
a := getBuckets() |
| 60 |
a.Buckets = a.Buckets[:1] |
| 61 |
if err := a.ToHTML(&buf, "foo-bar"); err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 64 |
if !strings.Contains(buf.String(), "foo-bar") { |
| 65 |
t.Fatal("expected") |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestGenerate(t *testing.T) { |
| 70 |
t.Parallel() |
| 71 |
// Confirms that nobody forgot to regenate data.go. |
| 72 |
htmlRaw, err := loadGoroutines() |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
if string(htmlRaw) != indexHTML { |
| 77 |
t.Fatal("please run go generate") |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// TestGetSrcBranchURL also tests pkgURL and srcURL and symbol. |
| 82 |
func TestGetSrcBranchURL(t *testing.T) { |
| 83 |
t.Parallel() |
| 84 |
ver := runtime.Version() |
| 85 |
const prefix = "devel +" |
| 86 |
if strings.HasPrefix(ver, prefix) { |
| 87 |
ver = ver[len(prefix) : len(prefix)+10] |
| 88 |
} |
| 89 |
ver = url.QueryEscape(ver) |
| 90 |
data := []struct { |
| 91 |
name string |
| 92 |
c Call |
| 93 |
url, branch template.URL |
| 94 |
pkgURL template.URL |
| 95 |
loc Location |
| 96 |
}{ |
| 97 |
{ |
| 98 |
"stdlib", |
| 99 |
newCallLocal( |
| 100 |
"net/http.(*Server).Serve", |
| 101 |
Args{}, |
| 102 |
goroot+"/src/net/http/server.go", |
| 103 |
2933), |
| 104 |
template.URL("https://github.com/golang/go/blob/" + ver + "/src/net/http/server.go#L2933"), |
| 105 |
template.URL(ver), |
| 106 |
"https://golang.org/pkg/net/http#Server.Serve", |
| 107 |
Stdlib, |
| 108 |
}, |
| 109 |
{ |
| 110 |
"gomodref", |
| 111 |
newCallLocal( |
| 112 |
"github.com/mattn/go-colorable.(*NonColorable).Write", |
| 113 |
Args{}, |
| 114 |
gopath+"/pkg/mod/github.com/mattn/[email protected]/noncolorable.go", |
| 115 |
30), |
| 116 |
"https://github.com/mattn/go-colorable/blob/v0.1.6/noncolorable.go#L30", |
| 117 |
"v0.1.6", |
| 118 |
"https://pkg.go.dev/github.com/mattn/[email protected]#NonColorable.Write", |
| 119 |
GoPkg, |
| 120 |
}, |
| 121 |
/* TODO(maruel): Fix this. |
| 122 |
{ |
| 123 |
"gomodref_with_dot", |
| 124 |
newCallLocal( |
| 125 |
"gopkg.in/fsnotify%2ev1.NewWatcher", |
| 126 |
Args{}, |
| 127 |
gopath+"/pkg/mod/gopkg.in/[email protected]/inotify.go", |
| 128 |
59), |
| 129 |
"file:////home/user/go/pkg/mod/gopkg.in/[email protected]/inotify.go", |
| 130 |
"v1.4.7", |
| 131 |
"https://pkg.go.dev/gopkg.in/[email protected]#NewWatcher", |
| 132 |
GoPkg, |
| 133 |
}, |
| 134 |
*/ |
| 135 |
{ |
| 136 |
"gomod_commit_ref", |
| 137 |
newCallLocal( |
| 138 |
"golang.org/x/sys/unix.Nanosleep", |
| 139 |
Args{}, |
| 140 |
gopath+"/pkg/mod/golang.org/x/[email protected]/unix/zsyscall_linux_amd64.go", |
| 141 |
1160), |
| 142 |
"https://github.com/golang/sys/blob/d5e6a3e2c0ae/unix/zsyscall_linux_amd64.go#L1160", |
| 143 |
"v0.0.0-20200223170610-d5e6a3e2c0ae", |
| 144 |
"https://pkg.go.dev/golang.org/x/[email protected]/unix#Nanosleep", |
| 145 |
GoPkg, |
| 146 |
}, |
| 147 |
{ |
| 148 |
"vendor", |
| 149 |
newCallLocal( |
| 150 |
"github.com/maruel/panicparse/vendor/golang.org/x/sys/unix.Nanosleep", |
| 151 |
Args{}, |
| 152 |
gopath+"/src/github.com/maruel/panicparse/vendor/golang.org/x/sys/unix/zsyscall_linux_amd64.go", |
| 153 |
1100), |
| 154 |
"https://github.com/golang/sys/blob/master/unix/zsyscall_linux_amd64.go#L1100", |
| 155 |
"master", |
| 156 |
"https://godoc.org/golang.org/x/sys/unix#Nanosleep", |
| 157 |
GOPATH, |
| 158 |
}, |
| 159 |
{ |
| 160 |
"windows", |
| 161 |
Call{RemoteSrcPath: "c:/random.go"}, |
| 162 |
"file:///c:/random.go", |
| 163 |
"", |
| 164 |
"", |
| 165 |
LocationUnknown, |
| 166 |
}, |
| 167 |
{ |
| 168 |
"windows_local", |
| 169 |
Call{LocalSrcPath: "c:/random.go"}, |
| 170 |
"file:///c:/random.go", |
| 171 |
"", |
| 172 |
"", |
| 173 |
LocationUnknown, |
| 174 |
}, |
| 175 |
{ |
| 176 |
"empty", |
| 177 |
Call{}, |
| 178 |
"", |
| 179 |
"", |
| 180 |
"", |
| 181 |
LocationUnknown, |
| 182 |
}, |
| 183 |
} |
| 184 |
for i, line := range data { |
| 185 |
line := line |
| 186 |
t.Run(fmt.Sprintf("%d-%s", i, line.name), func(t *testing.T) { |
| 187 |
t.Parallel() |
| 188 |
url, branch := getSrcBranchURL(&line.c) |
| 189 |
if url != line.url { |
| 190 |
t.Errorf("%q != %q", url, line.url) |
| 191 |
} |
| 192 |
if branch != line.branch { |
| 193 |
t.Errorf("%q != %q", branch, line.branch) |
| 194 |
} |
| 195 |
if url := srcURL(&line.c); url != line.url { |
| 196 |
t.Errorf("%q != %q", url, line.url) |
| 197 |
} |
| 198 |
if url := pkgURL(&line.c); url != line.pkgURL { |
| 199 |
t.Errorf("%q != %q", url, line.pkgURL) |
| 200 |
} |
| 201 |
if line.c.Location != line.loc { |
| 202 |
t.Errorf("%s != %s", line.loc, line.c.Location) |
| 203 |
} |
| 204 |
}) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestSymbol(t *testing.T) { |
| 209 |
t.Parallel() |
| 210 |
data := []struct { |
| 211 |
in Func |
| 212 |
want template.URL |
| 213 |
}{ |
| 214 |
{ |
| 215 |
newFunc("github.com/mattn/go-colorable.(*NonColorable).Write"), |
| 216 |
"NonColorable.Write", |
| 217 |
}, |
| 218 |
{ |
| 219 |
newFunc("golang.org/x/sys/unix.Nanosleep"), |
| 220 |
"Nanosleep", |
| 221 |
}, |
| 222 |
{ |
| 223 |
Func{}, |
| 224 |
"", |
| 225 |
}, |
| 226 |
{ |
| 227 |
newFunc("main.baz"), |
| 228 |
"baz", |
| 229 |
}, |
| 230 |
} |
| 231 |
for i, line := range data { |
| 232 |
line := line |
| 233 |
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 234 |
if s := symbol(&line.in); s != line.want { |
| 235 |
t.Fatalf("%q != %q", s, line.want) |
| 236 |
} |
| 237 |
}) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestSnapshot_ToHTML(t *testing.T) { |
| 242 |
t.Parallel() |
| 243 |
data := internaltest.PanicOutputs()["race"] |
| 244 |
if data == nil { |
| 245 |
t.Skip("-race is unsupported on this platform") |
| 246 |
} |
| 247 |
s, _, err := ScanSnapshot(bytes.NewReader(data), io.Discard, DefaultOpts()) |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
if s.Goroutines == nil { |
| 252 |
t.Fatal("missing context") |
| 253 |
} |
| 254 |
if s.Goroutines[0].RaceAddr == 0 { |
| 255 |
t.Fatal("expected a race") |
| 256 |
} |
| 257 |
if !s.IsRace() { |
| 258 |
t.Fatal("expected a race") |
| 259 |
} |
| 260 |
if err := s.ToHTML(io.Discard, ""); err != nil { |
| 261 |
t.Fatal(err) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func BenchmarkAggregated_ToHTML(b *testing.B) { |
| 266 |
b.ReportAllocs() |
| 267 |
s, _, err := ScanSnapshot(bytes.NewReader(internaltest.StaticPanicwebOutput()), io.Discard, DefaultOpts()) |
| 268 |
if err != io.EOF { |
| 269 |
b.Fatal(err) |
| 270 |
} |
| 271 |
if s == nil { |
| 272 |
b.Fatal("missing context") |
| 273 |
} |
| 274 |
a := s.Aggregate(AnyPointer) |
| 275 |
b.ResetTimer() |
| 276 |
for i := 0; i < b.N; i++ { |
| 277 |
if err := a.ToHTML(io.Discard, ""); err != nil { |
| 278 |
b.Fatal(err) |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// |
| 284 |
|
| 285 |
// loadGoroutines should match what is in regen.go. |
| 286 |
func loadGoroutines() ([]byte, error) { |
| 287 |
htmlRaw, err := os.ReadFile("goroutines.tpl") |
| 288 |
if err != nil { |
| 289 |
return nil, err |
| 290 |
} |
| 291 |
// Strip out leading whitespace. |
| 292 |
re := regexp.MustCompile("(\n[ \t]*)+") |
| 293 |
htmlRaw = re.ReplaceAll(htmlRaw, []byte("\n")) |
| 294 |
return htmlRaw, nil |
| 295 |
} |
| 296 |
|
| 297 |
// getBuckets returns a slice for testing. |
| 298 |
func getBuckets() *Aggregated { |
| 299 |
return &Aggregated{ |
| 300 |
Snapshot: &Snapshot{ |
| 301 |
LocalGOROOT: runtime.GOROOT(), |
| 302 |
LocalGOPATHs: []string{"/gopath"}, |
| 303 |
RemoteGOROOT: "/golang", |
| 304 |
RemoteGOPATHs: map[string]string{"/gopath": "/gopath"}, |
| 305 |
LocalGomods: map[string]string{"/tmp": "example.com/foo"}, |
| 306 |
}, |
| 307 |
Buckets: []*Bucket{ |
| 308 |
{ |
| 309 |
Signature: Signature{ |
| 310 |
State: "chan receive", |
| 311 |
Stack: Stack{ |
| 312 |
Calls: []Call{ |
| 313 |
newCall( |
| 314 |
"main.funcĀ·001", |
| 315 |
Args{Values: []Arg{{Value: 0x11000000}, {Value: 2}}}, |
| 316 |
"/gopath/src/github.com/maruel/panicparse/stack/stack.go", |
| 317 |
72), |
| 318 |
{ |
| 319 |
Func: newFunc("sliceInternal"), |
| 320 |
Args: Args{Values: []Arg{{Value: 0x11000000}, {Value: 2}}}, |
| 321 |
RemoteSrcPath: "/golang/src/sort/slices.go", |
| 322 |
Line: 72, |
| 323 |
Location: Stdlib, |
| 324 |
}, |
| 325 |
{ |
| 326 |
Func: newFunc("Slice"), |
| 327 |
Args: Args{Values: []Arg{{Value: 0x11000000}, {Value: 2}}}, |
| 328 |
RemoteSrcPath: "/golang/src/sort/slices.go", |
| 329 |
Line: 72, |
| 330 |
Location: Stdlib, |
| 331 |
}, |
| 332 |
newCall( |
| 333 |
"DoStuff", |
| 334 |
Args{Values: []Arg{{Value: 0x11000000}, {Value: 2}}}, |
| 335 |
"/gopath/src/foo/bar.go", |
| 336 |
72), |
| 337 |
newCall( |
| 338 |
"doStuffInternal", |
| 339 |
Args{ |
| 340 |
Values: []Arg{{Value: 0x11000000}, {Value: 2}}, |
| 341 |
Elided: true, |
| 342 |
}, |
| 343 |
"/gopath/src/foo/bar.go", |
| 344 |
72), |
| 345 |
}, |
| 346 |
}, |
| 347 |
}, |
| 348 |
IDs: []int{1, 2}, |
| 349 |
First: true, |
| 350 |
}, |
| 351 |
{ |
| 352 |
IDs: []int{3}, |
| 353 |
Signature: Signature{ |
| 354 |
State: "running", |
| 355 |
Stack: Stack{Elided: true}, |
| 356 |
}, |
| 357 |
}, |
| 358 |
}, |
| 359 |
} |
| 360 |
} |