-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_test.go
More file actions
305 lines (267 loc) · 8.6 KB
/
callback_test.go
File metadata and controls
305 lines (267 loc) · 8.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package svcinit
import (
"cmp"
"context"
"errors"
"fmt"
"slices"
"sync"
"sync/atomic"
"testing"
"testing/synctest"
"time"
"gotest.tools/v3/assert"
cmp2 "gotest.tools/v3/assert/cmp"
)
func TestCallback(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var runStarted, runStopped atomic.Int32
testcb := &testCallback{}
testtaskcb := &testCallback{}
testruncb := &testCallback{}
errCancelTask1 := errors.New("test context cancel 1")
errCancelTask2 := errors.New("test context cancel 2")
sinit, err := New(
// WithLogger(defaultLogger(t.Output())),
WithStages("s1", "s2"),
WithManagerCallback(ManagerCallbackFunc(func(ctx context.Context, stage string, step Step, callbackStep CallbackStep) {
switch step {
case StepStart:
runStarted.Add(1)
case StepStop:
runStopped.Add(1)
default:
}
})),
WithTaskCallback(TaskCallbackFunc(func(ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
assertTestTask(t, task, stage, step, callbackStep)
})),
WithTaskCallback(testcb),
)
assert.NilError(t, err)
sinit.
AddTask("s1", newTestTask(1, BuildTask(
WithStart(func(ctx context.Context) (err error) {
testruncb.add(1, "s1", StepStart, CallbackStepBefore, nil)
defer func() {
testruncb.add(1, "s1", StepStart, CallbackStepAfter, err)
}()
return sleepContext(ctx, 2*10*time.Second,
withSleepContextError(true))
}),
WithStop(func(ctx context.Context) (err error) {
testruncb.add(1, "s1", StepStop, CallbackStepBefore, nil)
defer func() {
testruncb.add(1, "s1", StepStop, CallbackStepAfter, err)
}()
ssm := StartStepManagerFromContext(ctx)
// fmt.Printf("t1 stop ctx: (cancancel:%t)(canfinished:%t)\n",
// ssm.CanContextCancel(), ssm.CanFinished())
ssm.ContextCancel(errCancelTask1)
select {
case <-ctx.Done():
case <-ssm.Finished():
}
return nil
}),
)),
// WithCancelContext(),
WithStartStepManager(),
WithCallback(testtaskcb))
sinit.AddTask("s2", newTestTask(2, BuildTask(
WithStart(func(ctx context.Context) (err error) {
testruncb.add(2, "s2", StepStart, CallbackStepBefore, nil)
defer func() {
testruncb.add(2, "s2", StepStart, CallbackStepAfter, err)
}()
return sleepContext(ctx, 10*time.Second, withSleepContextError(true)) // 10 seconds, will stop before task 1 which is 20 seconds
}),
WithStop(func(ctx context.Context) (err error) {
testruncb.add(2, "s2", StepStop, CallbackStepBefore, nil)
defer func() {
testruncb.add(2, "s2", StepStop, CallbackStepAfter, err)
}()
ssm := StartStepManagerFromContext(ctx)
// fmt.Printf("t2 stop ctx: (cancancel:%t)(canfinished:%t)\n",
// ssm.CanContextCancel(), ssm.CanFinished())
ssm.ContextCancel(errCancelTask2)
select {
case <-ctx.Done():
case <-ssm.Finished():
}
return nil
}),
)),
// WithCancelContext(),
WithStartStepManager(),
WithCallback(testtaskcb))
err = sinit.Run(t.Context())
assert.NilError(t, err)
expectedAll := []testCallbackItem{
{1, "s1", StepStart, CallbackStepBefore, nil},
{1, "s1", StepStop, CallbackStepBefore, nil},
{1, "s1", StepStart, CallbackStepAfter, errCancelTask1},
{1, "s1", StepStop, CallbackStepAfter, nil},
{2, "s2", StepStart, CallbackStepBefore, nil},
{2, "s2", StepStart, CallbackStepAfter, nil},
{2, "s2", StepStop, CallbackStepBefore, nil},
{2, "s2", StepStop, CallbackStepAfter, nil},
}
assert.Equal(t, int32(4), runStarted.Load())
assert.Equal(t, int32(4), runStopped.Load())
testcb.assertExpectedNotExpected(t, expectedAll, nil)
testtaskcb.assertExpectedNotExpected(t, expectedAll, nil)
testruncb.assertExpectedNotExpected(t, expectedAll, nil)
})
}
type testCallbackItem struct {
taskNo int
stage string
step Step
callbackStep CallbackStep
err error
}
func (t testCallbackItem) Equal(other testCallbackItem) bool {
if t.taskNo != other.taskNo || t.stage != other.stage || t.step != other.step || t.callbackStep != other.callbackStep {
return false
}
if t.err == nil && other.err == nil {
return true
}
return errors.Is(other.err, t.err)
}
func testCallbackItemCompare(a, b testCallbackItem) int {
if c := cmp.Compare(a.taskNo, b.taskNo); c != 0 {
return c
}
if c := cmp.Compare(a.stage, b.stage); c != 0 {
return c
}
if c := cmp.Compare(a.step, b.step); c != 0 {
return c
}
if c := cmp.Compare(a.callbackStep, b.callbackStep); c != 0 {
return c
}
return 0
}
type testStageStep struct {
Stage string
Step Step
}
func testStageStepCompare(a, b testStageStep) int {
if c := cmp.Compare(a.Stage, b.Stage); c != 0 {
return c
}
if c := cmp.Compare(a.Step, b.Step); c != 0 {
return c
}
return 0
}
type testCallback struct {
filterCallbackStep *CallbackStep
m sync.Mutex
items []testCallbackItem
}
func (t *testCallback) add(taskNo int, stage string, step Step, callbackStep CallbackStep, err error) {
if t.filterCallbackStep != nil && *t.filterCallbackStep != callbackStep {
return
}
if taskNo < 0 {
taskNo = 0
}
t.m.Lock()
defer t.m.Unlock()
item := testCallbackItem{
taskNo: taskNo,
stage: stage,
step: step,
callbackStep: callbackStep,
err: err,
}
t.items = append(t.items, item)
}
func (t *testCallback) containsAll(testTasks []testCallbackItem) []testCallbackItem {
t.m.Lock()
defer t.m.Unlock()
var ret []testCallbackItem
for _, task := range testTasks {
if !slices.ContainsFunc(t.items, func(item testCallbackItem) bool {
return item.Equal(task)
}) {
ret = append(ret, task)
}
}
return ret
}
func (t *testCallback) assertExpectedNotExpected(tt *testing.T, expected, notExpected []testCallbackItem) {
_ = assert.Check(tt, cmp2.DeepEqual([]testCallbackItem(nil), t.containsAll(expected)), "failed expected test")
_ = assert.Check(tt, cmp2.DeepEqual(notExpected, t.containsAll(notExpected)), "failed not expected test")
}
func (t *testCallback) Callback(_ context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
taskNo, _ := getTestTaskNo(task)
t.add(taskNo, stage, step, callbackStep, err)
}
type testManagerCallback struct {
filterCallbackStep *CallbackStep
m sync.Mutex
steps []testStageStep
}
func (t *testManagerCallback) add(stage string, step Step, callbackStep CallbackStep, err error) {
if t.filterCallbackStep != nil && *t.filterCallbackStep != callbackStep {
return
}
t.m.Lock()
defer t.m.Unlock()
stageStep := testStageStep{
Stage: stage,
Step: step,
}
if !slices.ContainsFunc(t.steps, func(step testStageStep) bool {
return testStageStepCompare(step, stageStep) == 0
}) {
t.steps = append(t.steps, stageStep)
}
}
func (t *testManagerCallback) assertStageSteps(tt *testing.T, expected []testStageStep) {
t.m.Lock()
defer t.m.Unlock()
assert.DeepEqual(tt, expected, t.steps)
}
func (t *testManagerCallback) Callback(ctx context.Context, stage string, step Step, callbackStep CallbackStep) {
cause, _ := CauseFromContext(ctx)
t.add(stage, step, callbackStep, cause)
}
func printTaskCallback(startTime time.Time, ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
taskNo, _ := getTestTaskNo(task)
fmt.Printf("[%s] --- [TASK %d] (stage:%s)(step:%s)(callbackStep:%s)(err:%v)\n", time.Since(startTime), // time.Now().Format("15:04:05.000000000"),
taskNo, stage, step, callbackStep, err)
}
func printManagerCallback(startTime time.Time, ctx context.Context, stage string, step Step, callbackStep CallbackStep) {
cause, _ := CauseFromContext(ctx)
fmt.Printf("[%s] $$$ [MANAGER] (stage:%s)(step:%s)(callbackStep:%s)(err:%v)\n", time.Since(startTime), // time.Now().Format("15:04:05.000000000"),
stage, step, callbackStep, cause)
}
func testTaskCallbackPrint() TaskCallback {
startTime := time.Now()
var m sync.Mutex
return TaskCallbackFunc(func(ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
m.Lock()
defer m.Unlock()
printTaskCallback(startTime, ctx, task, stage, step, callbackStep, err)
})
}
func testCallbacksPrint() (ManagerCallback, TaskCallback) {
startTime := time.Now()
var m sync.Mutex
return ManagerCallbackFunc(func(ctx context.Context, stage string, step Step, callbackStep CallbackStep) {
m.Lock()
defer m.Unlock()
printManagerCallback(startTime, ctx, stage, step, callbackStep)
}),
TaskCallbackFunc(func(ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
m.Lock()
defer m.Unlock()
printTaskCallback(startTime, ctx, task, stage, step, callbackStep, err)
})
}