-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_limiter_test.go
95 lines (92 loc) · 2.3 KB
/
set_limiter_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
package red
import (
"context"
"github.com/stretchr/testify/assert"
"log"
"testing"
"time"
)
func TestSetLimiter_Do(t *testing.T) {
for _, client := range Connecters {
setLimiter_Do(t, client)
}
}
func setLimiter_Do(t *testing.T, client Connecter) {
ctx := context.TODO()
_, err := DEL{
Keys: []string{"set_Limiter_1"},
}.Do(ctx, client)
_, err = DEL{
Keys: []string{"set_Limiter_2"},
}.Do(ctx, client)
assert.NoError(t, err)
/* 第1组 第1个 第1次 */
limited, err := SetLimiter{
Key: "set_Limiter_1",
Member: "a",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
// 验证过期时间
result, err := PTTL{
Key: "set_Limiter_1",
}.Do(ctx, client);assert.NoError(t, err)
log.Print(result.TTL)
tenSecond := time.Second*10
assert.Less(t, result.TTL.Milliseconds(), tenSecond.Milliseconds())
/* 第1组 第1个 第2次 */
time.Sleep(time.Second)
limited, err = SetLimiter{
Key: "set_Limiter_1",
Member: "a",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
/* 第1组 第2个 */
time.Sleep(time.Second)
limited, err = SetLimiter{
Key: "set_Limiter_1",
Member: "b",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
/* 第1组 第3个 */
time.Sleep(time.Second)
limited, err = SetLimiter{
Key: "set_Limiter_1",
Member: "c",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
/* 第1组 第4个 */
time.Sleep(time.Second)
limited, err = SetLimiter{
Key: "set_Limiter_1",
Member: "d",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, true)
/* 第2组 */
time.Sleep(time.Second)
limited, err = SetLimiter{
Key: "set_Limiter_2",
Member: "a",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
/* 第1组 过期后 */
time.Sleep(time.Second*7) // 等待过期
limited, err = SetLimiter{
Key: "set_Limiter_1",
Member: "e",
Expire: time.Second * 10,
Maximum: 3,
}.Do(ctx, client);assert.NoError(t, err)
assert.Equal(t, limited, false)
}