-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoother_test.go
382 lines (317 loc) · 9.52 KB
/
smoother_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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//nolint:revive // ok
package smoother
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type getTestTryer func(rps int) Tryer
func setupThrottledTryer(t *testing.T) getTestTryer {
t.Helper()
mr := miniredis.RunT(t)
client := redis.NewClient(&redis.Options{Addr: mr.Addr()})
require.NoError(t, client.Ping(context.Background()).Err())
tryerGetter := func(rps int) Tryer {
tryer, err := NewRedisThrottledTryer(
client, "test", rps)
require.NoError(t, err)
return tryer
}
return tryerGetter
}
func setupRedisRateTryer(t *testing.T) getTestTryer {
t.Helper()
mr := miniredis.RunT(t)
client := redis.NewClient(&redis.Options{Addr: mr.Addr()})
require.NoError(t, client.Ping(context.Background()).Err())
tryerGetter := func(rps int) Tryer {
tryer, err := NewRedisRateTryer(client, "test", rps)
require.NoError(t, err)
return tryer
}
return tryerGetter
}
func setupTestLocalTryer(t *testing.T) getTestTryer {
t.Helper()
tryerGetter := func(rps int) Tryer {
tryer, err := NewLocalTryer(rps)
require.NoError(t, err)
return tryer
}
return tryerGetter
}
func TestRateSmoother_Take(t *testing.T) {
t.Run("local", func(t *testing.T) {
testRateSmoother_Take_helper(t, setupTestLocalTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_Take_helper(t, setupThrottledTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_Take_helper(t, setupRedisRateTryer(t))
})
}
func testRateSmoother_Take_helper(t *testing.T, tryerGetter getTestTryer) {
ctx := context.Background()
name := fmt.Sprintf(" (%s)", t.Name())
tests := []struct {
name string
rps int
targerRPS int
count int
duration time.Duration
calls int
}{
{
name: "basic rate limiting RPS" + name,
rps: 100,
targerRPS: 100,
count: 1,
duration: time.Second * 2,
calls: 150 * 2, // Slightly more calls than possible in the duration
},
{
name: "high rate RPS" + name,
rps: 1000,
targerRPS: 1000,
count: 1,
duration: time.Second * 5,
calls: 1500 * 5,
},
{
name: "multiple tokens at once" + name,
rps: 1000,
targerRPS: 1000,
count: 2,
duration: time.Second * 2,
calls: 1500 * 2,
},
{
name: "RPS lower than target" + name,
rps: 1000,
targerRPS: 500,
count: 2,
duration: time.Second * 2,
calls: 500 * 2,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tryer := tryerGetter(tt.rps)
smoother, err := NewRateSmoother(tryer, WithTimeout(tt.duration))
require.NoError(t, err)
smoother.Start()
defer smoother.Stop()
start := time.Now()
successfulCalls := 0
// Send requests faster than the rate limit
for range tt.calls / tt.count {
_, err := smoother.Take(ctx, tt.count)
require.NoError(t, err)
if time.Since(start) >= tt.duration {
break
}
successfulCalls++
}
actualRPS := float64(successfulCalls) / tt.duration.Seconds()
expectedRPS := float64(tt.targerRPS) / float64(tt.count)
// Allow for small timing variations ±152%
marginOfError := 0.15
assert.InDelta(t, expectedRPS, actualRPS, expectedRPS*marginOfError,
"Expected rps: %.2f, Actual rps: %.2f (margin: ±%.0f%%)", expectedRPS, actualRPS, marginOfError*100)
})
}
}
func TestRateSmoother_ContextCancellation(t *testing.T) {
t.Run("local", func(t *testing.T) {
testRateSmoother_ContextCancellation_Helper(t, setupTestLocalTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_ContextCancellation_Helper(t, setupThrottledTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_ContextCancellation_Helper(t, setupRedisRateTryer(t))
})
}
func testRateSmoother_ContextCancellation_Helper(t *testing.T, tryerGetter getTestTryer) {
tryer := tryerGetter(1)
smoother, err := NewRateSmoother(tryer) // 1 RPS for easy timing
require.NoError(t, err)
smoother.Start()
defer smoother.Stop()
ctx, cancel := context.WithCancel(context.Background())
// First take should succeed immediately
_, err = smoother.Take(ctx, 1)
require.NoError(t, err, "name: %s", t.Name())
// Start a goroutine that will cancel the context shortly
go func() {
time.Sleep(time.Millisecond)
cancel()
}()
// This take should be blocked and then cancelled
start := time.Now()
for time.Since(start) < time.Second {
_, err = smoother.Take(ctx, 1)
if err != nil {
break
}
}
assert.ErrorIs(t, err, context.Canceled, "name: %s", t.Name())
}
func TestRateSmoother_Concurrency(t *testing.T) {
t.Run("local", func(t *testing.T) {
testRateSmoother_Concurrency_Helper(t, setupTestLocalTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_Concurrency_Helper(t, setupThrottledTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_Concurrency_Helper(t, setupRedisRateTryer(t))
})
}
func testRateSmoother_Concurrency_Helper(t *testing.T, tryerGetter getTestTryer) {
const (
multi = 5
rps = 1000
interval = time.Second * multi
goroutines = 100
)
var (
ctx = context.Background()
rateTryer = tryerGetter(rps)
start = time.Now()
wg sync.WaitGroup
actualCalls int64
minDelay, maxDelay time.Duration
muDelay sync.Mutex
)
smoother, err := NewRateSmoother(rateTryer)
require.NoError(t, err)
smoother.Start()
defer smoother.Stop()
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
for {
delay, err := smoother.Take(ctx, 1)
require.NoError(t, err)
muDelay.Lock()
actualCalls++
if delay > 0 {
if minDelay == 0 || delay < minDelay {
minDelay = delay
}
if delay > maxDelay {
maxDelay = delay
}
}
muDelay.Unlock()
if time.Since(start) >= interval {
break
}
}
}()
}
wg.Wait()
actualRate := float64(actualCalls) / interval.Seconds()
expectedRate := float64(rps)
// Allow for small timing variations ±10%
marginOfError := 0.1
assert.InDelta(t, expectedRate, actualRate, expectedRate*marginOfError,
"MinDelay: %s, MaxDelay: %s, Expected rate: %.2f, Actual rate: %.2f (margin: ±%.0f%%). Name: %s",
minDelay, maxDelay, expectedRate, actualRate, marginOfError*100, t.Name())
}
func TestRateSmoother_ShutdownWithPendingRequests(t *testing.T) {
t.Run("local", func(t *testing.T) {
testRateSmoother_ShutdownWithPendingRequests_Helper(t, setupTestLocalTryer(t))
})
t.Run("redis_throttled", func(t *testing.T) {
testRateSmoother_ShutdownWithPendingRequests_Helper(t, setupThrottledTryer(t))
})
t.Run("redis_rate", func(t *testing.T) {
testRateSmoother_ShutdownWithPendingRequests_Helper(t, setupRedisRateTryer(t))
})
}
func testRateSmoother_ShutdownWithPendingRequests_Helper(t *testing.T, tryerGetter getTestTryer) {
// Create a smoother with a very low rate to ensure requests will queue up
tryer := tryerGetter(1) // 1 RPS to ensure requests will be queued
smoother, err := NewRateSmoother(tryer, WithQueueSize(10))
require.NoError(t, err)
smoother.Start()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Take a token to ensure the first request doesn't stay in the queue
_, err = smoother.Take(ctx, 1)
require.NoError(t, err)
var wg sync.WaitGroup
var results []error
var resultsMu sync.Mutex
var requestsStarted sync.WaitGroup
// Launch 5 concurrent requests that will be queued
for i := 0; i < 5; i++ {
wg.Add(1)
requestsStarted.Add(1)
go func() {
defer wg.Done()
requestsStarted.Done() // Сигнализируем, что горутина запущена
// This call will be queued and then interrupted by shutdown
_, err := smoother.Take(ctx, 1)
resultsMu.Lock()
results = append(results, err)
resultsMu.Unlock()
}()
}
// Ждем, пока все горутины точно запустятся
requestsStarted.Wait()
// Даем дополнительное время для гарантированного попадания в очередь
time.Sleep(100 * time.Millisecond)
smoother.Stop()
wg.Wait()
shutdownErrors := 0
for _, err := range results {
if err != nil && err.Error() == "rate smoother shutting down" {
shutdownErrors++
}
}
assert.Greater(t, shutdownErrors, 0, "Expected at least one 'rate smoother shutting down' error")
}
func TestRateSmoother_HandleRequest_TryerError(t *testing.T) {
// Create a custom error to check for wrapping
customErr := errors.New("test tryer error")
// Create a mock tryer that returns an error
mockTryer := &mockTryer{
tryTakeFunc: func(ctx context.Context, count int) (bool, time.Duration, error) {
return false, 0, customErr
},
}
smoother, err := NewRateSmoother(mockTryer)
require.NoError(t, err)
smoother.Start()
defer smoother.Stop()
// Make the Take call - this should propagate our custom error
waitDuration, takeErr := smoother.Take(context.Background(), 1)
// Verify the error was properly wrapped
assert.Error(t, takeErr)
assert.ErrorContains(t, takeErr, "tryer:")
assert.ErrorIs(t, takeErr, customErr)
assert.Greater(t, waitDuration, time.Duration(0))
}
// Mock implementation for testing
type mockTryer struct {
tryTakeFunc func(ctx context.Context, count int) (bool, time.Duration, error)
}
func (m *mockTryer) TryTake(ctx context.Context, count int) (bool, time.Duration, error) {
if m.tryTakeFunc != nil {
return m.tryTakeFunc(ctx, count)
}
return true, 0, nil
}