-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgock_test.go
463 lines (403 loc) · 9.24 KB
/
gock_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package gock_test
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/tcard/gock"
)
func ExampleWait_singleError() {
var ErrOops = errors.New("oops")
err := gock.Wait(func() error {
return nil
}, func() error {
return ErrOops
})
fmt.Println(err == ErrOops)
// Output:
// true
}
func ExampleWait_concurrentErrors() {
var ErrOops = errors.New("oops")
var ErrFailed = errors.New("failed")
err := gock.Wait(func() error {
return ErrFailed
}, func() error {
return ErrOops
})
fmt.Println(gock.AnyIs(err, ErrOops))
fmt.Println(gock.AnyIs(err, ErrFailed))
// Output:
// true
// true
}
func ExampleWait_sameErrorTwice() {
var ErrOops = errors.New("oops")
err := gock.Wait(func() error {
return ErrOops
}, func() error {
return nil
}, func() error {
return ErrOops
})
fmt.Println(err == ErrOops)
// Output:
// true
}
func ExampleWait_commonErrorAncestor() {
var ErrCommonAncestor = errors.New("ye eldest")
err := gock.Wait(func() error {
return fmt.Errorf(
"first in first chain: %w",
fmt.Errorf(
"second in first chain: %w",
ErrCommonAncestor,
),
)
}, func() error {
return nil
}, func() error {
return fmt.Errorf(
"first in second chain: %w",
ErrCommonAncestor,
)
})
fmt.Println(errors.Is(err, ErrCommonAncestor))
// Output:
// true
}
func TestGoRunsBeforeWait(t *testing.T) {
g, wait := gock.Bundle()
defer wait()
done := make(chan struct{})
g(func() error { close(done); return nil })
<-done
}
func TestGoAfterWait(t *testing.T) {
g, wait := gock.Bundle()
wait()
func() {
defer func() {
recover()
}()
g(func() error { return nil })
t.Error("expected panic")
}()
}
func TestGoWhileWaiting(t *testing.T) {
doneGo := make(chan struct{}, 2)
g, wait := gock.Bundle()
g(func() error {
time.Sleep(100 * time.Millisecond) // Give time for wait() to start.
g(func() error {
doneGo <- struct{}{}
return nil
})
doneGo <- struct{}{}
return nil
})
err := wait()
for i := 0; i < cap(doneGo); i++ {
select {
case <-doneGo:
default:
t.Errorf("goroutines should've finished by now")
}
}
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
}
func TestIdempotentWait(t *testing.T) {
expected := errors.New("expect me")
g, wait := gock.Bundle()
timesRun := 0
g(func() error {
timesRun++
return expected
})
for i := 0; i < 2; i++ {
err := wait()
if expected != err {
t.Errorf("expected to get the same error twice, got %s on #%d", err, i)
}
}
if timesRun != 1 {
t.Errorf("expected function to only be run once, got run %d", timesRun)
}
}
func TestBundleNothing(t *testing.T) {
_, wait := gock.Bundle()
err := wait()
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
}
func TestWaitForNothing(t *testing.T) {
err := gock.Wait()
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
}
func TestConcurrentErrorsString(t *testing.T) {
err := gock.AddConcurrentError(errors.New("foo"), errors.New("bar"))
if expected, got := "concurrent errors: foo; bar", err.Error(); expected != got {
t.Errorf("expected: %q got: %q", expected, got)
}
}
func TestConcurrentErrorsFlatten(t *testing.T) {
errs := []error{errors.New("foo"), errors.New("bar"), errors.New("baz")}
cerrs := gock.AddConcurrentError(
errs[0],
gock.AddConcurrentError(
gock.AddConcurrentError(
nil,
errs[1],
),
errs[2],
),
).(gock.ConcurrentErrors)
if expected, got := 3, len(cerrs.Errors); expected != got {
t.Errorf("expected %d flattened ConcurrentErrors, got %d", expected, got)
}
for _, err := range cerrs.Errors {
if _, ok := err.(gock.ConcurrentErrors); ok {
t.Errorf("this ConcurrentErrors wasn't flattened: %s", err)
}
}
}
type chain struct {
err1, err2 error
}
func (c chain) Error() string { return fmt.Sprintf("%v: %v", c.err1, c.err2) }
func (c chain) Unwrap() error { return c.err2 }
func TestAnyIs(t *testing.T) {
expected := errors.New("expect me")
err := gock.Wait(func() error {
return errors.New("I'm not")
}, func() error {
return chain{errors.New("wrapping: "), expected}
})
ok := gock.AnyIs(err, expected)
if !ok {
t.Error("should find the expected error")
}
}
func TestAnyIsNot(t *testing.T) {
notFound := errors.New("won't find me")
err := gock.Wait(func() error {
return errors.New("I'm not")
}, func() error {
return chain{errors.New("wrapping: "), errors.New("me neither")}
})
ok := gock.AnyIs(err, notFound)
if ok {
t.Error("shouldn't find the error")
}
}
func TestAnyIsSingle(t *testing.T) {
expected := errors.New("expect me")
ok := gock.AnyIs(expected, expected)
if !ok {
t.Error("should find the expected error")
}
}
type myError string
func (err myError) Error() string { return "am an error: " + string(err) }
func TestAnyAs(t *testing.T) {
expected := myError("expect me")
err := gock.Wait(func() error {
return errors.New("I'm not")
}, func() error {
return chain{errors.New("wrapping: "), expected}
})
var got myError
ok := gock.AnyAs(err, &got)
if !ok {
t.Error("should find the myError")
}
if expected != got {
t.Errorf("expected: %v, got: %v", expected, got)
}
}
func TestAnyAsNot(t *testing.T) {
err := gock.Wait(func() error {
return errors.New("I'm not")
}, func() error {
return chain{errors.New("wrapping: "), errors.New("me neither")}
})
var got myError
ok := gock.AnyAs(err, &got)
if ok {
t.Error("shouldn't find a myError")
}
}
func TestAnyAsSingle(t *testing.T) {
expected := myError("expect me")
var got myError
ok := gock.AnyAs(expected, &got)
if !ok {
t.Error("should find the myError")
}
if expected != got {
t.Errorf("expected: %v, got: %v", expected, got)
}
}
func TestConcurrentErrorsUnwrapNoCommonAncestor(t *testing.T) {
ancestor := errors.New("ancestor")
err := gock.AddConcurrentError(
chain{errors.New("foo"), ancestor},
chain{errors.New("baz"), errors.New("another ancestor")},
)
ok := errors.Is(err, ancestor)
if ok {
t.Errorf("didn't expect to find the non-common ancestor")
}
}
func TestWaitRunsCallHereBeforeWait(t *testing.T) {
calledHere := make(chan struct{})
gock.Wait(func() error {
close(calledHere)
return nil
}, func() error {
<-calledHere
return nil
})
}
func TestAddConcurrentUncomparableErrors(t *testing.T) {
// https://github.com/tcard/gock/issues/1
var allErrors []error
for i := 0; i < 4; i++ {
allErrors = append(allErrors, fmt.Errorf("error %d", i))
}
err := gock.AddConcurrentError(
gock.AddConcurrentError(
allErrors[0],
allErrors[1],
),
gock.AddConcurrentError(
allErrors[2],
allErrors[3],
),
)
if expected, got := allErrors, err.(gock.ConcurrentErrors).Errors; !reflect.DeepEqual(expected, got) {
t.Errorf("expected %#v, got %#v", expected, got)
}
}
func TestPanic(t *testing.T) {
expectedErr := errors.New("expected")
for _, c := range []struct {
name string
do func()
}{{
"first goroutine on Wait",
func() {
gock.Wait(func() error {
panic(expectedErr)
}, func() error {
select {}
})
},
}, {
"non-first goroutine on Wait",
func() {
gock.Wait(func() error {
select {}
}, func() error {
panic(expectedErr)
})
},
}, {
"on Bundle",
func() {
g, wait := gock.Bundle()
g(func() error {
panic(expectedErr)
})
g(func() error {
select {}
})
wait()
},
}} {
t.Run(c.name, func(t *testing.T) {
defer func() {
r := recover()
err, ok := r.(error)
if !ok || !errors.Is(err, expectedErr) {
t.Errorf("expected repanic of expectedErr in the blocked goroutine, got: %v", r)
}
}()
c.do()
})
}
}
func TestNoErr(t *testing.T) {
g, wait := gock.Bundle()
called := false
g.NoErr(func() {
called = true
})
err := wait()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !called {
t.Errorf("expected concurrent function to be called")
}
}
func TestUnwrapDeepConcurrentErrors(t *testing.T) {
err := gock.AddConcurrentError(
fmt.Errorf("wrapping to avoid flattening: %w", gock.AddConcurrentError(
errors.New("A"),
errors.New("B"),
)),
errors.New("C"),
)
if got := err.(gock.ConcurrentErrors).Unwrap(); got != nil {
t.Errorf("didn't expect unwrapped error, got: %v", got)
}
}
func TestUnwrapUnhashableCommonAncestor(t *testing.T) {
unhashableErr := unhashableError{errors.New("hi"), nil}
err := gock.AddConcurrentError(
fmt.Errorf("wrapping: %w", unhashableErr),
unhashableErr,
)
if got := err.(gock.ConcurrentErrors).Unwrap(); got != nil {
t.Errorf("didn't expect unwrapped error, got: %v", got)
}
}
func TestIsUnhashableCommonAncestor(t *testing.T) {
unhashableErr := unhashableError{errors.New("hi"), nil}
err := gock.AddConcurrentError(
fmt.Errorf("wrapping: %w", unhashableErr),
unhashableErr,
)
if !errors.Is(err, unhashableErr) {
t.Errorf("expected unhashable common ancestor to be found")
}
}
func TestUnhashableButComparableErrorNoPanic(t *testing.T) {
unhashableErr := comparableError{unhashableError{errors.New("hi"), nil}}
err := gock.AddConcurrentError(
fmt.Errorf("wrapping: %w", unhashableErr),
unhashableErr,
)
errors.Unwrap(err)
}
type comparableError struct {
error
}
type unhashableError struct {
error
s []int
}
func (uerr unhashableError) Is(err error) bool {
switch err := err.(type) {
case unhashableError:
return errors.Is(err.error, uerr.error) && reflect.DeepEqual(err.s, uerr.s)
}
return false
}