-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcronex_test.go
112 lines (93 loc) · 2.08 KB
/
cronex_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
package cronex
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/antlabs/timer"
)
func Test_Cronex(t *testing.T) {
table := []string{
"*/1 * * * * *", //每秒执行一次
}
cron := New()
cron.Start() //运行事件循环
count := 2
durationChan := make(chan time.Duration, count)
now := time.Now()
var tm TimerNoder
var err error
for _, tc := range table {
tm, err = cron.AddFunc(tc, func() {
durationChan <- time.Since(now)
})
if err != nil {
t.Logf("err(%v)", err)
return
}
}
// 3s之后关闭
go func() {
time.Sleep(time.Second * 3)
tm.Stop()
cron.Stop()
close(durationChan)
}()
count = 0
first := time.Duration(0)
for tv := range durationChan {
if first == 0 {
first = tv
continue
}
left := first + time.Duration(count)*time.Second
right := first + time.Duration(1.2*float64(time.Duration(count)*time.Second))
if tv < left || tv > right {
t.Logf("count(%d), tv(%v), tv < left(%v) || tv > right(%v)", count, tv, left, right)
return
}
count++
}
if count != 1 {
t.Logf("count(%d), count != 1, callback 没有调用", count)
return
}
}
// 测试下Next函数的时间可正确
func Test_Cronex_ParseNext(t *testing.T) {
var schedule timer.Next
schedule, err := standardParser.Parse("* * * * * *")
if err != nil {
t.Logf("err(%v)", err)
return
}
first := time.Duration(0)
for count := 1; count < 4; count++ {
now := schedule.Next(time.Now())
left := first + time.Duration(0.8*float64(time.Second))
right := first + time.Duration(1.2*float64(time.Second))
tv := now.Sub(time.Now())
if tv < left || tv > right {
t.Logf("tv(%v), tv < left(%v) || tv > right(%v)", tv, left, right)
return
}
}
}
// 多次运行的例子
func Test_Multiple(t *testing.T) {
cron := New()
count := int32(0)
cron.Start()
max := int32(10)
for i := int32(0); i < max; i++ {
cron.AddFunc("* * * * * *", func() {
fmt.Printf("Every Second")
atomic.AddInt32(&count, 1)
})
}
time.Sleep(time.Duration(1.1 * float64(time.Second)))
cron.Stop()
if count != max {
t.Errorf("expected %d, got %d", max, count)
}
}