-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_test.go
141 lines (127 loc) · 3.73 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package bugsnag
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/kinbiko/jsonassert"
)
func TestApp(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
cfg *Configuration
exp string
}{
{name: "no optional config", cfg: &Configuration{}, exp: `{ "duration": 5000 }`},
{
name: "all optional config",
cfg: &Configuration{AppVersion: "1.5.2", ReleaseStage: "staging"},
exp: `{ "duration": 5000, "releaseStage": "staging", "version": "1.5.2" }`,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc.cfg.appStartTime = time.Now().Add(-5 * time.Second)
payload, err := json.Marshal(makeJSONApp(tc.cfg))
if err != nil {
t.Fatal(err)
}
jsonassert.New(t).Assertf(string(payload), tc.exp)
})
}
}
func TestDevice(t *testing.T) {
t.Parallel()
n, err := New(Configuration{APIKey: "abcd1234abcd1234abcd1234abcd1234", ReleaseStage: "dev", AppVersion: "1.2.3"})
if err != nil {
t.Fatal(err)
}
n.cfg.runtimeConstants = runtimeConstants{
hostname: "myHost",
osVersion: "4.1.12",
goVersion: "1.15",
osName: "linux innit",
}
payload, err := json.Marshal(n.makeJSONDevice())
if err != nil {
t.Fatal(err)
}
jsonassert.New(t).Assertf(string(payload), `{
"runtimeMetrics": "<<PRESENCE>>",
"hostname": "myHost",
"osName": "linux innit",
"osVersion": "4.1.12",
"goroutineCount": "<<PRESENCE>>",
"runtimeVersions": {"go": "1.15"}
}`)
}
func TestMakeExceptions(t *testing.T) {
t.Parallel()
n, err := New(Configuration{APIKey: "abcd1234abcd1234abcd1234abcd1234", ReleaseStage: "dev", AppVersion: "1.2.3"})
if err != nil {
t.Fatal(err)
}
ctx := n.WithBugsnagContext(context.Background(), "/api/user/1523")
ctx = n.WithMetadatum(ctx, "tab", "one", "1")
err = errors.New("1st error (errors.New)")
err = fmt.Errorf("2nd error (github.com/pkg/errors.Wrap): %w", err)
err = n.Wrap(n.WithMetadatum(ctx, "tab", "two", "2"), err, "3rd error (%s.(*Notifier).Wrap)", "bugsnag")
err = fmt.Errorf("4th error (fmt.Errorf('percent-w')): %w", err)
eps := makeExceptions(err)
payload, _ := json.Marshal(eps)
jsonassert.New(t).Assertf(string(payload), `[
{
"errorClass": "*fmt.wrapError",
"message": "4th error (fmt.Errorf('percent-w')): 3rd error (bugsnag.(*Notifier).Wrap): 2nd error (github.com/pkg/errors.Wrap): 1st error (errors.New)",
"stacktrace": null
}, {
"errorClass": "*bugsnag.Error",
"message": "3rd error (bugsnag.(*Notifier).Wrap): 2nd error (github.com/pkg/errors.Wrap): 1st error (errors.New)",
"stacktrace": [
{"file":"<<PRESENCE>>","inProject":false,"lineNumber":"<<PRESENCE>>","method":"testing.tRunner"},
{"file":"<<PRESENCE>>","inProject":false,"lineNumber":"<<PRESENCE>>","method":"<<PRESENCE>>"}
]
}, {
"errorClass": "*fmt.wrapError",
"message": "2nd error (github.com/pkg/errors.Wrap): 1st error (errors.New)",
"stacktrace": null
}, {
"errorClass": "*errors.errorString",
"message": "1st error (errors.New)",
"stacktrace": null
}
]`)
}
func TestInternalErrorCallback(t *testing.T) {
t.Parallel()
t.Run("gets invoked when set", func(t *testing.T) {
t.Parallel()
var got error
n, err := New(Configuration{
APIKey: "abcd1234abcd1234abcd1234abcd1234",
ReleaseStage: "dev",
AppVersion: "1.2.3",
InternalErrorCallback: func(err error) {
got = err
},
})
if err != nil {
t.Fatal(err)
}
n.Notify(nil, nil)
if got == nil {
t.Error("expected an error in the error callback but got none")
}
})
t.Run("doesn't panic when not set", func(t *testing.T) {
t.Parallel()
n, err := New(Configuration{APIKey: "abcd1234abcd1234abcd1234abcd1234", ReleaseStage: "dev", AppVersion: "1.2.3"})
if err != nil {
t.Fatal(err)
}
n.Notify(nil, nil)
})
}