-
Notifications
You must be signed in to change notification settings - Fork 1
/
aerror_test.go
286 lines (224 loc) · 5.59 KB
/
aerror_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package aerrors
import (
"errors"
"sync"
"testing"
"time"
)
const sleepTime = 1 * time.Millisecond
type testHandler struct {
errs []error
mu sync.Mutex
}
func (th *testHandler) HandleError(err error) {
th.mu.Lock()
defer th.mu.Unlock()
th.errs = append(th.errs, err)
}
func (th *testHandler) Reset() {
th.mu.Lock()
defer th.mu.Unlock()
th.errs = []error{}
}
var th = testHandler{
errs: []error{},
}
type testErrorType struct{}
func (e *testErrorType) Error() string {
return "[Test Error Type]"
}
var testErr = testErrorType{}
func TestErrorHandler(t *testing.T) {
aerror := New(WithHandler(&th))
aerror.StartHandle()
defer aerror.Stop()
defer th.Reset()
aerror.Add(errors.New("testing handler"))
if len(aerror.errorChan) != 1 {
t.Error("expected chan len of 1")
}
time.Sleep(sleepTime)
if len(th.errs) != 1 {
t.Error("expected to have an error")
}
if len(aerror.errorChan) != 0 {
t.Error("chan is not empty")
}
}
func TestErrorHandlerMultipleAdd(t *testing.T) {
aerror := New(WithHandler(&th))
aerror.StartHandle()
defer aerror.Stop()
defer th.Reset()
aerror.Add(errors.New("testing handler 1"))
aerror.Add(errors.New("testing handler 2"))
aerror.Add(errors.New("testing handler 3"))
if len(aerror.errorChan) != 3 {
t.Error("expected chan len of 3", len(aerror.errorChan))
}
time.Sleep(sleepTime)
if len(th.errs) != 3 {
t.Error("expected to have 3 errors")
}
if len(aerror.errorChan) != 0 {
t.Error("chan is not empty")
}
}
func TestWithBaseError(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr))
aerror.StartHandle()
defer aerror.Stop()
defer th.Reset()
aerror.Add(errors.New("testing handler with base error"))
if len(aerror.errorChan) != 1 {
t.Error("expected chan len of 1")
}
time.Sleep(sleepTime)
if len(th.errs) != 1 {
t.Error("expected to have an errors")
}
if len(aerror.errorChan) != 0 {
t.Error("chan is not empty")
}
if !errors.Is(th.errs[0], &testErr) {
t.Error("expected test error type")
}
if errors.Unwrap(errors.Unwrap(th.errs[0])).Error() != testErr.Error() {
t.Error("expected test error next to the current in chain")
}
}
func TestPanicInGoroutine(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr))
aerror.StartHandle()
defer aerror.Stop()
defer th.Reset()
if len(th.errs) != 0 {
t.Error("expected to have 0 errors")
}
go func() {
defer aerror.PanicToError()
panic("test panic")
}()
time.Sleep(sleepTime)
if len(th.errs) != 1 {
t.Error("expected to have an error")
}
}
func TestPanicInGoroutineWrapper(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr))
aerror.StartHandle()
defer aerror.Stop()
defer th.Reset()
if len(th.errs) != 0 {
t.Error("expected to have 0 errors")
}
f := func() {
panic("test panic in Go wrapper")
}
aerror.Go(f)
time.Sleep(sleepTime)
if len(th.errs) != 1 {
t.Error("expected to have an error")
}
}
func TestOverflowErrorChan(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr), WithErrorChanLen(2))
defer aerror.Stop()
defer th.Reset()
aerror.Add(errors.New("testing handler 1"))
aerror.Add(errors.New("testing handler 2"))
aerror.AddAsync(errors.New("testing handler 3"))
aerror.AddAsync(errors.New("testing handler 4"))
aerror.AddAsync(errors.New("testing handler 5"))
if len(aerror.errorChan) != 2 {
t.Error("expected to have 2 errors in chan")
}
aerror.StartHandle()
time.Sleep(sleepTime)
if len(th.errs) != 5 {
t.Error("expected to have 2 errors in chan")
}
}
func TestCloseStartedAerror(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr), WithErrorChanLen(2))
defer th.Reset()
if len(th.errs) > 0 {
t.Error("result errors should be empty")
}
err := aerror.Add(errors.New("testing handler 1"))
if err != nil {
t.Error(err)
}
err = aerror.Add(errors.New("testing handler 2"))
if err != nil {
t.Error(err)
}
err = aerror.AddAsync(errors.New("testing handler 3"))
if err != nil {
t.Error(err)
}
err = aerror.AddAsync(errors.New("testing handler 4"))
if err != nil {
t.Error(err)
}
err = aerror.StartHandle()
if err != nil {
t.Error(err)
}
if len(aerror.errorChan) != 2 {
t.Error("expected to have 2 errors in chan")
}
err = aerror.StartHandle()
if err != nil {
t.Error(err)
}
if !aerror.running {
t.Error("aerror should be started")
}
if aerror.IsClosed() {
t.Error("aerror should not be closed")
}
time.Sleep(sleepTime)
aerror.Close()
if len(th.errs) != 4 {
t.Error("expected to have 4 errors in chan after waiting in close")
}
if aerror.running {
t.Error("aerror should be stopped")
}
if !aerror.IsClosed() {
t.Error("aerror should be closed")
}
}
func TestWorkWithClosedAerror(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr), WithErrorChanLen(2))
defer th.Reset()
aerror.Close()
err := aerror.Add(errors.New("testing handler 1"))
if err == nil {
t.Error("expected an error while adding an error to closed aerror")
}
err = aerror.AddAsync(errors.New("testing handler 2"))
if err == nil {
t.Error("expected an error while async adding an error to closed aerror")
}
if len(th.errs) > 0 {
t.Error("expected to have no handled errors")
}
err = aerror.StartHandle()
if err == nil {
t.Error("expected an error while starting handle an error to closed aerror")
}
if aerror.running {
t.Error("aerror should be stopped")
}
if !aerror.IsClosed() {
t.Error("aerror should not be closed")
}
}
func TestCloseClosedAerror(t *testing.T) {
aerror := New(WithHandler(&th), WithBaseError(&testErr), WithErrorChanLen(2))
defer th.Reset()
aerror.Close()
aerror.Close()
}