Skip to content

Commit fe8e638

Browse files
committed
stack: make tests more resilient
Use the occasion to rename function arguments to make it easier to understand. Test only change.
1 parent 426572b commit fe8e638

2 files changed

Lines changed: 66 additions & 59 deletions

File tree

stack/context_test.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,9 +2017,9 @@ func testPanicUTF8(t *testing.T, s *Snapshot, b *bytes.Buffer, ppDir string) {
20172017
// not.
20182018
"github.com/maruel/panicparse"+ver+"/cmd/panic/internal/utf8.(*Strùct).Pànic",
20192019
ifCombinedAggregateArgs(
2020-
Args{Values: []Arg{{Value: 1}}},
2020+
Args{Values: []Arg{{Value: 1, IsInaccurate: true}}},
20212021
// else
2022-
Args{Values: []Arg{{Value: 0xc0000b2e48, IsPtr: true}}},
2022+
Args{Values: []Arg{{Value: 0xc0000b2e48, IsPtr: true, IsInaccurate: true}}},
20232023
),
20242024
// See TestCallUTF8 in stack_test.go for exercising the methods on
20252025
// Call in this situation.
@@ -2286,15 +2286,15 @@ func identifyPanicwebSignature(t *testing.T, b *Bucket, pwebDir string) panicweb
22862286
t.Fatal("first bucket is not correct")
22872287
return pstUnknown
22882288
}
2289-
crash := Signature{
2289+
want := Signature{
22902290
State: "running",
22912291
Stack: Stack{
22922292
Calls: []Call{
22932293
newCallLocal("main.main", Args{}, pathJoin(pwebDir, "main.go"), 80),
22942294
},
22952295
},
22962296
}
2297-
similarSignatures(t, &crash, &b.Signature)
2297+
similarSignatures(t, &want, &b.Signature)
22982298
return pstMain
22992299
}
23002300

@@ -2360,13 +2360,15 @@ func identifyPanicwebSignature(t *testing.T, b *Bucket, pwebDir string) panicweb
23602360
}
23612361
// That's the unix.Nanosleep() or windows.SleepEx() call.
23622362
if b.State == "syscall" {
2363-
created := Stack{
2364-
Calls: []Call{
2365-
newCallLocal("main.main", Args{}, pathJoin(pwebDir, "main.go"), 63),
2366-
},
2363+
{
2364+
want := Stack{
2365+
Calls: []Call{
2366+
newCallLocal("main.main", Args{}, pathJoin(pwebDir, "main.go"), 63),
2367+
},
2368+
}
2369+
zapStacks(t, &want, &b.CreatedBy)
2370+
compareStacks(t, &want, &b.CreatedBy)
23672371
}
2368-
zapStacks(t, &created, &b.CreatedBy)
2369-
compareStacks(t, &created, &b.CreatedBy)
23702372
if l := len(b.IDs); l != 1 {
23712373
t.Fatalf("expected 1 goroutine for the signature, got %d", l)
23722374
}
@@ -2412,24 +2414,26 @@ func identifyPanicwebSignature(t *testing.T, b *Bucket, pwebDir string) panicweb
24122414
t.Fatalf("unexpected version string %q", ver)
24132415
}
24142416
}
2415-
rest := []Call{
2416-
newCallLocal("main.sysHang", Args{}, pathJoin(pwebDir, mainOS), 12),
2417-
newCallLocal(
2418-
"main.main.func3",
2419-
ifCombinedAggregateArgs(
2420-
Args{},
2421-
// else
2422-
Args{Values: []Arg{{Value: 0xc000140720, Name: "#135", IsPtr: true}}},
2423-
),
2424-
pathJoin(pwebDir, "main.go"),
2425-
65),
2426-
}
2427-
got := b.Stack.Calls[2:]
2428-
for i := range rest {
2429-
zapCalls(t, &got[i], &rest[i])
2430-
}
2431-
if diff := cmp.Diff(rest, got); diff != "" {
2432-
t.Fatalf("rest of stack mismatch (-want +got):\n%s", diff)
2417+
{
2418+
want := []Call{
2419+
newCallLocal("main.sysHang", Args{}, pathJoin(pwebDir, mainOS), 12),
2420+
newCallLocal(
2421+
"main.main.func3",
2422+
ifCombinedAggregateArgs(
2423+
Args{},
2424+
// else
2425+
Args{Values: []Arg{{Value: 0xc000140720, Name: "#135", IsPtr: true}}},
2426+
),
2427+
pathJoin(pwebDir, "main.go"),
2428+
65),
2429+
}
2430+
got := b.Stack.Calls[2:]
2431+
for i := range want {
2432+
zapCalls(t, &want[i], &got[i])
2433+
}
2434+
if diff := cmp.Diff(want, got); diff != "" {
2435+
t.Fatalf("rest of stack mismatch (-want +got):\n%s", diff)
2436+
}
24332437
}
24342438
return pstStdlib
24352439
}

stack/stack_test.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ func similarGoroutines(t *testing.T, want, got []*Goroutine) {
426426
}
427427
}
428428

429-
func zapGoroutines(t *testing.T, a, b []*Goroutine) {
430-
if len(a) != len(b) {
429+
func zapGoroutines(t *testing.T, want, got []*Goroutine) {
430+
if len(want) != len(got) {
431431
t.Error("different []*Goroutine length")
432432
return
433433
}
434-
for i := range a {
434+
for i := range want {
435435
// &(*Goroutine).Signature
436-
zapSignatures(t, &a[i].Signature, &b[i].Signature)
436+
zapSignatures(t, &want[i].Signature, &got[i].Signature)
437437
}
438438
}
439439

@@ -448,55 +448,58 @@ func similarSignatures(t *testing.T, want, got *Signature) {
448448
}
449449
}
450450

451-
func zapSignatures(t *testing.T, a, b *Signature) {
451+
func zapSignatures(t *testing.T, want, got *Signature) {
452452
// Signature.Stack.([]Call)
453-
if len(a.Stack.Calls) != len(b.Stack.Calls) {
453+
if len(want.Stack.Calls) != len(got.Stack.Calls) {
454454
t.Error("different call length")
455455
return
456456
}
457-
if len(a.CreatedBy.Calls) != 0 && len(b.CreatedBy.Calls) != 0 {
458-
if a.CreatedBy.Calls[0].Line != 0 && b.CreatedBy.Calls[0].Line != 0 {
459-
a.CreatedBy.Calls[0].Line = 42
460-
b.CreatedBy.Calls[0].Line = 42
457+
if len(want.CreatedBy.Calls) != 0 && len(got.CreatedBy.Calls) != 0 {
458+
if want.CreatedBy.Calls[0].Line != 0 && got.CreatedBy.Calls[0].Line != 0 {
459+
want.CreatedBy.Calls[0].Line = 42
460+
got.CreatedBy.Calls[0].Line = 42
461461
}
462462
}
463-
zapStacks(t, &a.Stack, &b.Stack)
463+
zapStacks(t, &want.Stack, &got.Stack)
464464
}
465465

466-
func zapStacks(t *testing.T, a, b *Stack) {
467-
if len(a.Calls) != len(b.Calls) {
466+
func zapStacks(t *testing.T, want, got *Stack) {
467+
if len(want.Calls) != len(got.Calls) {
468468
t.Error("different Stack.[]Call length")
469469
return
470470
}
471-
for i := range a.Calls {
472-
zapCalls(t, &a.Calls[i], &b.Calls[i])
471+
for i := range want.Calls {
472+
zapCalls(t, &want.Calls[i], &got.Calls[i])
473473
}
474474
}
475475

476-
func zapCalls(t *testing.T, a, b *Call) {
477-
if a.Line != 0 && b.Line != 0 {
478-
a.Line = 42
479-
b.Line = 42
476+
func zapCalls(t *testing.T, want, got *Call) {
477+
if want.Line != 0 && got.Line != 0 {
478+
want.Line = 42
479+
got.Line = 42
480480
}
481-
zapArgs(t, &a.Args, &b.Args)
481+
zapArgs(t, &want.Args, &got.Args)
482482
}
483483

484-
func zapArgs(t *testing.T, a, b *Args) {
485-
if len(a.Values) != len(b.Values) {
484+
func zapArgs(t *testing.T, want, got *Args) {
485+
if len(want.Values) != len(got.Values) {
486486
t.Error("different Args.Values length")
487487
return
488488
}
489-
for i := range a.Values {
490-
if a.Values[i].Value != 0 && b.Values[i].Value != 0 {
491-
a.Values[i].Value = 42
492-
b.Values[i].Value = 42
489+
for i := range want.Values {
490+
if want.Values[i].Value != 0 && got.Values[i].Value != 0 {
491+
want.Values[i].Value = 42
492+
got.Values[i].Value = 42
493493
}
494-
if a.Values[i].IsAggregate && b.Values[i].IsAggregate {
495-
zapArgs(t, &a.Values[i].Fields, &b.Values[i].Fields)
494+
if want.Values[i].IsAggregate && got.Values[i].IsAggregate {
495+
zapArgs(t, &want.Values[i].Fields, &got.Values[i].Fields)
496496
}
497-
if a.Values[i].Name != "" && b.Values[i].Name != "" {
498-
a.Values[i].Name = "foo"
499-
b.Values[i].Name = "foo"
497+
if want.Values[i].Name != "" && got.Values[i].Name != "" {
498+
want.Values[i].Name = "foo"
499+
got.Values[i].Name = "foo"
500+
}
501+
if want.Values[i].IsInaccurate && !got.Values[i].IsInaccurate {
502+
want.Values[i].IsInaccurate = false
500503
}
501504
}
502505
}

0 commit comments

Comments
 (0)