-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
152 lines (134 loc) · 3.44 KB
/
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package xysched_test
import (
"fmt"
"time"
"github.com/xybor-x/xysched"
)
func panicFunc() {
panic("custom panic function")
}
func Example() {
// Example 1: You can use the global scheduler throughout program without
// creating a new one.
var done = make(chan any)
var future = xysched.NewTask(func() {
fmt.Println("1. bar bar")
close(done)
})
xysched.Now() <- future
<-done
// Example 2: Scheduler can schedule one future After or At a time.
done = make(chan any)
future = xysched.NewTask(func() {
fmt.Println("2. barfoo")
close(done)
})
xysched.After(time.Millisecond) <- future
<-done
// Output:
// 1. bar bar
// 2. barfoo
}
func ExampleTask() {
var scheduler = xysched.NewScheduler("")
defer scheduler.Stop()
// Example 1: Task is a simple future used for scheduling to run a function.
var done = make(chan any)
var future = xysched.NewTask(func(a ...any) {
fmt.Println(a...)
close(done)
}, "1. foo")
scheduler.Now() <- future
<-done
// Example 2: Callback will be run after the task completed.
done = make(chan any)
future = xysched.NewTask(fmt.Println, "2. foo foo")
future.Callback(func() { close(done) })
scheduler.Now() <- future
<-done
// Example 3: Then adds a callback handling returned values of task after
// task completed.
done = make(chan any)
future = xysched.NewTask(fmt.Sprintf, "3. foo %s", "bar")
future.Then(func(s string) {
fmt.Println(s)
close(done)
})
scheduler.Now() <- future
<-done
// Example 4: Catch adds a callback handling the panicked error of task if
// the task panicked.
// NOTE: if task panics a non-error interface, it will be wrapped into
// xysched.CallError.
done = make(chan any)
future = xysched.NewTask(panicFunc)
future.Then(func() {
fmt.Println("4. Then branch")
close(done)
})
future.Catch(func(e error) {
fmt.Println("4. Catch branch", e)
close(done)
})
scheduler.Now() <- future
<-done
// Output:
// 1. foo
// 2. foo foo
// 3. foo bar
// 4. Catch branch CallError: custom panic function
}
func wait(c chan any, n int) {
for i := 0; i < n; i++ {
<-c
}
}
func ExampleCron() {
var scheduler = xysched.NewScheduler("")
// Example 1: Cron is a future which runs function periodically. By default,
// it runs secondly forever.
var done = make(chan any)
var future = xysched.NewCron(func(a ...any) {
fmt.Println(a...)
done <- nil
}, "1.", "foo", "bar")
scheduler.Now() <- future
wait(done, 2)
scheduler.Stop()
scheduler = xysched.NewScheduler("")
defer scheduler.Stop()
// Example 2: It can modify periodic duration and the maximum times the
// function could run.
done = make(chan any)
future = xysched.NewCron(func() {
fmt.Println("2. bar bar")
done <- nil
}).Every(time.Millisecond).Twice()
scheduler.Now() <- future
wait(done, 2)
// Example 3: Callback, Then, Catch can also be used on cron.
done = make(chan any)
future = xysched.NewCron(fmt.Println, "3.", "foobar").Times(3)
future.Every(time.Millisecond)
future.Callback(func() { done <- nil })
scheduler.Now() <- future
wait(done, 3)
// Example 4: Finish adds a callback future which will be run when cron ran
// out of times.
done = make(chan any)
future = xysched.NewCron(fmt.Println, "4.", "foobar").Twice()
future.Every(time.Millisecond)
future.Finish(func() { close(done) })
scheduler.Now() <- future
wait(done, 1)
// Output:
// 1. foo bar
// 1. foo bar
// 2. bar bar
// 2. bar bar
// 3. foobar
// 3. foobar
// 3. foobar
// 4. foobar
// 4. foobar
}