forked from alileza/panics
-
Notifications
You must be signed in to change notification settings - Fork 11
/
panics_test.go
159 lines (150 loc) · 3.39 KB
/
panics_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package panics
import (
"bufio"
"os"
"testing"
"time"
)
func TestMain(m *testing.M) {
SetOptions(&Options{
Env: "development",
Filepath: "example",
})
os.Exit(m.Run())
}
// if the post to slack fails for some reason, it must log it
func TestPostToSlack(t *testing.T) {
slackWebhookURL = "http://127.0.0.2"
postToSlack("hello", "world")
}
func TestCaptureWithStackTrace(t *testing.T) {
tests := []struct {
name string
errStr string
messages []string
want []string // assertion per lines of output, starting from first line
}{
{
name: "no message",
errStr: "error",
want: []string{"[development] *error* `````` ```"},
},
{
name: "with message",
errStr: "error",
messages: []string{"message 1"},
want: []string{"[development] *error* ```message 1``` ```"},
},
{
name: "multiple messages",
errStr: "multiple errors",
messages: []string{"message 1", "message 2"},
want: []string{"[development] *multiple errors* ```message 1", "", "message 2``` ```"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CaptureWithStackTrace(tt.errStr, tt.messages...)
var (
file *os.File
err error
)
for {
file, err = os.Open("example/panics.log")
if err != nil {
if os.IsNotExist(err) {
time.Sleep(3 * time.Second)
continue
}
t.Fatalf("failed to read file: %s", err.Error())
}
break
}
scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
content := scanner.Text()
if content != tt.want[i] {
t.Errorf("unexpected file output got: %s, want: %s", content, tt.want[i])
}
i++
if i >= len(tt.want) {
break
}
}
err = os.Remove("example/panics.log")
if err != nil {
t.Fatalf("failed to delete file: %s", err.Error())
}
})
}
}
func TestCaptureGoroutine(t *testing.T) {
type args struct {
handleFn func()
recoveryFn func()
}
type want struct {
handleFnCalledTimes int
recoveryFnCalledTimes int
}
handleFnCalledTimes := 0
recoveryFnCalledTimes := 0
done := make(chan bool)
tests := []struct {
name string
args args
want want
}{
{
name: "handleFn executed and not panic, shouldn't trigger recoveryFn",
args: args{
handleFn: func() {
handleFnCalledTimes++
done <- true
},
recoveryFn: func() {
recoveryFnCalledTimes++
done <- true
},
},
want: want{
handleFnCalledTimes: 1,
recoveryFnCalledTimes: 0,
},
},
{
name: "handleFn executed and panic, should trigger recoveryFn",
args: args{
handleFn: func() {
handleFnCalledTimes++
panic("panic here")
},
recoveryFn: func() {
recoveryFnCalledTimes++
done <- true
},
},
want: want{
handleFnCalledTimes: 1,
recoveryFnCalledTimes: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handleFnCalledTimes = 0
recoveryFnCalledTimes = 0
go CaptureGoroutine(tt.args.handleFn, tt.args.recoveryFn)
select {
case <-done:
}
if handleFnCalledTimes != tt.want.handleFnCalledTimes {
t.Errorf("HandleFn was called %v times, expected %v times", handleFnCalledTimes, tt.want.handleFnCalledTimes)
}
if recoveryFnCalledTimes != tt.want.recoveryFnCalledTimes {
t.Errorf("RecoveryFn was called %v times, expected %v times", recoveryFnCalledTimes, tt.want.recoveryFnCalledTimes)
}
})
}
}