-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_test.go
127 lines (117 loc) · 2.63 KB
/
entry_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
// Copyright (c) 2018, dmc ([email protected]),
//
// Authors: dmc,
//
// Distribution:.
package gotask
import (
"fmt"
"testing"
"time"
)
func TestTask(t *testing.T) {
var p []int
tk, _ := NewTask(time.Second, func() {
p = append(p, 1)
})
AddToTaskList(tk)
select {
case <-time.After(time.Second*1 + time.Millisecond*100):
if len(p) != 1 {
t.Errorf("TestTask() fail , need len : %d , actually len : %d", 1, len(p))
}
return
}
}
func TestDailyTask(t *testing.T) {
tks, _ := NewDailyTasks([]string{"00:00:00", "12:00:00", "08:00:00"}, func() {
})
AddToTaskList(tks...)
for _, tk := range tks {
fmt.Println(tk.ExecuteTime())
fmt.Println(tk.ID())
tk.refreshExecuteTime()
if tk.ExecuteTime().Day()-time.Now().Day() < 1 {
t.Errorf("RefreshExecuteTime() appears error")
}
tk.do()
}
}
func init() {
tasks = &taskList{}
go doAllTask()
}
func TestChangeInterval(t *testing.T) {
var p []int
tk, _ := NewTask(time.Second*100, func() {
p = append(p, 1)
})
AddToTaskList(tk)
ChangeInterval(tk.ID(), time.Millisecond*200)
tc := time.After(time.Second)
select {
case <-tc:
if len(p) == 0 {
t.Errorf("TestTask() fail , need len : 1, actually len : %d", len(p))
}
return
}
}
func TestChangeInterval2(t *testing.T) {
var p []int
tk, _ := NewDailyTask("20:20:20", func() {
p = append(p, 1)
})
AddToTaskList(tk)
err := ChangeInterval(tk.ID(), time.Millisecond*200)
if err != ErrTaskTypeInValid {
t.Errorf("ChangeInterval2 expect get err: %v, actually: %v", ErrTaskTypeInValid, err)
}
}
func TestMonthlyTask(t *testing.T) {
var tks []*Task
tks, _ = NewMonthlyTasks([]string{"1 00:00:00", "1 1:00:00", "3 08:00:00"}, func() {
fmt.Println(time.Now())
})
for _, tk := range tks {
tk.refreshExecuteTime()
a := int(time.Now().Month()) - int(tk.ExecuteTime().Month())
if a > -2 {
if a > 10 {
t.Errorf("RefreshExecuteTime() appears error")
}
}
tk.do()
}
time.Sleep(time.Second)
}
func TestPauseAndResume(t *testing.T) {
var p []int
tk, _ := NewTask(time.Millisecond*90, func() {
p = append(p, 1)
})
AddToTaskList(tk)
Pause(tk.ID())
time.Sleep(time.Millisecond * 300)
if len(p) != 0 {
t.Errorf("TestTask() fail , need len : 0, actually len : %d", len(p))
}
Resume(tk.ID())
time.Sleep(time.Millisecond * 500)
if len(p) == 0 {
t.Errorf("TestTask() fail , need len : !=0, actually len : %d", len(p))
}
}
func TestRemove(t *testing.T) {
var p []int
tk, _ := NewTask(time.Millisecond*90, func() {
p = append(p, 1)
})
AddToTaskList(tk)
time.Sleep(time.Millisecond * 100)
Remove(tk.ID())
time.Sleep(time.Millisecond * 100)
if tasks.get(tk.ID()) != nil {
t.Errorf("Remove() failed")
}
}