/
flow_controller_test.go
256 lines (236 loc) · 6.43 KB
/
flow_controller_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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pubsub
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"golang.org/x/sync/errgroup"
)
func TestFlowControllerCancel(t *testing.T) {
// Test canceling a flow controller's context.
t.Parallel()
fc := newFlowController(3, 10)
if err := fc.acquire(context.Background(), 5); err != nil {
t.Fatal(err)
}
// Experiment: a context that times out should always return an error.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
if err := fc.acquire(ctx, 6); err != context.DeadlineExceeded {
t.Fatalf("got %v, expected DeadlineExceeded", err)
}
// Control: a context that is not done should always return nil.
go func() {
time.Sleep(5 * time.Millisecond)
fc.release(ctx, 5)
}()
if err := fc.acquire(context.Background(), 6); err != nil {
t.Errorf("got %v, expected nil", err)
}
}
func TestFlowControllerLargeRequest(t *testing.T) {
// Large requests succeed, consuming the entire allotment.
t.Parallel()
fc := newFlowController(3, 10)
err := fc.acquire(context.Background(), 11)
if err != nil {
t.Fatal(err)
}
}
func TestFlowControllerNoStarve(t *testing.T) {
// A large request won't starve, because the flowController is
// (best-effort) FIFO.
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
fc := newFlowController(10, 10)
first := make(chan int)
for i := 0; i < 20; i++ {
go func() {
for {
if err := fc.acquire(ctx, 1); err != nil {
if err != context.Canceled {
t.Error(err)
}
return
}
select {
case first <- 1:
default:
}
fc.release(ctx, 1)
}
}()
}
<-first // Wait until the flowController's state is non-zero.
if err := fc.acquire(ctx, 11); err != nil {
t.Errorf("got %v, want nil", err)
}
}
func TestFlowControllerSaturation(t *testing.T) {
t.Parallel()
const (
maxCount = 6
maxSize = 10
)
for _, test := range []struct {
acquireSize int
wantCount, wantSize int64
}{
{
// Many small acquires cause the flow controller to reach its max count.
acquireSize: 1,
wantCount: 6,
wantSize: 6,
},
{
// Five acquires of size 2 will cause the flow controller to reach its max size,
// but not its max count.
acquireSize: 2,
wantCount: 5,
wantSize: 10,
},
{
// If the requests are the right size (relatively prime to maxSize),
// the flow controller will not saturate on size. (In this case, not on count either.)
acquireSize: 3,
wantCount: 3,
wantSize: 9,
},
} {
fc := newFlowController(maxCount, maxSize)
// Atomically track flow controller state.
// The flowController itself tracks count.
var curSize int64
success := errors.New("")
// Time out if wantSize or wantCount is never reached.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < 10; i++ {
g.Go(func() error {
var hitCount, hitSize bool
// Run at least until we hit the expected values, and at least
// for enough iterations to exceed them if the flow controller
// is broken.
for i := 0; i < 100 || !hitCount || !hitSize; i++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := fc.acquire(ctx, test.acquireSize); err != nil {
return err
}
c := int64(fc.count())
if c > test.wantCount {
return fmt.Errorf("count %d exceeds want %d", c, test.wantCount)
}
if c == test.wantCount {
hitCount = true
}
s := atomic.AddInt64(&curSize, int64(test.acquireSize))
if s > test.wantSize {
return fmt.Errorf("size %d exceeds want %d", s, test.wantSize)
}
if s == test.wantSize {
hitSize = true
}
time.Sleep(5 * time.Millisecond) // Let other goroutines make progress.
if atomic.AddInt64(&curSize, -int64(test.acquireSize)) < 0 {
return errors.New("negative size")
}
fc.release(ctx, test.acquireSize)
}
return success
})
}
if err := g.Wait(); err != success {
t.Errorf("%+v: %v", test, err)
continue
}
}
}
func TestFlowControllerTryAcquire(t *testing.T) {
t.Parallel()
fc := newFlowController(3, 10)
ctx := context.Background()
// Successfully tryAcquire 4 bytes.
if !fc.tryAcquire(ctx, 4) {
t.Error("got false, wanted true")
}
// Fail to tryAcquire 7 bytes.
if fc.tryAcquire(ctx, 7) {
t.Error("got true, wanted false")
}
// Successfully tryAcquire 6 byte.
if !fc.tryAcquire(ctx, 6) {
t.Error("got false, wanted true")
}
}
func TestFlowControllerUnboundedCount(t *testing.T) {
t.Parallel()
ctx := context.Background()
fc := newFlowController(0, 10)
// Successfully acquire 4 bytes.
if err := fc.acquire(ctx, 4); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Successfully tryAcquire 4 bytes.
if !fc.tryAcquire(ctx, 4) {
t.Error("got false, wanted true")
}
// Fail to tryAcquire 3 bytes.
if fc.tryAcquire(ctx, 3) {
t.Error("got true, wanted false")
}
}
func TestFlowControllerUnboundedCount2(t *testing.T) {
t.Parallel()
ctx := context.Background()
fc := newFlowController(0, 0)
// Successfully acquire 4 bytes.
if err := fc.acquire(ctx, 4); err != nil {
t.Errorf("got %v, wanted no error", err)
}
fc.release(ctx, 1)
fc.release(ctx, 1)
fc.release(ctx, 1)
wantCount := int64(-2)
c := int64(fc.count())
if c != wantCount {
t.Fatalf("got count %d, want %d", c, wantCount)
}
}
func TestFlowControllerUnboundedBytes(t *testing.T) {
t.Parallel()
ctx := context.Background()
fc := newFlowController(2, 0)
// Successfully acquire 4GB.
if err := fc.acquire(ctx, 4e9); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Successfully tryAcquire 4GB bytes.
if !fc.tryAcquire(ctx, 4e9) {
t.Error("got false, wanted true")
}
// Fail to tryAcquire a third message.
if fc.tryAcquire(ctx, 3) {
t.Error("got true, wanted false")
}
}