@@ -45,24 +45,60 @@ func TestRateLimiter_AllowN(t *testing.T) {
45
45
now := time .Now ()
46
46
47
47
// Tenant #1
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 ))
48
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 8 ). OK () )
49
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 10 ). OK () )
50
+ assert .Equal (t , false , limiter .AllowN (now , "tenant-1" , 3 ). OK () )
51
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 2 ). OK () )
52
52
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 ))
53
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 8 ). OK () )
54
+ assert .Equal (t , false , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 3 ). OK () )
55
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-1" , 2 ). OK () )
56
56
57
57
// Tenant #2
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 ))
58
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 18 ).OK ())
59
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 20 ).OK ())
60
+ assert .Equal (t , false , limiter .AllowN (now , "tenant-2" , 3 ).OK ())
61
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-2" , 2 ).OK ())
62
+
63
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 18 ).OK ())
64
+ assert .Equal (t , false , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 3 ).OK ())
65
+ assert .Equal (t , true , limiter .AllowN (now .Add (time .Second ), "tenant-2" , 2 ).OK ())
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 , limiter .AllowN (now , "tenant-1" , 12 ).OK ())
80
+ assert .Equal (t , false , limiter .AllowN (now , "tenant-1" , 9 ).OK ())
81
+
82
+ r1 := limiter .AllowN (now , "tenant-1" , 8 )
83
+ assert .Equal (t , true , r1 .OK ())
84
+ r1 .CancelAt (now )
85
+
86
+ assert .Equal (t , true , limiter .AllowN (now , "tenant-1" , 8 ).OK ())
87
+
88
+ // +10 tokens (1s)
89
+ nowPlus := now .Add (time .Second )
90
+
91
+ assert .Equal (t , true , limiter .AllowN (nowPlus , "tenant-1" , 6 ).OK ())
92
+ assert .Equal (t , false , limiter .AllowN (nowPlus , "tenant-1" , 5 ).OK ())
93
+
94
+ r2 := limiter .AllowN (nowPlus , "tenant-1" , 4 )
95
+ assert .Equal (t , true , r2 .OK ())
96
+ r2 .CancelAt (nowPlus )
97
+
98
+ assert .Equal (t , true , limiter .AllowN (nowPlus , "tenant-1" , 2 ).OK ())
99
+ assert .Equal (t , false , limiter .AllowN (nowPlus , "tenant-1" , 3 ).OK ())
100
+ assert .Equal (t , true , limiter .AllowN (nowPlus , "tenant-1" , 2 ).OK ())
101
+ assert .Equal (t , false , limiter .AllowN (nowPlus , "tenant-1" , 1 ).OK ())
66
102
}
67
103
68
104
func BenchmarkRateLimiter_CustomMultiTenant (b * testing.B ) {
0 commit comments