-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding_window_test.go
51 lines (40 loc) · 1008 Bytes
/
sliding_window_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
package ratelimit
import (
"context"
"testing"
"time"
)
func TestSlidingWindowLimiter_Allow(t *testing.T) {
limit := PerSecond(1)
key := "key"
s := &mockStore[slidingWindowRecord]{record: &slidingWindowRecord{}}
limiter := NewSlidingWindowLimiter(s)
result, err := limiter.Allow(context.Background(), key, limit)
if err != nil {
t.Error(err)
}
if !result.Allowed {
t.Error("this attempt should be allowed")
}
result, err = limiter.Allow(context.Background(), key, limit)
if err != nil {
t.Error(err)
}
if result.Allowed {
t.Error("this attempt should'nt be allowed")
}
if s.record.CurrentCount != 1 || s.record.PrevCount != 0 {
t.Error("wrong current or prev Count")
}
time.Sleep(time.Second)
result, err = limiter.Allow(context.Background(), key, limit)
if err != nil {
t.Error(err)
}
if !result.Allowed {
t.Errorf("this attempt should be allowed")
}
if s.record.CurrentCount != 1 || s.record.PrevCount != 1 {
t.Error("wrong current or prev Count")
}
}