-
Notifications
You must be signed in to change notification settings - Fork 1
/
trigger_test.go
110 lines (104 loc) · 2.41 KB
/
trigger_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
package red
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestTrigger_Do(t *testing.T) {
for _, client := range Connecters {
trigger_Do(t, client)
}
}
func trigger_Do(t *testing.T, client Connecter) {
ctx := context.TODO()
do := func (namespace string) (bool, error) {
return Trigger{
Namespace: namespace,
Interval: time.Second*1,
Threshold: 3,
Frequency: time.Second*1,
}.Do(ctx, client)
}
_, err := DEL{
Keys: []string{"trigger_alarm_1", "trigger_alarm_1:frequency"},
}.Do(ctx, client)
assert.NoError(t, err)
// 第一次触发
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
value, isNil, err := GET{Key: "trigger_alarm_1"}.Do(ctx, client)
assert.NoError(t, err)
assert.Equal(t,isNil, false)
assert.Equal(t,value, "1")
}
// 第二次触发
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
value, isNil, err := GET{Key: "trigger_alarm_1"}.Do(ctx, client)
assert.NoError(t, err)
assert.Equal(t,isNil, false)
assert.Equal(t,value, "2")
}
// 第三次触发
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, true)
value, isNil, err := GET{Key: "trigger_alarm_1"}.Do(ctx, client)
assert.NoError(t, err)
assert.Equal(t,isNil, true)
assert.Equal(t,value, "")
result, err := PTTL{
Key: "trigger_alarm_1:frequency",
}.Do(ctx, client)
assert.NoError(t, err)
assert.Less(t, result.TTL.Milliseconds(), int64(1000))
assert.Greater(t, result.TTL.Milliseconds(), int64(500))
}
// 第456789次触发
{
// 4
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
}
// 5
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
}
// 6 不触发
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
}
// sleep 1秒等待 frequency 过期
time.Sleep(time.Second)
// 7
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
}
// 8
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, false)
}
// 9 触发
{
triggered,err := do("trigger_alarm_1")
assert.NoError(t, err)
assert.Equal(t,triggered, true)
}
}
}