-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbudget_test.go
191 lines (165 loc) · 3.81 KB
/
budget_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
package retry
import (
"context"
"errors"
"log"
"math"
"math/rand"
"os"
"sort"
"sync"
"testing"
"time"
)
func ExampleBudget() {
ctx := context.Background()
// fooRetryBudget is a global variable holding the state of foo's retry budget.
// You should have one retry budget per backend service.
var fooRetryBudget = Budget{
Rate: 1.0,
Ratio: 0.1,
}
// failingRPC is a fake RPC call simulating a temporary backend failure.
failingRPC := func(_ context.Context) error {
return errors.New("temporary failure")
}
// Simulate 100 concurrent requests. Each request is tried initially,
// but only ~10 requests are retried, i.e. there will be approximately
// 110 calls of failingRPC in total.
for i := 0; i < 100; i++ {
go func() {
// Pass a pointer to fooRetryBudget to all Do() calls,
// i.e. all Do() calls receive a pointer to the same Budget{}.
// This allows state to be shared between Do() calls.
if err := Do(ctx, failingRPC, &fooRetryBudget); err != nil {
log.Println(err)
}
}()
}
}
func TestBudget(t *testing.T) {
if os.Getenv("RETRY_ENDTOEND") == "" {
t.Skip("set the \"RETRY_ENDTOEND\" environment variable to enable this test")
}
var (
callsCount int
callsLock sync.Mutex
updateTime time.Time
rates []float64
)
rpcCall := func(ctx context.Context) error {
callsLock.Lock()
defer callsLock.Unlock()
callsCount++
now := time.Now()
if updateTime.IsZero() {
updateTime = now
} else if s := now.Sub(updateTime).Seconds(); s >= 0.1 {
rates = append(rates, float64(callsCount)/s)
callsCount = 0
updateTime = now
}
if rand.Float64() < 0.5 {
return errors.New("temporary error")
}
return nil
}
ctx := context.Background()
ticker := time.NewTicker(time.Second / 100)
wg := &sync.WaitGroup{}
budget := &Budget{
Rate: 0,
Ratio: 0.1,
}
for i := 0; i < 200; i++ {
<-ticker.C
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
if err := Do(ctx, rpcCall, budget); err != nil {
// log.Printf("retry.Do() = %v", err)
}
}(ctx)
}
wg.Wait()
sort.Float64s(rates)
if got, want := rates[len(rates)/2], 110.0; math.Abs(got-want) > 1.0 {
t.Errorf("got median rate %g, want %g", got, want)
}
}
func TestMovingRate(t *testing.T) {
cases := []struct {
calls []int
wantCount float64
wantSecond float64
}{
{
calls: []int{5},
wantCount: 5,
wantSecond: 0.2,
},
{
calls: []int{5, 3},
wantCount: 8,
wantSecond: 1.2,
},
{
calls: []int{5, 5, 1},
wantCount: 11,
wantSecond: 2.2,
},
{
calls: []int{5, 5, 5, 5, 5, 5, 5, 5, 5, 1},
wantCount: 9*5 + 1,
wantSecond: 9.2,
},
{
calls: []int{
5, // partial value
5, 5, 5, 5, 5, 5, 5, 5, 5, 1},
wantCount: 5*.8 + 9*5 + 1,
wantSecond: 10,
},
{
calls: []int{
1000000, // partial value
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
},
wantCount: 1000000*.8 + 20,
wantSecond: 10.0,
},
{
calls: []int{
2, 2, 2, 2, // old
5, // partial
1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
},
wantCount: 5*.8 + 5,
wantSecond: 10.0,
},
}
for _, c := range cases {
mr := &movingRate{
BucketLength: time.Second,
BucketNum: 10,
}
tm := time.Date(2018, time.February, 22, 22, 24, 53, 200000000, time.UTC)
for _, n := range c.calls {
tm = tm.Add(mr.BucketLength)
for j := 0; j < n; j++ {
mr.Add(tm, 1)
}
}
t.Logf("BEFORE mr = %+v", mr)
if got, want := mr.count(), c.wantCount; got != want {
t.Errorf("mr.count() = %g, want %g", got, want)
}
if got, want := mr.second(), c.wantSecond; got != want {
t.Errorf("mr.second() = %g, want %g", got, want)
}
if got, want := mr.Rate(tm), c.wantCount/c.wantSecond; got != want {
t.Errorf("mr.Rate(%v) = %g, want %g (= %g/%g)", tm, got, want, c.wantCount, c.wantSecond)
}
t.Logf("AFTER mr = %+v", mr)
}
}