| 1 |
// Copyright 2016 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 internal |
| 6 |
|
| 7 |
import ( |
| 8 |
"path" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maruel/panicparse/v2/stack" |
| 14 |
) |
| 15 |
|
| 16 |
var testPalette = &Palette{ |
| 17 |
EOLReset: "A", |
| 18 |
RoutineFirst: "B", |
| 19 |
Routine: "C", |
| 20 |
CreatedBy: "D", |
| 21 |
Package: "E", |
| 22 |
SrcFile: "F", |
| 23 |
FuncMain: "G", |
| 24 |
FuncLocationUnknown: "H", |
| 25 |
FuncLocationUnknownExported: "I", |
| 26 |
FuncGoMod: "J", |
| 27 |
FuncGoModExported: "K", |
| 28 |
FuncGOPATH: "L", |
| 29 |
FuncGOPATHExported: "M", |
| 30 |
FuncGoPkg: "N", |
| 31 |
FuncGoPkgExported: "O", |
| 32 |
FuncStdLib: "P", |
| 33 |
FuncStdLibExported: "Q", |
| 34 |
Arguments: "R", |
| 35 |
} |
| 36 |
|
| 37 |
func TestCalcBucketsLengths(t *testing.T) { |
| 38 |
t.Parallel() |
| 39 |
a := stack.Aggregated{ |
| 40 |
Buckets: []*stack.Bucket{ |
| 41 |
{ |
| 42 |
Signature: stack.Signature{ |
| 43 |
Stack: stack.Stack{ |
| 44 |
Calls: []stack.Call{ |
| 45 |
newCallLocal("main.funcĀ·001", stack.Args{}, "/home/user/go/src/foo/baz.go", 123), |
| 46 |
}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
IDs: []int{}, |
| 50 |
First: true, |
| 51 |
}, |
| 52 |
}, |
| 53 |
} |
| 54 |
srcLen, pkgLen := calcBucketsLengths(&a, fullPath) |
| 55 |
// When printing, it prints the remote path, not the transposed local path. |
| 56 |
compareString(t, "/home/user/go/src/foo/baz.go:123", fullPath.formatCall(&a.Buckets[0].Signature.Stack.Calls[0])) |
| 57 |
compareInt(t, len("/home/user/go/src/foo/baz.go:123"), srcLen) |
| 58 |
compareString(t, "main", a.Buckets[0].Signature.Stack.Calls[0].Func.ImportPath) |
| 59 |
compareInt(t, len("main"), pkgLen) |
| 60 |
|
| 61 |
srcLen, pkgLen = calcBucketsLengths(&a, basePath) |
| 62 |
compareString(t, "baz.go:123", basePath.formatCall(&a.Buckets[0].Signature.Stack.Calls[0])) |
| 63 |
compareInt(t, len("baz.go:123"), srcLen) |
| 64 |
compareString(t, "main", a.Buckets[0].Signature.Stack.Calls[0].Func.ImportPath) |
| 65 |
compareInt(t, len("main"), pkgLen) |
| 66 |
} |
| 67 |
|
| 68 |
func TestBucketHeader(t *testing.T) { |
| 69 |
t.Parallel() |
| 70 |
b := stack.Bucket{ |
| 71 |
Signature: stack.Signature{ |
| 72 |
State: "chan receive", |
| 73 |
CreatedBy: stack.Stack{ |
| 74 |
Calls: []stack.Call{ |
| 75 |
newCallLocal("main.mainImpl", stack.Args{}, "/home/user/go/src/github.com/foo/bar/baz.go", 74), |
| 76 |
}, |
| 77 |
}, |
| 78 |
SleepMax: 6, |
| 79 |
SleepMin: 2, |
| 80 |
}, |
| 81 |
IDs: []int{1, 2}, |
| 82 |
First: true, |
| 83 |
} |
| 84 |
// When printing, it prints the remote path, not the transposed local path. |
| 85 |
compareString(t, "B2: chan receive [2~6 minutes]D [Created by main.mainImpl @ /home/user/go/src/github.com/foo/bar/baz.go:74]A\n", testPalette.BucketHeader(&b, fullPath, true)) |
| 86 |
compareString(t, "C2: chan receive [2~6 minutes]D [Created by main.mainImpl @ /home/user/go/src/github.com/foo/bar/baz.go:74]A\n", testPalette.BucketHeader(&b, fullPath, false)) |
| 87 |
compareString(t, "B2: chan receive [2~6 minutes]D [Created by main.mainImpl @ github.com/foo/bar/baz.go:74]A\n", testPalette.BucketHeader(&b, relPath, true)) |
| 88 |
compareString(t, "C2: chan receive [2~6 minutes]D [Created by main.mainImpl @ github.com/foo/bar/baz.go:74]A\n", testPalette.BucketHeader(&b, relPath, false)) |
| 89 |
compareString(t, "B2: chan receive [2~6 minutes]D [Created by main.mainImpl @ baz.go:74]A\n", testPalette.BucketHeader(&b, basePath, true)) |
| 90 |
compareString(t, "C2: chan receive [2~6 minutes]D [Created by main.mainImpl @ baz.go:74]A\n", testPalette.BucketHeader(&b, basePath, false)) |
| 91 |
|
| 92 |
b = stack.Bucket{ |
| 93 |
Signature: stack.Signature{ |
| 94 |
State: "b0rked", |
| 95 |
SleepMax: 6, |
| 96 |
SleepMin: 6, |
| 97 |
Locked: true, |
| 98 |
}, |
| 99 |
IDs: []int{}, |
| 100 |
First: true, |
| 101 |
} |
| 102 |
compareString(t, "C0: b0rked [6 minutes] [locked]A\n", testPalette.BucketHeader(&b, basePath, false)) |
| 103 |
} |
| 104 |
|
| 105 |
func TestStackLines(t *testing.T) { |
| 106 |
t.Parallel() |
| 107 |
s := &stack.Signature{ |
| 108 |
State: "idle", |
| 109 |
Stack: stack.Stack{ |
| 110 |
Calls: []stack.Call{ |
| 111 |
newCallLocal( |
| 112 |
"runtime.Epollwait", |
| 113 |
stack.Args{ |
| 114 |
Values: []stack.Arg{ |
| 115 |
{Value: 4}, |
| 116 |
{Value: 0x7fff671c7118}, |
| 117 |
{Value: 0xffffffff00000080}, |
| 118 |
{}, |
| 119 |
{Value: 0xffffffff0028c1be}, |
| 120 |
{}, |
| 121 |
{}, |
| 122 |
{}, |
| 123 |
{}, |
| 124 |
{}, |
| 125 |
}, |
| 126 |
Elided: true, |
| 127 |
}, |
| 128 |
"/goroot/src/runtime/sys_linux_amd64.s", |
| 129 |
400), |
| 130 |
newCallLocal( |
| 131 |
"runtime.netpoll", |
| 132 |
stack.Args{Values: []stack.Arg{{Value: 0x901b01}, {}}}, |
| 133 |
"/goroot/src/runtime/netpoll_epoll.go", |
| 134 |
68), |
| 135 |
newCallLocal( |
| 136 |
"main.Main", |
| 137 |
stack.Args{Values: []stack.Arg{{Value: 0xc208012000}}}, |
| 138 |
"/home/user/go/src/main.go", |
| 139 |
1472), |
| 140 |
newCallLocal( |
| 141 |
"foo.OtherExported", |
| 142 |
stack.Args{}, |
| 143 |
"/home/user/go/src/foo/bar.go", |
| 144 |
1575), |
| 145 |
newCallLocal( |
| 146 |
"foo.otherPrivate", |
| 147 |
stack.Args{}, |
| 148 |
"/home/user/go/src/foo/bar.go", |
| 149 |
10), |
| 150 |
}, |
| 151 |
Elided: true, |
| 152 |
}, |
| 153 |
} |
| 154 |
// When printing, it prints the remote path, not the transposed local path. |
| 155 |
want := "" + |
| 156 |
" Eruntime F/goroot/src/runtime/sys_linux_amd64.s:400 QEpollwaitR(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" + |
| 157 |
" Eruntime F/goroot/src/runtime/netpoll_epoll.go:68 PnetpollR(0x901b01, 0)A\n" + |
| 158 |
" Emain F/home/user/go/src/main.go:1472 GMainR(0xc208012000)A\n" + |
| 159 |
" Efoo F/home/user/go/src/foo/bar.go:1575 MOtherExportedR()A\n" + |
| 160 |
" Efoo F/home/user/go/src/foo/bar.go:10 LotherPrivateR()A\n" + |
| 161 |
" (...)\n" |
| 162 |
compareString(t, want, testPalette.StackLines(s, 10, 10, fullPath)) |
| 163 |
want = "" + |
| 164 |
" Eruntime Fsys_linux_amd64.s:400 QEpollwaitR(4, 0x7fff671c7118, 0xffffffff00000080, 0, 0xffffffff0028c1be, 0, 0, 0, 0, 0, ...)A\n" + |
| 165 |
" Eruntime Fnetpoll_epoll.go:68 PnetpollR(0x901b01, 0)A\n" + |
| 166 |
" Emain Fmain.go:1472 GMainR(0xc208012000)A\n" + |
| 167 |
" Efoo Fbar.go:1575 MOtherExportedR()A\n" + |
| 168 |
" Efoo Fbar.go:10 LotherPrivateR()A\n" + |
| 169 |
" (...)\n" |
| 170 |
compareString(t, want, testPalette.StackLines(s, 10, 10, basePath)) |
| 171 |
} |
| 172 |
|
| 173 |
// |
| 174 |
|
| 175 |
func newFunc(s string) stack.Func { |
| 176 |
f := stack.Func{} |
| 177 |
if err := f.Init(s); err != nil { |
| 178 |
panic(err) |
| 179 |
} |
| 180 |
return f |
| 181 |
} |
| 182 |
|
| 183 |
func newCallLocal(f string, a stack.Args, s string, l int) stack.Call { |
| 184 |
c := stack.Call{Func: newFunc(f), Args: a, RemoteSrcPath: s, Line: l} |
| 185 |
// Do the equivalent of Call.init(). |
| 186 |
c.SrcName = filepath.Base(c.RemoteSrcPath) |
| 187 |
c.DirSrc = path.Join(filepath.Base(c.RemoteSrcPath[:len(c.RemoteSrcPath)-len(c.SrcName)-1]), c.SrcName) |
| 188 |
const goroot = "/goroot/src/" |
| 189 |
const gopath = "/home/user/go/src/" |
| 190 |
const gopathmod = "/home/user/go/pkg/mod/" |
| 191 |
// Do the equivalent of Call.updateLocations(). |
| 192 |
if strings.HasPrefix(s, goroot) { |
| 193 |
c.LocalSrcPath = s |
| 194 |
c.RelSrcPath = s[len(goroot):] |
| 195 |
c.Location = stack.Stdlib |
| 196 |
} else if strings.HasPrefix(s, gopath) { |
| 197 |
c.LocalSrcPath = s |
| 198 |
c.RelSrcPath = s[len(gopath):] |
| 199 |
c.Location = stack.GOPATH |
| 200 |
} else if strings.HasPrefix(s, gopathmod) { |
| 201 |
c.LocalSrcPath = s |
| 202 |
c.RelSrcPath = s[len(gopathmod):] |
| 203 |
c.Location = stack.GoPkg |
| 204 |
} |
| 205 |
return c |
| 206 |
} |
| 207 |
|
| 208 |
func compareInt(t *testing.T, want, got int) { |
| 209 |
if want != got { |
| 210 |
t.Helper() |
| 211 |
t.Fatalf("%d != %d", want, got) |
| 212 |
} |
| 213 |
} |