-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
quota_pool_test.go
313 lines (268 loc) · 7.5 KB
/
quota_pool_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
// Copyright 2017 The Cockroach Authors.
//
// 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.
//
// Author: Irfan Sharif ([email protected])
package storage
import (
"errors"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"golang.org/x/net/context"
)
// TestQuotaPoolBasic tests the minimal expected behaviour of the quota pool
// with different sized quota pool and a varying number of threads, each
// acquiring a unit quota and releasing it immediately after.
func TestQuotaPoolBasic(t *testing.T) {
defer leaktest.AfterTest(t)()
quotas := []int64{1, 10, 100, 1000}
threadCounts := []int{1, 10, 100}
for _, quota := range quotas {
for _, threadCount := range threadCounts {
qp := newQuotaPool(quota)
ctx := context.Background()
errCh := make(chan error, threadCount)
for i := 0; i < threadCount; i++ {
go func() {
if err := qp.acquire(ctx, 1); err != nil {
errCh <- err
return
}
errCh <- nil
qp.add(1)
}()
}
for i := 0; i < threadCount; i++ {
select {
case <-time.After(5 * time.Second):
t.Fatal("did not complete acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
}
if q := qp.approximateQuota(); q != quota {
t.Fatalf("expected quota: %d, got: %d", quota, q)
}
}
}
}
// TestQuotaPoolContextCancellation tests the behaviour that for an ongoing
// blocked acquisition, if the context passed in gets cancelled the acquisition
// gets cancelled too with an error indicating so. This should not affect the
// available quota in the pool.
func TestQuotaPoolContextCancellation(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx, cancel := context.WithCancel(context.Background())
qp := newQuotaPool(1)
if err := qp.acquire(ctx, 1); err != nil {
t.Fatal(err)
}
errCh := make(chan error, 1)
go func() {
if err := qp.acquire(ctx, 1); !testutils.IsError(err, "context canceled") {
errCh <- errors.New("expected acquisition to fail with ctx cancellation")
return
}
close(errCh)
}()
cancel()
select {
case <-time.After(5 * time.Second):
t.Fatal("context cancellation did not unblock acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
qp.add(1)
if q := qp.approximateQuota(); q != 1 {
t.Fatalf("expected quota: 1, got: %d", q)
}
}
// TestQuotaPoolClose tests the behaviour that for an ongoing
// blocked acquisition, if the quota pool gets closed the acquisition
// gets cancelled too with an error indicating so.
func TestQuotaPoolClose(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
qp := newQuotaPool(1)
if err := qp.acquire(ctx, 1); err != nil {
t.Fatal(err)
}
errCh := make(chan error, 1)
go func() {
if err := qp.acquire(ctx, 1); !testutils.IsError(err, "quota pool no longer in use") {
errCh <- errors.New("expected acquisition to fail with pool closing")
return
}
close(errCh)
}()
qp.close()
select {
case <-time.After(5 * time.Second):
t.Fatal("quota pool closing did not unblock acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
}
// TestQuotaPoolCancelledAcquisitions tests the behaviour where we enqueue
// multiple acquisitions with cancelled contexts and expect any subsequent
// acquisition with a valid context to proceed without error.
func TestQuotaPoolCancelledAcquisitions(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx, cancel := context.WithCancel(context.Background())
qp := newQuotaPool(1)
if err := qp.acquire(ctx, 1); err != nil {
t.Fatal(err)
}
cancel()
const threadCount = 5
errCh := make(chan error, threadCount)
for i := 0; i < threadCount; i++ {
go func() {
if err := qp.acquire(ctx, 1); !testutils.IsError(err, "context canceled") {
errCh <- errors.New("expected acquisition to fail with ctx cancellation")
return
}
errCh <- nil
}()
}
for i := 0; i < threadCount; i++ {
select {
case <-time.After(5 * time.Second):
t.Fatal("context cancellations did not unblock acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
}
qp.add(1)
go func() {
errCh <- qp.acquire(context.Background(), 1)
}()
select {
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("acquisition didn't go through within 5s")
}
}
// TestQuotaPoolNoops tests that quota pool operations that should be noops are
// so, e.g. quotaPool.acquire(0) and quotaPool.release(0).
func TestQuotaPoolNoops(t *testing.T) {
defer leaktest.AfterTest(t)()
qp := newQuotaPool(1)
ctx := context.Background()
if err := qp.acquire(ctx, 1); err != nil {
t.Fatal(err)
}
errCh := make(chan error, 1)
go func() {
if err := qp.acquire(ctx, 1); err != nil {
errCh <- err
return
}
close(errCh)
}()
qp.add(0)
qp.add(1)
select {
case <-time.After(5 * time.Second):
t.Fatal("context cancellations did not unblock acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
qp.add(0)
qp.add(1)
if err := qp.acquire(ctx, 1); err != nil {
t.Fatal(err)
}
if err := qp.acquire(ctx, 0); err != nil {
t.Fatal(err)
}
}
// TestQuotaPoolMaxQuota tests that at no point does the total quota available
// in the pool exceed the maximum amount the pool was initialized with.
func TestQuotaPoolMaxQuota(t *testing.T) {
defer leaktest.AfterTest(t)()
const quota = 100
const threadCount = 200
qp := newQuotaPool(quota)
ctx := context.Background()
errCh := make(chan error, threadCount)
for i := 0; i < threadCount; i++ {
go func() {
if err := qp.acquire(ctx, 1); err != nil {
errCh <- err
return
}
errCh <- nil
qp.add(2)
}()
}
for i := 0; i < threadCount; i++ {
select {
case <-time.After(5 * time.Second):
t.Fatal("did not complete acquisitions within 5s")
case err := <-errCh:
if err != nil {
t.Fatal(err)
}
}
}
if q := qp.approximateQuota(); q != quota {
t.Fatalf("expected quota: %d, got: %d", quota, q)
}
}
// TestQuotaPoolCappedAcquisition verifies that when an acquisition request
// greater than the maximum quota is placed, we still allow the acquisition to
// proceed but after having acquired the maximum quota amount.
func TestQuotaPoolCappedAcquisition(t *testing.T) {
defer leaktest.AfterTest(t)()
const quota = 1
qp := newQuotaPool(quota)
if err := qp.acquire(context.Background(), quota*100); err != nil {
t.Fatal(err)
}
if q := qp.approximateQuota(); q != 0 {
t.Fatalf("expected quota: %d, got: %d", 0, q)
}
qp.add(quota)
if q := qp.approximateQuota(); q != quota {
t.Fatalf("expected quota: %d, got: %d", quota, q)
}
}
// BenchmarkQuotaPool benchmarks the common case where we have sufficient
// quota available in the pool and we repeatedly acquire and release quota.
func BenchmarkQuotaPool(b *testing.B) {
qp := newQuotaPool(1)
ctx := context.Background()
for n := 0; n < b.N; n++ {
if err := qp.acquire(ctx, 1); err != nil {
b.Fatal(err)
}
qp.add(1)
}
qp.close()
}