-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_test.go
139 lines (115 loc) · 2.91 KB
/
schedule_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
/*
*
*
* * Copyright 2019 koalaone@163.com
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package schedule
import (
"context"
"log"
"testing"
"time"
)
func EventSecond(param string) {
log.Printf("second event value:%v\n", param)
}
func EventSecondWithWait(param string) {
log.Printf("second event value:%v\n", param)
time.Sleep(12 * time.Second)
}
func EventMinute(param string) {
log.Printf("minute event value:%v\n", param)
}
func EventHour(param string) {
log.Printf("hour event value:%v\n", param)
}
func EventDay(param string) {
log.Printf("day event value:%v\n", param)
}
func EventAtDatetime(param string) {
log.Printf("AtDatetime event value:%v\n", param)
}
func TestScheduler(t *testing.T) {
err := EverySeconds(1).Do(EventSecond, "second")
if err != nil {
t.Errorf("test schedule EventSecond error:%v", err.Error())
return
}
err = EverySeconds(2).Do(EventSecondWithWait, "second")
if err != nil {
t.Errorf("test schedule EventSecondWithWait error:%v", err.Error())
return
}
err = EveryMinutes(1).Do(EventMinute, "minute")
if err != nil {
t.Errorf("test schedule EventMinute error:%v", err.Error())
return
}
err = EveryHours(1).Do(EventHour, "hour")
if err != nil {
t.Errorf("test schedule EventHour error:%v", err.Error())
return
}
err = EveryDays(1).Do(EventDay, "day")
if err != nil {
t.Errorf("test schedule EventDay error:%v", err.Error())
return
}
err = AtDateTime(2018, time.December, 21, 16, 59, 10).
Do(EventAtDatetime, "at_datetime")
if err != nil {
t.Errorf("test schedule AtDateTime error:%v", err.Error())
return
}
ctx, cancel := context.WithCancel(context.Background())
Start(ctx)
defer func() {
cancel()
}()
time.Sleep(3 * time.Minute)
}
func TestSchedulerError(t *testing.T) {
// error test
err := EverySeconds(0).Do(EventSecond, "second")
if err != nil {
t.Errorf("test schedule EventSecond error:%v", err.Error())
return
}
// error test
err = EverySeconds(1).Do(nil, "second")
if err == nil {
t.Errorf("test schedule EventSecond test error")
return
}
ctx, cancel := context.WithCancel(context.Background())
Start(ctx)
defer func() {
cancel()
}()
// stop and start
Stop()
Start(ctx)
// test need to use
// select {}
time.Sleep(10 * time.Second)
}
func TestGetScheduler(t *testing.T) {
GetScheduler()
}
func TestScheduler_Add(t *testing.T) {
// error
GetScheduler().Add(nil)
}