forked from ShiningRush/fastflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastflow_test.go
191 lines (183 loc) · 4.47 KB
/
fastflow_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
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
package fastflow
import (
"fmt"
"testing"
"time"
"github.com/mingmingshiliyu/fastflow/pkg/entity"
"github.com/mingmingshiliyu/fastflow/pkg/mod"
"github.com/mingmingshiliyu/fastflow/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func Test_checkOption(t *testing.T) {
tests := []struct {
giveOpt *InitialOption
wantErr error
wantOpt *InitialOption
}{
{
giveOpt: &InitialOption{
Keeper: &mod.MockKeeper{},
Store: &mod.MockStore{},
},
wantOpt: &InitialOption{
Keeper: &mod.MockKeeper{},
Store: &mod.MockStore{},
ParserWorkersCnt: 100,
ExecutorWorkerCnt: 1000,
ExecutorTimeout: time.Second * 30,
DagScheduleTimeout: time.Second * 15,
},
},
{
giveOpt: &InitialOption{},
wantOpt: &InitialOption{},
wantErr: fmt.Errorf("keeper cannot be nil"),
},
{
giveOpt: &InitialOption{
Keeper: &mod.MockKeeper{},
},
wantOpt: &InitialOption{
Keeper: &mod.MockKeeper{},
},
wantErr: fmt.Errorf("store cannot be nil"),
},
}
for _, tc := range tests {
err := checkOption(tc.giveOpt)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantOpt, tc.giveOpt)
}
}
func Test_readDagFromDir(t *testing.T) {
tests := []struct {
caseDesc string
givePaths []string
givePathsErr error
givePathDagMap map[string][]byte
calledEnsured []bool
giveDir string
wantDag *entity.Dag
wantErr error
}{
{
caseDesc: "read path failed",
givePathsErr: fmt.Errorf("read failed"),
wantErr: fmt.Errorf("read failed"),
},
{
caseDesc: "get dag failed",
givePaths: []string{"dag1", "dag2"},
givePathDagMap: map[string][]byte{
"dag1": {},
},
wantErr: fmt.Errorf("read dag2 failed: %w", fmt.Errorf("not found")),
calledEnsured: []bool{true},
},
{
caseDesc: "unmarshal dag failed",
givePaths: []string{"dag1"},
givePathDagMap: map[string][]byte{
"dag1": []byte(`tasks: 123`),
},
wantErr: fmt.Errorf("unmarshal dag1 failed: %w", &yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!int `123` into []entity.Task"}}),
},
{
caseDesc: "normal",
givePaths: []string{"dag1"},
givePathDagMap: map[string][]byte{
"dag1": []byte(`
id: test-dag
name: dag-name
desc: "desc"
tasks:
- id: "task-1"
actionName: "action"
params:
p1: p1value
`),
},
calledEnsured: []bool{true},
wantDag: &entity.Dag{
BaseInfo: entity.BaseInfo{
ID: "test-dag",
},
Name: "dag-name",
Desc: "desc",
Cron: "",
Tasks: []entity.Task{
{
ID: "task-1",
ActionName: "action",
Params: map[string]interface{}{
"p1": "p1value",
},
},
},
Status: entity.DagStatusNormal,
},
},
{
caseDesc: "no id",
givePaths: []string{"/test/filename.yaml"},
givePathDagMap: map[string][]byte{
"/test/filename.yaml": []byte(``),
},
calledEnsured: []bool{true},
wantDag: &entity.Dag{
BaseInfo: entity.BaseInfo{
ID: "filename",
},
Status: entity.DagStatusNormal,
},
},
{
caseDesc: "no id(yml)",
givePaths: []string{"c:/test/dag2.yaml"},
givePathDagMap: map[string][]byte{
"c:/test/dag2.yaml": []byte(``),
},
calledEnsured: []bool{true},
wantDag: &entity.Dag{
BaseInfo: entity.BaseInfo{
ID: "dag2",
},
Status: entity.DagStatusNormal,
},
},
}
for _, tc := range tests {
t.Run(tc.caseDesc, func(t *testing.T) {
mReader := &utils.MockDagReader{}
mReader.On("ReadPathsFromDir", mock.Anything).Run(func(args mock.Arguments) {
assert.Equal(t, tc.giveDir, args.Get(0))
}).Return(tc.givePaths, tc.givePathsErr)
mReader.On("ReadDag", mock.Anything).Run(func(args mock.Arguments) {
}).Return(func(path string) []byte {
return tc.givePathDagMap[path]
}, func(path string) error {
_, ok := tc.givePathDagMap[path]
if !ok {
return fmt.Errorf("not found")
}
return nil
})
utils.DefaultReader = mReader
var called []bool
mStore := &mod.MockStore{}
existedDag := &entity.Dag{}
mStore.On("GetDag", mock.Anything).Return(existedDag, nil)
mStore.On("UpdateDag", mock.Anything).Run(func(args mock.Arguments) {
called = append(called, true)
if tc.wantDag != nil {
assert.Equal(t, tc.wantDag, args.Get(0))
}
}).Return(nil, nil)
mod.SetStore(mStore)
err := readDagFromDir(tc.giveDir)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.calledEnsured, called)
})
}
}