-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.go
163 lines (150 loc) · 4.16 KB
/
timer_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
package timer
import (
"fmt"
"os"
"os/signal"
"syscall"
"testing"
"time"
)
/*
*
Tests add task functions
=== RUN Test_Func
[2023-04-24 16:40:36] hytimer:hello world
[2023-04-24 16:40:39] hytimer:hello world
[2023-04-24 16:40:42] hytimer:hello world
[2023-04-24 16:40:45] hytimer:hello world
[2023-04-24 16:40:48] hytimer:hello world
[2023-04-24 16:40:51] hytimer:hello world
........
*/
func Test_ScheduleFunc(t *testing.T) {
fmt.Println("Begin")
//Initializes and sets the queue size
inst := GetInstance(10000)
//Adding a scheduled task
//Once every 3 seconds
//Callback function:FuncTest1
//Callback function input param: "hello world"
inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello world")
//Scheduled task start
inst.StarTimer()
select {}
}
/*
*
Set the number of runs. After 3 runs, it will stop
=== RUN Test_RunTimes
[2023-04-24 16:47:28] hytimer:hello world
[2023-04-24 16:47:31] hytimer:hello world
[2023-04-24 16:47:34] hytimer:hello world
*/
func Test_RunTimes(t *testing.T) {
//Initializes and sets the queue size
inst := GetInstance(10000)
//Adding a scheduled task
//Once every 3 seconds
//Callback function:FuncTest1
//Callback function input param: "hello world"
//MaxRunTimes:3 It will stop after 3 runs
inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello world", 3)
//Scheduled task start
inst.StarTimer()
select {}
}
/*
*TestHeapTimer_Cancel
Tests how to use Cancel
=== RUN Test_Cancel
[2023-04-24 16:55:06] hytimer:hello world
[2023-04-24 16:55:09] hytimer:hello world
[2023-04-24 16:55:12] hytimer:hello world
10 seconds is up,Timer Over
*/
func Test_Cancel(t *testing.T) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
//Initializes and sets the queue size
inst := GetInstance(10000)
go func() {
//Adding a scheduled task
//Once every 3 seconds
//Callback function:FuncTest1
//Callback function input param: "hello world"
//MaxRunTimes:3 It will stop after 3 runs
inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello world")
//Scheduled task start
inst.StarTimer()
}()
time.Sleep(10 * time.Second)
inst.Cancel()
fmt.Println("10 seconds is up,Timer Over")
<-sigs
}
/*
*Test_StopByIndex
=== RUN Test_StopByIndex
[2023-04-24 17:02:35] hytimer:hello world
[2023-04-24 17:02:38] hytimer:hello world
[2023-04-24 17:02:41] hytimer:hello world
My schedule index is:37456253 5 seconds will be close
[2023-04-24 17:02:44] hytimer:hello world
schedule index [37456253] close success
--- PASS: Test_StopByIndex (15.01s)
PASS
ok clog/hytimer 15.201s
*/
func Test_StopByIndex(t *testing.T) {
//Initializes and sets the queue size
inst := GetInstance(10000)
//Adding a scheduled task
//Once every 3 seconds
//Callback function:FuncTest1
//Callback function input param: "hello world"
//MaxRunTimes:3 It will stop after 3 runs
sid := inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello world")
//Scheduled task start
inst.StarTimer()
//sleep
time.Sleep(10 * time.Second)
fmt.Println(fmt.Sprintf("My schedule index is:%d 5 seconds will be close", sid))
time.Sleep(5 * time.Second)
inst.StopIdx(sid)
fmt.Println(fmt.Sprintf("schedule index [%d] close success", sid))
}
func Test_BatchSchedule(t *testing.T) {
//Initializes and sets the queue size
inst := GetInstance(10000)
for i := 0; i < 10; i++ {
inst.AddScheduleFunc(3*time.Second, FuncTest1, fmt.Sprintf("hello my id is=>%d", i))
}
for i := 10; i < 20; i++ {
inst.AddScheduleFunc(10*time.Second, FuncTest1, fmt.Sprintf("hello my id is=>%d", i))
}
inst.StarTimer()
select {}
}
func Test_GetRunScIndexList(t *testing.T) {
inst := GetInstance(10000)
go func() {
inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello world")
inst.AddScheduleFunc(3*time.Second, FuncTest1, "hello golang")
//Scheduled task start
inst.StarTimer()
}()
inst.StarTimer()
//sleep
time.Sleep(10 * time.Second)
//get length
slen := inst.GetRunScLength()
fmt.Println("schedule length=>", slen)
//get index list
slist := inst.GetRunScIndexList()
fmt.Println("schedule list=>", *slist)
select {}
}
// test function
func FuncTest1(v interface{}) {
fmt.Println(fmt.Sprintf("[%s] hytimer:%s", time.Now().Format("2006-01-02 15:04:05"), v.(string)))
}