@@ -17,18 +17,18 @@ func TestRateLimit_Basic(t *testing.T) {
1717
1818 // First 3 should be allowed
1919 for i := 0 ; i < 3 ; i ++ {
20- if ! l .RateLimit (action , actor , bucket , allowed ) {
20+ if ! l .IsAllowed (action , nil , actor , bucket , allowed ) {
2121 t .Errorf ("Request %d should have been allowed" , i + 1 )
2222 }
2323 }
2424
2525 // 4th should be rate limited
26- if l .RateLimit (action , actor , bucket , allowed ) {
26+ if l .IsAllowed (action , nil , actor , bucket , allowed ) {
2727 t .Error ("Request 4 should have been rate limited" )
2828 }
2929
3030 // 5th should also be rate limited
31- if l .RateLimit (action , actor , bucket , allowed ) {
31+ if l .IsAllowed (action , nil , actor , bucket , allowed ) {
3232 t .Error ("Request 5 should have been rate limited" )
3333 }
3434}
@@ -37,23 +37,24 @@ func TestRateLimit_DifferentActors(t *testing.T) {
3737 l := New ()
3838 defer l .Stop ()
3939
40+ namespace := []byte ("namespace1" )
4041 actor1 := []byte ("user1" )
4142 actor2 := []byte ("user2" )
4243 action := "test-action"
4344 bucket := time .Minute
4445 allowed := uint64 (2 )
4546
4647 // Actor 1 uses both requests
47- l .RateLimit (action , actor1 , bucket , allowed )
48- l .RateLimit (action , actor1 , bucket , allowed )
48+ l .IsAllowed (action , namespace , actor1 , bucket , allowed )
49+ l .IsAllowed (action , namespace , actor1 , bucket , allowed )
4950
5051 // Actor 1 should be limited
51- if l .RateLimit (action , actor1 , bucket , allowed ) {
52+ if l .IsAllowed (action , namespace , actor1 , bucket , allowed ) {
5253 t .Error ("Actor1 should be rate limited" )
5354 }
5455
5556 // Actor 2 should still be allowed
56- if ! l .RateLimit (action , actor2 , bucket , allowed ) {
57+ if ! l .IsAllowed (action , namespace , actor2 , bucket , allowed ) {
5758 t .Error ("Actor2 should be allowed" )
5859 }
5960}
@@ -63,21 +64,22 @@ func TestRateLimit_DifferentActions(t *testing.T) {
6364 defer l .Stop ()
6465
6566 actor := []byte ("user123" )
67+ namespace := []byte ("namespace" )
6668 action1 := "action1"
6769 action2 := "action2"
6870 bucket := time .Minute
6971 allowed := uint64 (1 )
7072
7173 // Use up action1 limit
72- l .RateLimit (action1 , actor , bucket , allowed )
74+ l .IsAllowed (action1 , namespace , actor , bucket , allowed )
7375
7476 // Action1 should be limited
75- if l .RateLimit (action1 , actor , bucket , allowed ) {
77+ if l .IsAllowed (action1 , namespace , actor , bucket , allowed ) {
7678 t .Error ("Action1 should be rate limited" )
7779 }
7880
7981 // Action2 should still be allowed
80- if ! l .RateLimit (action2 , actor , bucket , allowed ) {
82+ if ! l .IsAllowed (action2 , namespace , actor , bucket , allowed ) {
8183 t .Error ("Action2 should be allowed" )
8284 }
8385}
@@ -86,24 +88,25 @@ func TestRateLimit_BucketReset(t *testing.T) {
8688 l := New ()
8789 defer l .Stop ()
8890
91+ namespace := []byte ("namespace" )
8992 actor := []byte ("user123" )
9093 action := "test-action"
9194 bucket := 100 * time .Millisecond
9295 allowed := uint64 (2 )
9396
9497 // Use up the limit
95- l .RateLimit (action , actor , bucket , allowed )
96- l .RateLimit (action , actor , bucket , allowed )
98+ l .IsAllowed (action , namespace , actor , bucket , allowed )
99+ l .IsAllowed (action , namespace , actor , bucket , allowed )
97100
98- if l .RateLimit (action , actor , bucket , allowed ) {
101+ if l .IsAllowed (action , namespace , actor , bucket , allowed ) {
99102 t .Error ("Should be rate limited" )
100103 }
101104
102105 // Wait for next bucket
103106 time .Sleep (150 * time .Millisecond )
104107
105108 // Should be allowed again
106- if ! l .RateLimit (action , actor , bucket , allowed ) {
109+ if ! l .IsAllowed (action , namespace , actor , bucket , allowed ) {
107110 t .Error ("Should be allowed after bucket reset" )
108111 }
109112}
@@ -112,18 +115,19 @@ func TestCount(t *testing.T) {
112115 l := New ()
113116 defer l .Stop ()
114117
118+ namespace := []byte ("namespace" )
115119 actor := []byte ("user123" )
116120 action := "test-action"
117121 bucket := time .Minute
118122
119- if c := l .Count (action , actor , bucket ); c != 0 {
123+ if c := l .Count (action , namespace , actor , bucket ); c != 0 {
120124 t .Errorf ("Expected count 0, got %d" , c )
121125 }
122126
123- l .RateLimit (action , actor , bucket , 10 )
124- l .RateLimit (action , actor , bucket , 10 )
127+ l .IsAllowed (action , namespace , actor , bucket , 10 )
128+ l .IsAllowed (action , namespace , actor , bucket , 10 )
125129
126- if c := l .Count (action , actor , bucket ); c != 2 {
130+ if c := l .Count (action , namespace , actor , bucket ); c != 2 {
127131 t .Errorf ("Expected count 2, got %d" , c )
128132 }
129133}
@@ -132,19 +136,20 @@ func TestRemaining(t *testing.T) {
132136 l := New ()
133137 defer l .Stop ()
134138
139+ namespace := []byte ("namespace" )
135140 actor := []byte ("user123" )
136141 action := "test-action"
137142 bucket := time .Minute
138143 allowed := uint64 (5 )
139144
140- if r := l .Remaining (action , actor , bucket , allowed ); r != 5 {
145+ if r := l .Remaining (action , namespace , actor , bucket , allowed ); r != 5 {
141146 t .Errorf ("Expected 5 remaining, got %d" , r )
142147 }
143148
144- l .RateLimit (action , actor , bucket , allowed )
145- l .RateLimit (action , actor , bucket , allowed )
149+ l .IsAllowed (action , namespace , actor , bucket , allowed )
150+ l .IsAllowed (action , namespace , actor , bucket , allowed )
146151
147- if r := l .Remaining (action , actor , bucket , allowed ); r != 3 {
152+ if r := l .Remaining (action , namespace , actor , bucket , allowed ); r != 3 {
148153 t .Errorf ("Expected 3 remaining, got %d" , r )
149154 }
150155}
@@ -153,6 +158,7 @@ func TestRateLimit_Concurrent(t *testing.T) {
153158 l := New ()
154159 defer l .Stop ()
155160
161+ namespace := []byte ("namespace" )
156162 actor := []byte ("user123" )
157163 action := "test-action"
158164 bucket := time .Minute
@@ -162,12 +168,12 @@ func TestRateLimit_Concurrent(t *testing.T) {
162168 allowedCount := 0
163169 var mu sync.Mutex
164170
165- // Run 200 concurrent requests
166- for i := 0 ; i < 200 ; i ++ {
171+ // Run 300 concurrent requests
172+ for i := 0 ; i < 300 ; i ++ {
167173 wg .Add (1 )
168174 go func () {
169175 defer wg .Done ()
170- if l .RateLimit (action , actor , bucket , allowed ) {
176+ if l .IsAllowed (action , namespace , actor , bucket , allowed ) {
171177 mu .Lock ()
172178 allowedCount ++
173179 mu .Unlock ()
@@ -192,11 +198,12 @@ func TestRateLimit_BinaryActor(t *testing.T) {
192198 action := "test-action"
193199 bucket := time .Minute
194200 allowed := uint64 (1 )
201+ namespace := []byte ("namespace" )
195202
196- if ! l .RateLimit (action , actor , bucket , allowed ) {
203+ if ! l .IsAllowed (action , namespace , actor , bucket , allowed ) {
197204 t .Error ("First request should be allowed" )
198205 }
199- if l .RateLimit (action , actor , bucket , allowed ) {
206+ if l .IsAllowed (action , namespace , actor , bucket , allowed ) {
200207 t .Error ("Second request should be rate limited" )
201208 }
202209}
0 commit comments