-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathline_test.go
241 lines (224 loc) · 4.61 KB
/
line_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
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
package pipeline
import (
"context"
"testing"
)
var testJSONConf = `
[
{
"ref_handler_id":"by_square",
"timeout":20,
"required":true
},
[
{
"ref_handler_id":"by_square",
"timeout":20,
"required":true
},
{
"ref_handler_id":"by_cubic",
"timeout":20,
"required":true
}
]
]
`
var testFailedJSONConf = `
[
{
"ref_handler_id":"by_square",
"timeout":20,
"required":true
},
[
{
"ref_handler_id":"by_square",
"timeout":20,
"required":true
},
{
"desc": "slow",
"ref_handler_id":"delay_1000",
"timeout":200,
"required":true
}
]
]
`
func TestNewLineByJSON(t *testing.T) {
tt := []struct {
caseName string
jsonConf string
pipeLen int
hasErr bool
}{
{
caseName: "not a json array",
jsonConf: `{"ref_handler_id":"by_square","timeout":1000,"required":true}`,
hasErr: true,
},
{
caseName: "parallel conf contains a array",
jsonConf: `[[{"ref_handler_id":"by_square","timeout":1000,"required":true},[{"ref_handler_id":"by_square","timeout":1000,"required":true}]]]`,
hasErr: true,
},
{
caseName: "single pipe conf timeout type wrong",
jsonConf: `[{"ref_handler_id":"by_square","timeout":"1000","required":true}]`,
hasErr: true,
},
{
caseName: "single pipe conf ref_handler_id not found",
jsonConf: `[{"ref_handler_id":"not_found","timeout":1000,"required":true}]`,
hasErr: true,
},
{
caseName: "parallel pipe conf wrong",
jsonConf: `[[{"ref_handler_id":"by_square","timeout":-1,"required":true}]]`,
hasErr: true,
},
{
caseName: "normal",
jsonConf: testJSONConf,
pipeLen: 2,
hasErr: false,
},
}
for _, item := range tt {
t.Run(item.caseName, func(t *testing.T) {
line, err := NewLineByJSON(item.jsonConf, exampleHandlerBuilderGetter, exampleHandlerGetter)
if item.hasErr {
if err == nil {
t.Error("err is nil")
} else {
t.Log(err)
}
return
}
if len(line.Pipes) != item.pipeLen {
t.Errorf("pipes len: want=%v, got=%v", item.pipeLen, len(line.Pipes))
}
})
}
}
func TestLine_Handle(t *testing.T) {
tt := []struct {
caseName string
conf string
res HandleRes
err error
}{
{
caseName: "normal",
conf: testJSONConf,
res: HandleRes{
Status: HandleStatusOK,
Data: []int{16, 64},
},
err: nil,
},
{
caseName: "normal",
conf: testFailedJSONConf,
res: HandleRes{
Status: HandleStatusTimeout,
},
err: MakeErrHandleTimeout("slow", 200),
},
}
for _, item := range tt {
t.Run(item.caseName, func(t *testing.T) {
line, err := NewLineByJSON(item.conf, exampleHandlerBuilderGetter, exampleHandlerGetter)
if err != nil {
t.Fatal(err)
}
reqRes := &HandleRes{
Data: float64(2),
}
res, err := line.Handle(context.Background(), reqRes)
if item.err != nil {
if item.err == err {
t.Log(err)
}
return
}
if err != nil {
t.Fatal(err)
}
if text, ok := diff(item.res, res); !ok {
t.Error("err diff:\n", text)
}
})
}
}
func TestLine_HandleVerbosely(t *testing.T) {
tt := []struct {
caseName string
conf string
reses []HandleRes
hasErr bool
}{
{
caseName: "normal",
conf: testJSONConf,
reses: []HandleRes{
{
Status: HandleStatusOK,
Data: 4,
},
{
Status: HandleStatusOK,
Data: []int{16, 64},
},
},
hasErr: false,
},
{
caseName: "normal",
conf: testFailedJSONConf,
reses: []HandleRes{
{
Status: HandleStatusOK,
Data: 4,
},
{
Status: HandleStatusFailed,
Data: []interface{}{16, nil},
},
},
hasErr: true,
},
}
for _, item := range tt {
t.Run(item.caseName, func(t *testing.T) {
line, err := NewLineByJSON(item.conf, exampleHandlerBuilderGetter, exampleHandlerGetter)
if err != nil {
t.Fatal(err)
}
reqRes := &HandleRes{
Data: float64(2),
}
reses, err := line.HandleVerbosely(context.Background(), reqRes)
if item.hasErr {
if err == nil {
t.Error("err is nil")
} else {
t.Log(err)
}
}
if text, ok := diff(item.reses, reses); !ok {
t.Error("err diff:\n", text)
}
})
}
}
func BenchmarkLine_Handle(b *testing.B) {
line, err := NewLineByJSON(testJSONConf, exampleHandlerBuilderGetter, exampleHandlerGetter)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
line.Handle(context.Background(), &HandleRes{Data: float64(2)})
}
}