generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 8
/
alarm_test.go
65 lines (49 loc) · 1.24 KB
/
alarm_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
package watchdog
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAlarm(t *testing.T) {
{
// 间隔 0.5s,每天最多触发次数 20 次
alarm := NewAlarm(AlertOptions{
Interval: time.Microsecond * 500,
MaxTimesForDay: 20,
})
index := 0
for {
if index == 20 {
break
}
assert.True(t, alarm.Tick(), index)
assert.Equal(t, int(uint(index+1)), int(alarm.triggerTimesToDay), index)
index++
time.Sleep(time.Microsecond * 600)
}
assert.False(t, alarm.Tick(), index)
assert.Equal(t, 20, int(alarm.triggerTimesToDay), index)
assert.False(t, alarm.Tick(), index)
assert.Equal(t, 20, int(alarm.triggerTimesToDay), index)
assert.False(t, alarm.Tick(), index)
assert.Equal(t, 20, int(alarm.triggerTimesToDay), index)
}
{
// 间隔 2s,每天最多触发次数 20 次
alarm := NewAlarm(AlertOptions{
Interval: time.Millisecond * 200,
MaxTimesForDay: 20,
})
index := 0
for {
if index == 20 {
break
}
index++
_ = alarm.Tick()
// The default quantum under Linux is 10ms so this is expected behavior and is a property of Linux, not go.
time.Sleep(time.Millisecond * 105)
}
assert.LessOrEqual(t, 10, int(alarm.triggerTimesToDay))
}
}