-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtask_test.go
More file actions
222 lines (202 loc) · 4.1 KB
/
Copy pathtask_test.go
File metadata and controls
222 lines (202 loc) · 4.1 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package task
import (
"context"
"errors"
"io"
"sync/atomic"
"testing"
"time"
)
// Use RootManager as root task manager
type TaskManager = RootManager[uint32, ManagerItem[uint32]]
// Create root task manager
var root TaskManager
func init() {
root.Init()
}
func Test_AddTask_AddsTaskSuccessfully(t *testing.T) {
var task Task
root.AddTask(&task)
_ = task.WaitStarted()
root.RangeSubTask(func(t ITask) bool {
if t.GetTaskID() == task.GetTaskID() {
return false
}
return true
})
}
type retryDemoTask struct {
Task
}
func (task *retryDemoTask) Start() error {
return io.ErrClosedPipe
}
func Test_RetryTask(t *testing.T) {
var demoTask retryDemoTask
var parent Job
root.AddTask(&parent)
demoTask.SetRetry(3, time.Second)
parent.AddTask(&demoTask)
_ = parent.WaitStopped()
if demoTask.retry.RetryCount != 3 {
t.Errorf("expected 3 retries, got %d", demoTask.retry.RetryCount)
}
}
func Test_Call_ExecutesCallback(t *testing.T) {
called := false
root.Call(func() {
called = true
return
})
if !called {
t.Errorf("expected callback to be called")
}
}
func Test_StopByContext(t *testing.T) {
var task Task
ctx, cancel := context.WithCancel(context.Background())
root.AddTask(&task, ctx)
time.AfterFunc(time.Millisecond*100, cancel)
if !errors.Is(task.WaitStopped(), context.Canceled) {
t.Errorf("expected task to be stopped by context")
}
}
func Test_ParentStop(t *testing.T) {
var parent Job
root.AddTask(&parent)
var called atomic.Uint32
var task Task
checkCalled := func(expected uint32) {
if count := called.Add(1); count != expected {
t.Errorf("expected %d, got %d", expected, count)
}
}
task.OnDispose(func() {
checkCalled(1)
})
parent.OnDispose(func() {
checkCalled(2)
})
parent.AddTask(&task)
parent.Stop(ErrAutoStop)
if !errors.Is(task.WaitStopped(), ErrAutoStop) {
t.Errorf("expected task auto stop")
}
}
func Test_ParentAutoStop(t *testing.T) {
var parent Job
root.AddTask(&parent)
var called atomic.Uint32
var task Task
checkCalled := func(expected uint32) {
if count := called.Add(1); count != expected {
t.Errorf("expected %d, got %d", expected, count)
}
}
task.OnDispose(func() {
checkCalled(1)
})
parent.OnDispose(func() {
checkCalled(2)
})
parent.AddTask(&task)
time.AfterFunc(time.Second, func() {
task.Stop(ErrTaskComplete)
})
if !errors.Is(parent.WaitStopped(), ErrAutoStop) {
t.Errorf("expected task auto stop")
}
}
func Test_Hooks(t *testing.T) {
var called atomic.Uint32
var task Task
checkCalled := func(expected uint32) {
if count := called.Add(1); count != expected {
t.Errorf("expected %d, got %d", expected, count)
}
}
task.OnStart(func() {
checkCalled(1)
})
task.OnDispose(func() {
checkCalled(3)
})
task.OnStart(func() {
checkCalled(2)
})
task.OnDispose(func() {
checkCalled(4)
})
task.Stop(ErrTaskComplete)
root.AddTask(&task).WaitStopped()
}
type startFailTask struct {
Task
}
func (task *startFailTask) Start() error {
return errors.New("start failed")
}
func (task *startFailTask) Dispose() {
task.Logger.Info("Dispose")
}
func Test_StartFail(t *testing.T) {
var task startFailTask
root.AddTask(&task)
if err := task.WaitStarted(); err == nil {
t.Errorf("expected start to fail")
}
}
func Test_Block(t *testing.T) {
var task Task
block := make(chan struct{})
var job Job
task.OnStart(func() {
task.OnStop(func() {
close(block)
})
<-block
})
time.AfterFunc(time.Second*2, func() {
job.Stop(ErrTaskComplete)
})
root.AddTask(&job)
job.AddTask(&task)
job.WaitStopped()
}
//
//type DemoTask struct {
// Task
// file *os.File
// filePath string
//}
//
//func (d *DemoTask) Start() (err error) {
// d.file, err = os.Open(d.filePath)
// return
//}
//
//func (d *DemoTask) Run() (err error) {
// _, err = d.file.Write([]byte("hello"))
// return
//}
//
//func (d *DemoTask) Dispose() {
// d.file.Close()
//}
//
//type HelloWorld struct {
// DemoTask
//}
//
//func (h *HelloWorld) Run() (err error) {
// _, err = h.file.Write([]byte("world"))
// return nil
//}
//type HelloWorld struct {
// Task
//}
//
//func (h *HelloWorld) Start() (err error) {
// fmt.Println("Hello World")
// return nil
//}