@@ -29,7 +29,7 @@ func setupTestRedis(t *testing.T) (*RedisClient, *miniredis.Miniredis) {
2929 return client , mr
3030}
3131
32- func TestUpdateRateLimit (t * testing.T ) {
32+ func TestCheckRateLimit (t * testing.T ) {
3333 client , mr := setupTestRedis (t )
3434 defer mr .Close ()
3535
@@ -79,7 +79,7 @@ func TestUpdateRateLimit(t *testing.T) {
7979 wantErr : false ,
8080 },
8181 {
82- name : "should reset count after period expires" ,
82+ name : "should ignore existing counter (treat as allowed) after period expires" ,
8383 projectID : "project4" ,
8484 eventsLimit : 5 ,
8585 eventsPeriod : 60 ,
@@ -101,12 +101,16 @@ func TestUpdateRateLimit(t *testing.T) {
101101 wantErr : false ,
102102 },
103103 {
104- name : "should handle multiple calls up to limit " ,
104+ name : "should fail if limit is already reached " ,
105105 projectID : "project6" ,
106106 eventsLimit : 3 ,
107107 eventsPeriod : 60 ,
108- calls : 4 ,
109- wantAllowed : false , // Last call should be denied
108+ setup : func () {
109+ client .rdb .HSet (client .ctx , "rate_limits" , "project6" ,
110+ fmt .Sprintf ("%d:%d" , time .Now ().Unix (), 3 ))
111+ },
112+ calls : 4 ,
113+ wantAllowed : false ,
110114 wantErr : false ,
111115 },
112116 }
@@ -123,7 +127,7 @@ func TestUpdateRateLimit(t *testing.T) {
123127
124128 // Make the specified number of calls
125129 for i := 0 ; i < tt .calls ; i ++ {
126- lastAllowed , lastErr = client .UpdateRateLimit (tt .projectID , tt .eventsLimit , tt .eventsPeriod )
130+ lastAllowed , lastErr = client .CheckRateLimit (tt .projectID , tt .eventsLimit , tt .eventsPeriod )
127131 }
128132
129133 if tt .wantErr {
@@ -136,7 +140,7 @@ func TestUpdateRateLimit(t *testing.T) {
136140 }
137141}
138142
139- func TestUpdateRateLimitConcurrent (t * testing.T ) {
143+ func TestCheckRateLimitConcurrent (t * testing.T ) {
140144 client , mr := setupTestRedis (t )
141145 defer mr .Close ()
142146
@@ -148,15 +152,20 @@ func TestUpdateRateLimitConcurrent(t *testing.T) {
148152 callsPerRoutine = 20
149153 )
150154
151- var rejectedCount int = 0
155+ initialValue := fmt .Sprintf ("%d:%d" , time .Now ().Unix (), eventsLimit )
156+ if err := client .rdb .HSet (client .ctx , "rate_limits" , projectID , initialValue ).Err (); err != nil {
157+ t .Fatalf ("failed to seed rate limit: %v" , err )
158+ }
159+
160+ var rejectedCount int := 0
152161
153162 done := make (chan bool )
154163
155164 // Launch multiple goroutines to test concurrent access
156165 for i := 0 ; i < goroutines ; i ++ {
157166 go func () {
158167 for j := 0 ; j < callsPerRoutine ; j ++ {
159- allowed , err := client .UpdateRateLimit (projectID , eventsLimit , eventsPeriod )
168+ allowed , err := client .CheckRateLimit (projectID , eventsLimit , eventsPeriod )
160169 assert .NoError (t , err )
161170 if ! allowed {
162171 rejectedCount ++
@@ -171,17 +180,11 @@ func TestUpdateRateLimitConcurrent(t *testing.T) {
171180 <- done
172181 }
173182
174- // Verify the total number of successful updates doesn't exceed the limit
183+ // Verify the stored value remains unchanged and all checks are denied
175184 val , err := client .rdb .HGet (client .ctx , "rate_limits" , projectID ).Result ()
176185 assert .NoError (t , err )
177- assert .NotEmpty (t , val )
178-
179- // The total count should not exceed the events limit
180- count := 0
181- _ , err = fmt .Sscanf (val , "%d:%d" , & count , & count )
182- assert .NoError (t , err )
183- assert .Equal (t , count , eventsLimit )
184- assert .Equal (t , rejectedCount , goroutines * callsPerRoutine - eventsLimit )
185- t .Logf ("count: %d" , count )
186+ assert .Equal (t , initialValue , val )
187+ assert .Equal (t , int64 (goroutines * callsPerRoutine ), rejectedCount )
188+ t .Logf ("stored value: %s" , val )
186189 t .Logf ("rejectedCount: %d" , rejectedCount )
187190}
0 commit comments