-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_test.go
173 lines (151 loc) · 3.36 KB
/
cron_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package exec
import (
"testing"
"time"
)
func TestCron_StartStop(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EverySecond()
err := cron.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(4 * time.Second)
cron.Stop()
nowI := i
if i < 3 || i > 4 {
t.Fatal("should run 3 ~ 4 times, but it did", i)
}
time.Sleep(2 * time.Second)
if i-nowI > 1 {
t.Fatal("should stop")
}
}
func TestCron_MultiJobs(t *testing.T) {
i := 0
j := 0
k := 0
cron := NewCron()
job := cron.DoJob(func() { i++ })
job.EverySecond()
cron.DoJob(func() { j++ }).Every(29)
cron.DoJob(func() { k++ }).EveryMinute()
cron.Stopped = false
location := time.Now().Location()
for s := 29; s <= 89; s++ {
t := time.Date(2018, 1, 1, 1, s/60, s%60, 0, location)
cron.doJobs(&t)
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 61 {
t.Fatal("i should run 61 times, but it did", i)
}
if j != 2 {
t.Fatal("j should run 2 times, but it did", j)
}
if k != 1 {
t.Fatal("k should run 1 time, but it did", k)
}
}
func TestCron_EveryHour(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryHour()
cron.Stopped = false
location := time.Now().Location()
for n := 10; n < 180; n++ {
t := time.Date(2018, 1, 1, n/60, n, 0, 0, location)
cron.doJobs(&t)
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 2 {
t.Fatal("i should run 2 times, but it did", i)
}
}
func TestCron_EveryDay(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryDay()
cron.Stopped = false
location := time.Now().Location()
for n := 2; n < 24*3; n++ {
t := time.Date(2018, 1, n/24, n, 0, 0, 0, location)
cron.doJobs(&t)
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 2 {
t.Fatal("i should run 2 times, but it did", i)
}
}
func TestCron_EveryMonth(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryMonth()
cron.Stopped = false
location := time.Now().Location()
for m := 1; m < 3; m++ {
for d := 0; d < 24; d++ {
t := time.Date(2018, time.Month(m), d, 0, 0, 0, 0, location)
cron.doJobs(&t)
}
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 2 {
t.Fatal("i should run 2 times, but it did", i)
}
}
func TestCron_EveryYear(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryYear()
cron.Stopped = false
location := time.Now().Location()
for y := 2015; y < 2018; y++ {
for m := 1; m <= 12; m++ {
t := time.Date(y, time.Month(m), 1, 0, 0, 0, 0, location)
cron.doJobs(&t)
}
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 3 {
t.Fatal("i should run 3 times, but it did", i)
}
}
func TestCron_EveryWeekDay(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryWeekDay(1, 1, 2, 3)
cron.Stopped = false
location := time.Now().Location()
for d := 1; d <= 15; d++ {
t := time.Date(2018, 1, d, 3, 2, 1, 0, location)
cron.doJobs(&t)
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 3 {
t.Fatal("i should run 3 times, but it did", i)
}
}
func TestCron_EveryWeek(t *testing.T) {
i := 0
cron := NewCron()
cron.DoJob(func() { i++ }).EveryWeek()
cron.Stopped = false
location := time.Now().Location()
for d := 7; d <= 21; d++ {
t := time.Date(2018, 1, d, 0, 0, 0, 0, location)
cron.doJobs(&t)
}
cron.Stopped = true
time.Sleep(time.Second)
if i != 3 {
t.Fatal("i should run 3 times, but it did", i)
}
}