-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_test.go
More file actions
125 lines (102 loc) · 2.85 KB
/
wrap_test.go
File metadata and controls
125 lines (102 loc) · 2.85 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
package svcinit
import (
"cmp"
"context"
"testing"
"testing/synctest"
cmp2 "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"gotest.tools/v3/assert"
)
func TestWrapTask(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
started := &testList[string]{}
stopped := &testList[string]{}
sm, err := New(
WithTaskCallback(TaskCallbackFunc(func(ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
_, ok := getTestTaskNo(task)
assert.Check(t, !ok)
tn, ok := getTestTaskNo(UnwrapTask(task))
assert.Check(t, ok)
assert.Check(t, cmp2.Equal(3, tn))
})),
)
assert.NilError(t, err)
task1 := newTestTask(3, BuildTask(
WithStart(func(ctx context.Context) error {
started.add("task3")
return nil
}),
WithStop(func(ctx context.Context) error {
stopped.add("task3")
return nil
}),
))
sm.AddTask(StageDefault, WrapTask(task1))
err = sm.Run(t.Context())
assert.NilError(t, err)
assert.DeepEqual(t, []string{"task3"}, started.get(), cmpopts.SortSlices(cmp.Less[string]))
assert.DeepEqual(t, []string{"task3"}, stopped.get(), cmpopts.SortSlices(cmp.Less[string]))
})
}
func TestWrapTaskImplements(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
started := &testList[string]{}
sm, err := New(
WithTaskCallback(TaskCallbackFunc(func(ctx context.Context, task Task, stage string, step Step, callbackStep CallbackStep, err error) {
_, ok := task.(*testTaskComplete)
assert.Check(t, !ok)
_, ok = UnwrapTask(task).(*testTaskComplete)
if !assert.Check(t, ok) {
return
}
ts, ok := task.(TaskSteps)
if !assert.Check(t, ok) {
return
}
assert.Check(t, cmp2.Equal([]Step{StepStart}, ts.TaskSteps()))
to, ok := task.(TaskWithOptions)
if !assert.Check(t, ok) {
return
}
assert.Check(t, 1 == len(to.TaskOptions()))
})),
)
assert.NilError(t, err)
task1 := &testTaskComplete{
task: BuildTask(
WithStart(func(ctx context.Context) error {
started.add("task3")
return nil
}),
),
steps: []Step{
StepStart,
},
options: []TaskInstanceOption{
WithCancelContext(true),
},
}
sm.AddTask(StageDefault, WrapTask(task1))
err = sm.Run(t.Context())
assert.NilError(t, err)
assert.DeepEqual(t, []string{"task3"}, started.get(), cmpopts.SortSlices(cmp.Less[string]))
})
}
type testTaskComplete struct {
task Task
steps []Step
options []TaskInstanceOption
}
var _ Task = (*testTaskComplete)(nil)
var _ TaskSteps = (*testTaskComplete)(nil)
var _ TaskWithOptions = (*testTaskComplete)(nil)
func (t *testTaskComplete) Run(ctx context.Context, step Step) error {
return t.task.Run(ctx, step)
}
func (t *testTaskComplete) TaskSteps() []Step {
return t.steps
}
func (t *testTaskComplete) TaskOptions() []TaskInstanceOption {
return t.options
}