forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
67 lines (53 loc) · 937 Bytes
/
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
package timer_test
import (
"fmt"
"github.com/name5566/leaf/timer"
"time"
)
func ExampleTimer() {
d := timer.NewDispatcher(10)
// timer 1
d.AfterFunc(1, func() {
fmt.Println("My name is Leaf")
})
// timer 2
t := d.AfterFunc(1, func() {
fmt.Println("will not print")
})
t.Stop()
// dispatch
(<-d.ChanTimer).Cb()
// Output:
// My name is Leaf
}
func ExampleCronExpr() {
cronExpr, err := timer.NewCronExpr("0 * * * *")
if err != nil {
return
}
fmt.Println(cronExpr.Next(time.Date(
2000, 1, 1,
20, 10, 5,
0, time.UTC,
)))
// Output:
// 2000-01-01 21:00:00 +0000 UTC
}
func ExampleCron() {
d := timer.NewDispatcher(10)
// cron expr
cronExpr, err := timer.NewCronExpr("* * * * * *")
if err != nil {
return
}
// cron
var c *timer.Cron
c = d.CronFunc(cronExpr, func() {
fmt.Println("My name is Leaf")
c.Stop()
})
// dispatch
(<-d.ChanTimer).Cb()
// Output:
// My name is Leaf
}