-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps_limiter_test.go
65 lines (53 loc) · 1.38 KB
/
rps_limiter_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
package rate
import (
"fmt"
"math/rand/v2"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func TestRPSRateLimiterExecutionDuration(t *testing.T) {
const (
roundsPerSecond = 16
nbTasks = 128
)
logger := zaptest.NewLogger(t)
task := func() error {
time.Sleep(1 * time.Second)
if rand.Int32()%3 == 0 { // Error out one third of the responses.
return fmt.Errorf("something went wrong")
}
return nil
}
start := time.Now()
limiter := NewRPSLimiter(roundsPerSecond).
MakeBuffered(nbTasks).
WithLogger(logger.Sugar())
limiter.Start()
errorChans := make([]<-chan error, 0, nbTasks)
for range nbTasks {
errorChan, enqueued := limiter.TryEnqueue(task)
require.NotNil(t, errorChan)
require.True(t, enqueued)
errorChans = append(errorChans, errorChan)
}
t.Logf("enqueued all %d tasks", nbTasks)
require.NoError(t, limiter.Close())
var errCount, nilCount int
for _, errorCh := range errorChans {
err := <-errorCh
// Each chan must be closed after the err value is read.
_, ok := <-errorCh
require.False(t, ok)
if err == nil {
nilCount++
} else {
errCount++
}
}
elapsed := time.Since(start)
t.Logf("rate limited %d tasks in %s at %d rps", nbTasks, elapsed, roundsPerSecond)
require.Equal(t, nbTasks, errCount+nilCount)
require.GreaterOrEqual(t, elapsed, time.Duration(nbTasks/roundsPerSecond))
}