-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter_test.go
92 lines (76 loc) · 2.23 KB
/
ratelimiter_test.go
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
package main
import (
"sync"
"testing"
"time"
)
// Normally we would use a library that supports time mocking like testify (https://github.com/stretchr/testify)
// but requirement does not allow external libraries
func TestResetWindow(t *testing.T) {
test_rate := 40
test_window := 1 * time.Second
mockTime := time.Now() // Replace with mocking library
wt := &WindowTracker{
mu: sync.Mutex{},
StartTime: mockTime,
RequestCount: 0,
config: &RateLimiterConfig{
Rate: test_rate,
Window: test_window,
},
}
// Fill window
wt.RequestCount = 0
for i := 0; i < test_rate; i++ {
wt.AllowRequest()
}
// ensure window is full
allowed := wt.AllowRequest()
if allowed || wt.RequestCount != test_rate {
t.Errorf("Expected request denied after reaching rate, got allowed: %v, request count: %v", allowed, wt.RequestCount)
}
// reset window
wt.ResetWindow()
if wt.RequestCount != 0 {
t.Errorf("Expected request count to be reset to zero but found:request count: %v", wt.RequestCount)
}
now := time.Now()
if now.Sub(wt.StartTime) < 0 {
t.Errorf("Expected start time to be reset to now but found: %v", wt.StartTime)
}
}
func TestAllowRequest(t *testing.T) {
test_rate := 40
test_window := 1 * time.Second
mockTime := time.Now() // Replace with mocking library
wt := &WindowTracker{
mu: sync.Mutex{},
StartTime: mockTime,
RequestCount: 0,
config: &RateLimiterConfig{
Rate: test_rate,
Window: test_window,
},
}
// Test within Window
allowed := wt.AllowRequest()
if !allowed || wt.RequestCount != 1 {
t.Errorf("Expected request allowed within window, got allowed: %v, request count: %v", allowed, wt.RequestCount)
}
// Fill window
wt.RequestCount = 0
for i := 0; i < test_rate; i++ {
wt.AllowRequest()
}
allowed = wt.AllowRequest()
if allowed || wt.RequestCount != test_rate {
t.Errorf("Expected request denied after reaching rate, got allowed: %v, request count: %v", allowed, wt.RequestCount)
}
// Sleep to allow refill
time.Sleep(time.Second * 2)
// Test after Window is reset
allowed = wt.AllowRequest()
if !allowed || wt.RequestCount != 1 {
t.Errorf("Expected request allowed after window reset, got allowed: %v, request count: %v", allowed, wt.RequestCount)
}
}