-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
190 lines (171 loc) · 4.33 KB
/
Copy pathclient_test.go
File metadata and controls
190 lines (171 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package redis
import (
"context"
"fmt"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)
func setupTestRedis(t *testing.T) (*RedisClient, *miniredis.Miniredis) {
// Create a mock Redis server
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("Failed to create mock redis: %v", err)
}
// Create Redis client connected to mock server
client := &RedisClient{
rdb: redis.NewClient(&redis.Options{
Addr: mr.Addr(),
}),
ctx: context.Background(),
}
return client, mr
}
func TestCheckRateLimit(t *testing.T) {
client, mr := setupTestRedis(t)
defer mr.Close()
tests := []struct {
name string
projectID string
eventsLimit int64
eventsPeriod int64
setup func()
calls int
wantAllowed bool
wantErr bool
}{
{
name: "should allow when no previous events",
projectID: "project1",
eventsLimit: 10,
eventsPeriod: 60,
calls: 1,
wantAllowed: true,
wantErr: false,
},
{
name: "should allow when under limit",
projectID: "project2",
eventsLimit: 10,
eventsPeriod: 60,
setup: func() {
client.rdb.HSet(client.ctx, "rate_limits", "project2",
fmt.Sprintf("%d:%d", time.Now().Unix()-30, 5))
},
calls: 1,
wantAllowed: true,
wantErr: false,
},
{
name: "should deny when at limit",
projectID: "project3",
eventsLimit: 5,
eventsPeriod: 60,
setup: func() {
client.rdb.HSet(client.ctx, "rate_limits", "project3",
fmt.Sprintf("%d:%d", time.Now().Unix()-30, 5))
},
calls: 1,
wantAllowed: false,
wantErr: false,
},
{
name: "should ignore existing counter (treat as allowed) after period expires",
projectID: "project4",
eventsLimit: 5,
eventsPeriod: 60,
setup: func() {
client.rdb.HSet(client.ctx, "rate_limits", "project4",
fmt.Sprintf("%d:%d", time.Now().Unix()-61, 5))
},
calls: 1,
wantAllowed: true,
wantErr: false,
},
{
name: "should allow all when limit is 0",
projectID: "project5",
eventsLimit: 0,
eventsPeriod: 60,
calls: 5,
wantAllowed: true,
wantErr: false,
},
{
name: "should fail if limit is already reached",
projectID: "project6",
eventsLimit: 3,
eventsPeriod: 60,
setup: func() {
client.rdb.HSet(client.ctx, "rate_limits", "project6",
fmt.Sprintf("%d:%d", time.Now().Unix(), 3))
},
calls: 4,
wantAllowed: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Run setup if provided
if tt.setup != nil {
tt.setup()
}
var lastAllowed bool
var lastErr error
// Make the specified number of calls
for i := 0; i < tt.calls; i++ {
lastAllowed, lastErr = client.CheckRateLimit(tt.projectID, tt.eventsLimit, tt.eventsPeriod)
}
if tt.wantErr {
assert.Error(t, lastErr)
} else {
assert.NoError(t, lastErr)
}
assert.Equal(t, tt.wantAllowed, lastAllowed)
})
}
}
func TestCheckRateLimitConcurrent(t *testing.T) {
client, mr := setupTestRedis(t)
defer mr.Close()
const (
projectID = "concurrent-project"
eventsLimit = 90
eventsPeriod = 60
goroutines = 10
callsPerRoutine = 20
)
initialValue := fmt.Sprintf("%d:%d", time.Now().Unix(), eventsLimit)
if err := client.rdb.HSet(client.ctx, "rate_limits", projectID, initialValue).Err(); err != nil {
t.Fatalf("failed to seed rate limit: %v", err)
}
var rejectedCount int64 = 0
done := make(chan bool)
// Launch multiple goroutines to test concurrent access
for i := 0; i < goroutines; i++ {
go func() {
for j := 0; j < callsPerRoutine; j++ {
allowed, err := client.CheckRateLimit(projectID, eventsLimit, eventsPeriod)
assert.NoError(t, err)
if !allowed {
rejectedCount++
}
}
done <- true
}()
}
// Wait for all goroutines to complete
for i := 0; i < goroutines; i++ {
<-done
}
// Verify the stored value remains unchanged and all checks are denied
val, err := client.rdb.HGet(client.ctx, "rate_limits", projectID).Result()
assert.NoError(t, err)
assert.Equal(t, initialValue, val)
assert.Equal(t, int64(goroutines*callsPerRoutine), rejectedCount)
t.Logf("stored value: %s", val)
t.Logf("rejectedCount: %d", rejectedCount)
}