-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.go
53 lines (45 loc) · 1021 Bytes
/
task_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
package task
import (
"testing"
"time"
)
func createTask(t *testing.T, customTick func(*Task)) *TaskRunner {
r, err := Every(1).Second().
OnBeforeStart(func(task *Task) error {
t.Logf("task %s - elapsed %d (onBeforeStart)", task.Id(), task.Elapsed().Milliseconds())
return nil
}).
Do(func(task *Task) error {
t.Logf("task %s - elapsed %d", task.Id(), task.Elapsed().Milliseconds())
if customTick != nil {
customTick(task)
}
return nil
})
if err != nil {
t.Fatalf("failed to create runner: %v", err)
}
return r
}
func TestEvery(t *testing.T) {
r := createTask(t, nil)
time.Sleep(4 * time.Second)
if err := r.Close(); err != nil {
t.Fatalf("failed to close runner: %v", err)
}
}
func TestEvery_Stop(t *testing.T) {
i := 0
r := createTask(t, func(task *Task) {
if i == 2 {
t.Logf("stopping task %s", task.Id())
task.Stop()
return
}
i++
})
time.Sleep(5 * time.Second)
if err := r.Close(); err != nil {
t.Fatalf("failed to close runner: %v", err)
}
}