-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathclock_example_test.go
131 lines (114 loc) · 2.71 KB
/
clock_example_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
package clock
import (
"fmt"
"log"
"time"
)
//ExampleClock_AddJobRepeat, show how to add repeat job。
func ExampleClock_AddJobRepeat() {
var (
myClock = NewClock()
)
fn := func() {
fmt.Println("schedule repeat")
}
//create a task that executes three times,interval 50 millisecond
_, inserted := myClock.AddJobRepeat(time.Duration(time.Millisecond*50), 3, fn)
if !inserted {
log.Println("failure")
}
//wait a second,watching
time.Sleep(time.Second)
//Output:
//
//schedule repeat
//schedule repeat
//schedule repeat
}
//ExampleClock_AddJobForEvent,show receive signal when job is called by system
func ExampleClock_AddJobForEvent() {
var (
myClock = NewClock()
)
//define a repeat task
fn := func() {
//fmt.Println("schedule repeat")
}
//add in clock,execute three times,interval 200 millisecond
job, inserted := myClock.AddJobRepeat(time.Duration(time.Millisecond*200), 3, fn)
if !inserted {
log.Println("failure")
}
go func() {
for _ = range job.C() {
fmt.Println("job done")
}
}()
//wait a second,watching
time.Sleep(time.Second)
//Output:
//
//job done
//job done
//job done
}
//ExampleClock_AddJobWithInterval,show how to add a job just do once with interval
func ExampleClock_AddJobWithInterval() {
var (
jobClock = NewClock()
jobFunc = func() {
fmt.Println("schedule once")
}
)
//add a task that executes once,interval 100 millisecond
jobClock.AddJobWithInterval(time.Duration(100*time.Millisecond), jobFunc)
//wait a second,watching
time.Sleep(1 * time.Second)
//Output:
//
//schedule once
}
//ExampleClock_AddJobWithDeadtime,show how to add a job with a point time
func ExampleClock_AddJobWithDeadtime() {
var (
myClock = Default()
jobFunc = func() {
fmt.Println("schedule once")
}
actionTime = time.Now().Add(time.Millisecond * 500)
)
//创建一次性任务,定时500ms
job, _ := myClock.AddJobWithDeadtime(actionTime, jobFunc)
//任务执行前,撤销任务
time.Sleep(time.Millisecond * 300)
job.Cancel()
//等待2秒,正常情况下,事件不会再执行
time.Sleep(2 * time.Second)
//Output:
//
//
}
//ExampleClock_AddJobWithDeadtime,show how to cancel a job which added in system
func ExampleClock_RmJob() {
var (
myClock = NewClock()
count int
jobFunc = func() {
count++
fmt.Println("do ", count)
}
)
//创建任务,间隔1秒,执行两次
job, _ := myClock.AddJobRepeat(time.Second*1, 2, jobFunc)
//任务执行前,撤销任务
time.Sleep(time.Millisecond * 500)
job.Cancel()
//等待2秒,正常情况下,事件不会再执行
time.Sleep(2 * time.Second)
//再次添加一个任务,病观察
myClock.AddJobRepeat(time.Second*1, 1, jobFunc)
time.Sleep(time.Second * 2)
//Output:
//
//do 1
}