-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_test.go
162 lines (147 loc) · 4 KB
/
backoff_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
// Copyright (c) 2019 Hervé Gouchet. All rights reserved.
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
package backoff_test
import (
"context"
"errors"
"testing"
"time"
"github.com/matryer/is"
"github.com/rvflash/backoff"
)
func newTask(until int) *task {
return &task{start: time.Now(), until: until}
}
type task struct {
// called contains the number of iteration done
called,
// until contains the number of iteration to do in success
until int
// start is the start time
start time.Time
// uptime return the time since the last call
latest time.Duration
}
var errRetry = errors.New("oops")
func (t *task) stopwatch() {
t.latest = time.Since(t.start)
}
// KoUntil implements the backoff.Func interface.
func (t *task) KoUntil(context.Context) error {
defer t.stopwatch()
t.called++
if t.called < t.until {
return errRetry
}
return nil
}
// OkUntil implements the backoff.Func interface.
func (t *task) OkUntil(context.Context) error {
defer t.stopwatch()
t.called++
if t.called > t.until {
return errRetry
}
return nil
}
func TestDo(t *testing.T) {
var (
are = is.New(t)
dt = map[string]struct {
f backoff.Func
n int
err error
}{
"ko: no ctx: 1 times": {f: newTask(0).OkUntil, n: 0, err: errRetry},
"ko: no ctx: 2 times": {f: newTask(1).OkUntil, n: 1, err: errRetry},
"ko: ctx cancelled: 2 times": {f: newTask(4).OkUntil, n: 3, err: backoff.ErrDeadlineExceeded},
}
)
for name, tt := range dt {
tt := tt
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
n, err := backoff.Do(ctx, tt.f)
cancel()
are.Equal(n, tt.n) // mismatch attempt
are.Equal(err, tt.err) // mismatch error
})
}
}
func TestDoN(t *testing.T) {
const n = 3
var (
are = is.New(t)
job = newTask(5)
)
i, err := backoff.DoN(context.Background(), n, job.OkUntil)
are.Equal(err, backoff.ErrRetry) // mismatch error
are.Equal(i, n) // mismatch attempt
are.Equal(i, job.called) // mismatch call
}
func TestDoUntil(t *testing.T) {
const d = time.Second
var (
are = is.New(t)
dur = time.Now().Add(d)
job = newTask(5)
)
i, err := backoff.DoUntil(context.Background(), dur, job.OkUntil)
are.Equal(err, backoff.ErrDeadlineExceeded) // mismatch error
are.Equal(i, 2) // mismatch attempt
are.True(job.latest < d) // mismatch duration
}
func TestRetry(t *testing.T) {
var (
are = is.New(t)
dt = map[string]struct {
f backoff.Func
n int
err error
}{
"ok: no retry": {f: newTask(1).KoUntil},
"ok: 1 times": {f: newTask(2).KoUntil, n: 1},
"ko: ctx cancelled: 2 times": {f: newTask(5).KoUntil, n: 3, err: backoff.ErrDeadlineExceeded},
}
)
for name, tt := range dt {
tt := tt
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
n, err := backoff.Retry(ctx, tt.f)
cancel()
are.Equal(n, tt.n) // mismatch attempt
are.Equal(err, tt.err) // mismatch error
})
}
}
func TestRetryN(t *testing.T) {
const n = 3
var (
are = is.New(t)
job = newTask(5)
)
var backoffError *backoff.Error
i, err := backoff.RetryN(context.Background(), n, job.KoUntil)
are.True(errors.As(err, &backoffError)) // mismatch error
are.True(!backoffError.DeadlineExceeded()) // mismatch type
are.Equal(i, n) // mismatch attempt
are.Equal(i, job.called) // mismatch call
}
func TestRetryUntil(t *testing.T) {
const (
n = 2
d = time.Second
)
var (
are = is.New(t)
job = newTask(5)
)
var backoffError *backoff.Error
i, err := backoff.RetryUntil(context.Background(), time.Now().Add(d), job.KoUntil)
are.True(errors.As(err, &backoffError)) // mismatch error
are.True(backoffError.DeadlineExceeded()) // mismatch type
are.Equal(i, n) // mismatch attempt
are.True(job.latest < d) // mismatch duration
}