forked from bugsnag/bugsnag-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_test.go
123 lines (109 loc) · 3.88 KB
/
notifier_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package bugsnag_test
import (
"fmt"
"testing"
simplejson "github.com/bitly/go-simplejson"
"github.com/bugsnag/bugsnag-go"
. "github.com/bugsnag/bugsnag-go/testutil"
)
var bugsnaggedReports chan []byte
func notifierSetup(url string) *bugsnag.Notifier {
return bugsnag.New(bugsnag.Configuration{
APIKey: TestAPIKey,
Endpoints: bugsnag.Endpoints{Notify: url, Sessions: url + "/sessions"},
})
}
func crash(s interface{}) int {
return s.(int)
}
func TestStackframesAreSkippedCorrectly(t *testing.T) {
ts, reports := Setup()
bugsnaggedReports = reports
defer ts.Close()
notifier := notifierSetup(ts.URL)
bugsnag.Configure(bugsnag.Configuration{
APIKey: TestAPIKey,
Endpoints: bugsnag.Endpoints{Notify: ts.URL, Sessions: ts.URL + "/sessions"},
})
// Expect the following frames to be present for *.Notify
/*
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func1" },
{ "file": "testing/testing.go", "method": "tRunner" },
{ "file": "runtime/asm_amd64.s", "method": "goexit" }
*/
t.Run("notifier.Notify", func(st *testing.T) {
notifier.Notify(fmt.Errorf("oopsie"))
assertStackframeCount(st, 3)
})
t.Run("bugsnag.Notify", func(st *testing.T) {
bugsnag.Notify(fmt.Errorf("oopsie"))
assertStackframeCount(st, 3)
})
// Expect the following frames to be present for notifier.NotifySync
/*
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func2" },
{ "file": "testing/testing.go", "method": "tRunner" },
{ "file": "runtime/asm_amd64.s", "method": "goexit" }
*/
t.Run("notifier.NotifySync", func(st *testing.T) {
notifier.NotifySync(fmt.Errorf("oopsie"), true)
assertStackframeCount(st, 3)
})
// Expect the following frames to be present for *.AutoNotify
/*
{ "file": "runtime/panic.go", "method": "gopanic" },
{ "file": "runtime/iface.go", "method": "panicdottypeE" },
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func2.1" },
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func3" },
{ "file": "testing/testing.go", "method": "tRunner" },
{ "file": "runtime/asm_amd64.s", "method": "goexit" }
*/
t.Run("notifier.AutoNotify", func(st *testing.T) {
func() {
defer func() { recover() }()
defer notifier.AutoNotify()
crash("NaN")
}()
assertStackframeCount(st, 6)
})
t.Run("bugsnag.AutoNotify", func(st *testing.T) {
func() {
defer func() { recover() }()
defer bugsnag.AutoNotify()
crash("NaN")
}()
assertStackframeCount(st, 6)
})
// Expect the following frames to be present for *.Recover
/*
{ "file": "runtime/panic.go", "method": "gopanic" },
{ "file": "runtime/iface.go", "method": "panicdottypeE" },
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func4.1" },
{ "file": "$GOPATH/src/github.com/bugsnag/bugsnag-go/notifier_test.go", "method": "TestStackframesAreSkippedCorrectly.func4" },
{ "file": "testing/testing.go", "method": "tRunner" },
{ "file": "runtime/asm_amd64.s", "method": "goexit" }
*/
t.Run("notifier.Recover", func(st *testing.T) {
func() {
defer notifier.Recover()
crash("NaN")
}()
assertStackframeCount(st, 6)
})
t.Run("bugsnag.Recover", func(st *testing.T) {
func() {
defer bugsnag.Recover()
crash("NaN")
}()
assertStackframeCount(st, 6)
})
}
func assertStackframeCount(t *testing.T, expCount int) {
report, _ := simplejson.NewJson(<-bugsnaggedReports)
stacktrace := GetIndex(GetIndex(report, "events", 0), "exceptions", 0).Get("stacktrace")
if s := stacktrace.MustArray(); len(s) != expCount {
t.Errorf("Expected %d stackframe(s), but there were %d stackframes", expCount, len(s))
s, _ := stacktrace.EncodePretty()
t.Errorf(string(s))
}
}