-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_limit_store_test.go
124 lines (116 loc) · 2.68 KB
/
map_limit_store_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
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
package ratelimiter
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewMapLimitStore(t *testing.T) {
type args struct {
expirationTime time.Duration
flushInterval time.Duration
}
tests := []struct {
name string
args args
}{
{
name: "test_NewMapLimitStore",
args: args{
expirationTime: 1 * time.Minute,
flushInterval: 2 * time.Second,
},
},
}
for _, tt := range tests {
mapLimitStore := NewMapLimitStore(tt.args.expirationTime, tt.args.flushInterval)
assert.Equal(t, tt.args.expirationTime, mapLimitStore.expirationTime)
}
}
func TestMapLimitStore_Inc(t *testing.T) {
tests := []struct {
name string
key string
window time.Time
wantErr bool
}{
{
name: "test_MapLimitStore_Inc",
key: "tt",
window: time.Now().UTC(),
wantErr: false,
},
}
for _, tt := range tests {
m := NewMapLimitStore(1*time.Minute, 10*time.Second)
err := m.Inc(tt.key, tt.window)
assert.NoError(t, err)
prevVal, currVal, err := m.Get(tt.key, tt.window, tt.window)
assert.NoError(t, err)
assert.Equal(t, int64(1), prevVal)
assert.Equal(t, int64(1), currVal)
}
}
func TestMapLimitStore_Get(t *testing.T) {
type args struct {
key string
previousWindow time.Time
currentWindow time.Time
}
tests := []struct {
name string
args args
wantPrevValue int64
wantCurrValue int64
wantErr bool
}{
{
name: "test_MapLimitStore_Get",
args: args{
key: "tt",
previousWindow: time.Now().UTC().Add(-1 * time.Minute),
currentWindow: time.Now().UTC(),
},
wantPrevValue: 10,
wantCurrValue: 5,
wantErr: false,
},
}
for _, tt := range tests {
m := NewMapLimitStore(1*time.Minute, 10*time.Second)
m.data[mapKey(tt.args.key, tt.args.previousWindow)] = limitValue{val: tt.wantPrevValue}
m.data[mapKey(tt.args.key, tt.args.currentWindow)] = limitValue{val: tt.wantCurrValue}
prevVal, currVal, err := m.Get(tt.args.key, tt.args.previousWindow, tt.args.currentWindow)
assert.NoError(t, err)
assert.Equal(t, tt.wantPrevValue, prevVal)
assert.Equal(t, tt.wantCurrValue, currVal)
}
}
func TestMapLimitStore_Size(t *testing.T) {
tests := []struct {
name string
key string
window time.Time
size int
}{
{
name: "test_MapLimitStore_Size",
key: "tt",
window: time.Now().UTC(),
size: 1,
},
{
name: "test_MapLimitStore_Size",
key: "tt",
window: time.Time{},
size: 0,
},
}
for _, tt := range tests {
m := NewMapLimitStore(1*time.Minute, 10*time.Second)
if !tt.window.IsZero() {
err := m.Inc(tt.key, tt.window)
assert.NoError(t, err)
}
assert.Equal(t, tt.size, m.Size())
}
}