@@ -45,60 +45,24 @@ func TestRateLimiter_AllowN(t *testing.T) {
45
45
now := time .Now ()
46
46
47
47
// Tenant #1
48
- assert .Equal (t , true , isOK ( limiter .AllowN (now , "tenant-1" , 8 ) ))
49
- assert .Equal (t , true , isOK ( limiter .AllowN (now , "tenant-1" , 10 ) ))
50
- assert .Equal (t , false , isOK ( limiter .AllowN (now , "tenant-1" , 3 ) ))
51
- assert .Equal (t , true , isOK ( limiter .AllowN (now , "tenant-1" , 2 ) ))
48
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 8 ))
49
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 10 ))
50
+ assert .Equal (t , false , limiter .AllowN (now , "tenant-1" , 3 ))
51
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 2 ))
52
52
53
- assert .Equal (t , true , isOK ( limiter .AllowN (now .Add (time .Second ), "tenant-1" , 8 ) ))
54
- assert .Equal (t , false , isOK ( limiter .AllowN (now .Add (time .Second ), "tenant-1" , 3 ) ))
55
- assert .Equal (t , true , isOK ( limiter .AllowN (now .Add (time .Second ), "tenant-1" , 2 ) ))
53
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 8 ))
54
+ assert .Equal (t , false , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 3 ))
55
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 2 ))
56
56
57
57
// Tenant #2
58
- assert .Equal (t , true , isOK (limiter .AllowN (now , "tenant-2" , 18 )))
59
- assert .Equal (t , true , isOK (limiter .AllowN (now , "tenant-2" , 20 )))
60
- assert .Equal (t , false , isOK (limiter .AllowN (now , "tenant-2" , 3 )))
61
- assert .Equal (t , true , isOK (limiter .AllowN (now , "tenant-2" , 2 )))
62
-
63
- assert .Equal (t , true , isOK (limiter .AllowN (now .Add (time .Second ), "tenant-2" , 18 )))
64
- assert .Equal (t , false , isOK (limiter .AllowN (now .Add (time .Second ), "tenant-2" , 3 )))
65
- assert .Equal (t , true , isOK (limiter .AllowN (now .Add (time .Second ), "tenant-2" , 2 )))
66
- }
67
-
68
- func TestRateLimiter_AllowNCancelation (t * testing.T ) {
69
- strategy := & staticLimitStrategy {tenants : map [string ]struct {
70
- limit float64
71
- burst int
72
- }{
73
- "tenant-1" : {limit : 10 , burst : 20 },
74
- }}
75
-
76
- limiter := NewRateLimiter (strategy , 10 * time .Second )
77
- now := time .Now ()
78
-
79
- assert .Equal (t , true , isOK (limiter .AllowN (now , "tenant-1" , 12 )))
80
- assert .Equal (t , false , isOK (limiter .AllowN (now , "tenant-1" , 9 )))
81
-
82
- ok1 , r1 := limiter .AllowN (now , "tenant-1" , 8 )
83
- assert .Equal (t , true , ok1 )
84
- r1 .CancelAt (now )
85
-
86
- assert .Equal (t , true , isOK (limiter .AllowN (now , "tenant-1" , 8 )))
87
-
88
- // +10 tokens (1s)
89
- nowPlus := now .Add (time .Second )
90
-
91
- assert .Equal (t , true , isOK (limiter .AllowN (nowPlus , "tenant-1" , 6 )))
92
- assert .Equal (t , false , isOK (limiter .AllowN (nowPlus , "tenant-1" , 5 )))
93
-
94
- ok2 , r2 := limiter .AllowN (nowPlus , "tenant-1" , 4 )
95
- assert .Equal (t , true , ok2 )
96
- r2 .CancelAt (nowPlus )
97
-
98
- assert .Equal (t , true , isOK (limiter .AllowN (nowPlus , "tenant-1" , 2 )))
99
- assert .Equal (t , false , isOK (limiter .AllowN (nowPlus , "tenant-1" , 3 )))
100
- assert .Equal (t , true , isOK (limiter .AllowN (nowPlus , "tenant-1" , 2 )))
101
- assert .Equal (t , false , isOK (limiter .AllowN (nowPlus , "tenant-1" , 1 )))
58
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 18 ))
59
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 20 ))
60
+ assert .Equal (t , false , limiter .AllowN (now , "tenant-2" , 3 ))
61
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 2 ))
62
+
63
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 18 ))
64
+ assert .Equal (t , false , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 3 ))
65
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 2 ))
102
66
}
103
67
104
68
func BenchmarkRateLimiter_CustomMultiTenant (b * testing.B ) {
@@ -163,7 +127,3 @@ func (s *staticLimitStrategy) Burst(tenantID string) int {
163
127
164
128
return tenant .burst
165
129
}
166
-
167
- func isOK (ok bool , r Reservation ) bool {
168
- return ok
169
- }
0 commit comments