-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathretry_internal_test.go
94 lines (77 loc) · 1.62 KB
/
retry_internal_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
package proxy
import (
"testing"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
)
func TestWithRetry(t *testing.T) {
t.Parallel()
const (
errA errors.Error = "error about a"
errB errors.Error = "error about b"
)
var (
good = func() (err error) {
return nil
}
badOne = func() (err error) {
return errA
}
// Don't protect against concurrent access since the closure is expected
// to be used in a single case.
returnedA = false
badBoth = func() (err error) {
if !returnedA {
returnedA = true
return errA
}
return errB
}
// Don't protect against concurrent access since the closure is expected
// to be used in a single case.
returnedErr = false
badThenOk = func() (err error) {
if !returnedErr {
returnedErr = true
return assert.AnError
}
return nil
}
)
testCases := []struct {
f func() (err error)
wantErr error
name string
}{{
f: good,
wantErr: nil,
name: "no_error",
}, {
f: badOne,
wantErr: errA,
name: "one_error",
}, {
f: badBoth,
wantErr: errA,
name: "two_errors",
}, {
f: badThenOk,
wantErr: nil,
name: "error_then_ok",
}}
p := &Proxy{
logger: slogutil.NewDiscardLogger(),
bindRetryNum: 1,
bindRetryIvl: 0,
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.ContextWithTimeout(t, testTimeout)
err := p.bindWithRetry(ctx, tc.f)
assert.ErrorIs(t, err, tc.wantErr)
})
}
}