-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtoken_bucket_test.go
More file actions
42 lines (38 loc) · 879 Bytes
/
token_bucket_test.go
File metadata and controls
42 lines (38 loc) · 879 Bytes
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
package ratelimit
import (
"testing"
"time"
)
func TestBucket_AllowWithStatus(t *testing.T) {
pool := newRedisPool("localhost:6379")
store := NewRedigoStore(pool)
rate := 5
key := "test"
n := time.Now()
timeNow = func() time.Time {
return n
}
b := &Bucket{
identifier: "marketing_campaigns",
rate: rate,
windowSize: 1000,
store: &store,
}
// nextRefresh := time.Duration(b.windowSize) * time.Millisecond
for i := 1; i <= rate; i++ {
got, err := b.AllowWithStatus(key)
if err != nil {
t.Error("AllowWithStatus call failed = ", err)
}
if !got.Allowed {
t.Errorf("limit breached")
}
//if got.NextRefresh != nextRefresh {
// t.Errorf("invalid next refresh duration, got = %d, want = %d\n", got.NextRefresh, nextRefresh)
//}
}
got, _ := b.AllowWithStatus(key)
if got.Allowed {
t.Error("expected to hit the limit")
}
}