-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier_panic_test.go
113 lines (96 loc) · 2.74 KB
/
notifier_panic_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
//go:build !race
// +build !race
package bugsnag
import (
"context"
"errors"
"strings"
"testing"
)
// This test intentionally checks for the absence of user-facing panics when
// attempting to write to a closed channel.
// Therefore, the test is only run when the race detector is not running.
func TestNoPanicsWhenShuttingDown(t *testing.T) {
t.Parallel()
cfg := Configuration{
APIKey: "abcd1234abcd1234abcd1234abcd1234",
ReleaseStage: "dev",
AppVersion: "1.2.3",
// Dummy endpoints to avoid accidentally sending payloads to Bugsnag.
// Note that the panics are most likely to occur *after* any execution
// of the Sanitizer functions, and thus setting different endpoints
// that will fail are more appropriate.
EndpointNotify: "http://0.0.0.0:1234",
EndpointSessions: "http://0.0.0.0:1234",
}
t.Run("doesn't panic if invoking StartSession or Notify", func(t *testing.T) {
t.Parallel()
n, err := New(cfg)
if err != nil {
t.Fatal(err)
}
ctx := n.StartSession(context.Background())
n.Notify(ctx, errors.New("oooi"))
n.Close()
})
t.Run("StartSession and Notify are uncalled", func(t *testing.T) {
t.Parallel()
n, err := New(cfg)
if err != nil {
t.Fatal(err)
}
n.Close()
})
t.Run("Notify after Close invokes InternalErrorCallback", func(t *testing.T) {
t.Parallel()
var got error
cfg.InternalErrorCallback = func(err error) { got = err }
n, err := New(cfg)
if err != nil {
t.Fatal(err)
}
n.Close()
n.Notify(context.Background(), errors.New("oops"))
if got == nil {
t.Fatal("expected error but got none")
}
if exp, got := "did you invoke Notify", got.Error(); !strings.Contains(got, exp) {
t.Errorf("expected error message containing %s but got %s", exp, got)
}
})
t.Run("StartSession after Close invokes InternalErrorCallback", func(t *testing.T) {
t.Parallel()
var got error
cfg.InternalErrorCallback = func(err error) { got = err }
n, err := New(cfg)
if err != nil {
t.Fatal(err)
}
n.Close()
n.StartSession(context.Background())
if got == nil {
t.Fatal("expected error but got none")
}
if exp, got := "did you invoke StartSession", got.Error(); !strings.Contains(got, exp) {
t.Errorf("expected error message containing %s but got %s", exp, got)
}
})
t.Run("Close after Close invokes InternalErrorCallback", func(t *testing.T) {
t.Parallel()
var got error
cfg := cfg
cfg.InternalErrorCallback = func(err error) { got = err }
n, err := New(cfg)
if err != nil {
t.Fatal(err)
}
n.Close()
n.Close()
if got == nil {
t.Fatal("expected error but got none")
}
if exp, got := "did you invoke Close", got.Error(); !strings.Contains(got, exp) {
t.Errorf("expected error message containing %s but got %s", exp, got)
}
})
}