@@ -2,82 +2,62 @@ package ratelimit_test
2
2
3
3
import (
4
4
"context"
5
- "math"
6
5
"strings"
7
6
"testing"
8
7
"time"
9
8
10
9
jujuratelimit "github.com/juju/ratelimit"
10
+ "golang.org/x/time/rate"
11
11
12
12
"github.com/go-kit/kit/endpoint"
13
13
"github.com/go-kit/kit/ratelimit"
14
- "golang.org/x/time/rate"
15
14
)
16
15
16
+ var nopEndpoint = func (context.Context , interface {}) (interface {}, error ) { return struct {}{}, nil }
17
+
17
18
func TestTokenBucketLimiter (t * testing.T ) {
18
- e := func (context. Context , interface {}) ( interface {}, error ) { return struct {}{}, nil }
19
- for _ , n := range [] int { 1 , 2 , 100 } {
20
- tb := jujuratelimit . NewBucketWithRate ( float64 ( n ), int64 ( n ))
21
- testLimiter ( t , ratelimit .NewTokenBucketLimiter (tb )(e ), n )
22
- }
19
+ tb := jujuratelimit . NewBucket ( time . Minute , 1 )
20
+ testSuccessThenFailure (
21
+ t ,
22
+ ratelimit .NewTokenBucketLimiter (tb )(nopEndpoint ),
23
+ ratelimit . ErrLimited . Error ())
23
24
}
24
25
25
26
func TestTokenBucketThrottler (t * testing.T ) {
26
- d := time .Duration (0 )
27
- s := func (d0 time.Duration ) { d = d0 }
28
-
29
- e := func (context.Context , interface {}) (interface {}, error ) { return struct {}{}, nil }
30
- e = ratelimit .NewTokenBucketThrottler (jujuratelimit .NewBucketWithRate (1 , 1 ), s )(e )
31
-
32
- // First request should go through with no delay.
33
- e (context .Background (), struct {}{})
34
- if want , have := time .Duration (0 ), d ; want != have {
35
- t .Errorf ("want %s, have %s" , want , have )
36
- }
37
-
38
- // Next request should request a ~1s sleep.
39
- e (context .Background (), struct {}{})
40
- if want , have , tol := time .Second , d , time .Millisecond ; math .Abs (float64 (want - have )) > float64 (tol ) {
41
- t .Errorf ("want %s, have %s" , want , have )
42
- }
43
- }
44
-
45
- func testLimiter (t * testing.T , e endpoint.Endpoint , rate int ) {
46
- // First <rate> requests should succeed.
47
- for i := 0 ; i < rate ; i ++ {
48
- if _ , err := e (context .Background (), struct {}{}); err != nil {
49
- t .Fatalf ("rate=%d: request %d/%d failed: %v" , rate , i + 1 , rate , err )
50
- }
51
- }
52
-
53
- // Next request should fail.
54
- if _ , err := e (context .Background (), struct {}{}); err != ratelimit .ErrLimited {
55
- t .Errorf ("rate=%d: want %v, have %v" , rate , ratelimit .ErrLimited , err )
56
- }
27
+ tb := jujuratelimit .NewBucket (time .Minute , 1 )
28
+ testSuccessThenFailure (
29
+ t ,
30
+ ratelimit .NewTokenBucketThrottler (tb , nil )(nopEndpoint ),
31
+ "context deadline exceeded" )
57
32
}
58
33
59
34
func TestXRateErroring (t * testing.T ) {
60
35
limit := rate .NewLimiter (rate .Every (time .Minute ), 1 )
61
- e := func (context.Context , interface {}) (interface {}, error ) { return struct {}{}, nil }
62
- testLimiter (t , ratelimit .NewErroringLimiter (limit )(e ), 1 )
36
+ testSuccessThenFailure (
37
+ t ,
38
+ ratelimit .NewErroringLimiter (limit )(nopEndpoint ),
39
+ ratelimit .ErrLimited .Error ())
63
40
}
64
41
65
42
func TestXRateDelaying (t * testing.T ) {
66
43
limit := rate .NewLimiter (rate .Every (time .Minute ), 1 )
67
- e := func (context.Context , interface {}) (interface {}, error ) { return struct {}{}, nil }
68
- e = ratelimit .NewDelayingLimiter (limit )(e )
44
+ testSuccessThenFailure (
45
+ t ,
46
+ ratelimit .NewDelayingLimiter (limit )(nopEndpoint ),
47
+ "exceed context deadline" )
48
+ }
69
49
70
- _ , err := e (context .Background (), struct {}{})
71
- if err != nil {
50
+ func testSuccessThenFailure (t * testing.T , e endpoint.Endpoint , failContains string ) {
51
+ ctx , cxl := context .WithTimeout (context .Background (), 500 * time .Millisecond )
52
+ defer cxl ()
53
+
54
+ // First request should succeed.
55
+ if _ , err := e (ctx , struct {}{}); err != nil {
72
56
t .Errorf ("unexpected: %v\n " , err )
73
57
}
74
58
75
- dur := 500 * time .Millisecond
76
- ctx , cxl := context .WithTimeout (context .Background (), dur )
77
- defer cxl ()
78
-
79
- _ , err = e (ctx , struct {}{})
80
- if ! strings .Contains (err .Error (), "exceed context deadline" ) {
81
- t .Errorf ("expected timeout: %v\n " , err )
59
+ // Next request should fail.
60
+ if _ , err := e (ctx , struct {}{}); ! strings .Contains (err .Error (), failContains ) {
61
+ t .Errorf ("expected `%s`: %v\n " , failContains , err )
82
62
}
83
63
}
0 commit comments