Skip to content

Commit 9ff2cd9

Browse files
committed
chore(): update client test and rename the method of the client
1 parent 5d19608 commit 9ff2cd9

4 files changed

Lines changed: 26 additions & 23 deletions

File tree

pkg/redis/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ func (r *RedisClient) CheckAvailability() bool {
176176
return pong == "PONG"
177177
}
178178

179-
// UpdateRateLimit checks and updates the rate limit for a project using a Lua script
180-
func (r *RedisClient) UpdateRateLimit(projectID string, eventsLimit int64, eventsPeriod int64) (bool, error) {
179+
// CheckRateLimit checks and updates the rate limit for a project using a Lua script
180+
func (r *RedisClient) CheckRateLimit(projectID string, eventsLimit int64, eventsPeriod int64) (bool, error) {
181181
// If eventsLimit is 0, we don't need to update the rate limit
182182
if eventsLimit == 0 {
183183
return true, nil

pkg/redis/client_test.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

pkg/server/errorshandler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (handler *Handler) process(body []byte) ResponseMessage {
7777
return ResponseMessage{402, true, "Project has exceeded the events limit"}
7878
}
7979

80-
rateWithinLimit, err := handler.RedisClient.UpdateRateLimit(projectId, projectLimits.EventsLimit, projectLimits.EventsPeriod)
80+
rateWithinLimit, err := handler.RedisClient.CheckRateLimit(projectId, projectLimits.EventsLimit, projectLimits.EventsPeriod)
8181
if err != nil {
8282
log.Errorf("Failed to update rate limit: %s", err)
8383
return ResponseMessage{402, true, "Failed to update rate limit"}

pkg/server/errorshandler/handler_sentry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (handler *Handler) HandleSentry(ctx *fasthttp.RequestCtx) {
107107
return
108108
}
109109

110-
rateWithinLimit, err := handler.RedisClient.UpdateRateLimit(projectId, projectLimits.EventsLimit, projectLimits.EventsPeriod)
110+
rateWithinLimit, err := handler.RedisClient.CheckRateLimit(projectId, projectLimits.EventsLimit, projectLimits.EventsPeriod)
111111
if err != nil {
112112
log.Errorf("Failed to update rate limit: %s", err)
113113
sendAnswerHTTP(ctx, ResponseMessage{402, true, "Failed to update rate limit"})

0 commit comments

Comments
 (0)