forked from learning-go-book-2e/simplewebapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProcessor_test.go
More file actions
300 lines (260 loc) · 7.01 KB
/
Copy pathDataProcessor_test.go
File metadata and controls
300 lines (260 loc) · 7.01 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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
)
func testInput(id string, op string, val1 int, val2 int) []byte {
return []byte(fmt.Sprintf("%s\n%s\n%d\n%d", id, op, val1, val2))
}
func Test_parser(t *testing.T) {
type TestRun struct {
name string
bytes []byte
expected Input
err error
}
data := make([]TestRun, 0, 6)
data = append(data, TestRun{name: "sum", bytes: testInput("sum", "+", 1, 2), expected: Input{"sum", "+", 1, 2}, err: nil})
data = append(data, TestRun{name: "sub", bytes: testInput("sub", "-", 3, 2), expected: Input{"sub", "-", 3, 2}, err: nil})
data = append(data, TestRun{name: "mult", bytes: testInput("mult", "*", 2, 5), expected: Input{"mult", "*", 2, 5}, err: nil})
data = append(data, TestRun{name: "div", bytes: testInput("div", "/", 4, 3), expected: Input{"div", "/", 4, 3}, err: nil})
// error cases
data = append(data, TestRun{name: "invalid val1", bytes: []byte(fmt.Sprintf("%s\n%s\n%s\n%d\n", "div", "/", "?", 1)), expected: Input{}, err: strconv.ErrSyntax})
data = append(data, TestRun{name: "invalid val2", bytes: []byte(fmt.Sprintf("%s\n%s\n%d\n%s\n", "div", "/", 1, ".")), expected: Input{}, err: strconv.ErrSyntax})
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
result, err := parser(d.bytes)
if diff := cmp.Diff(result, d.expected); diff != "" {
t.Error(diff)
}
if err != nil {
if !errors.Is(err, strconv.ErrSyntax) {
t.Error("unexpected error returned")
}
}
})
}
}
func Fuzz_parser(f *testing.F) {
testcases := make([][]byte, 0, 6)
testcases = append(testcases, testInput("sum", "+", 1, 2))
testcases = append(testcases, testInput("sub", "-", 3, 2))
testcases = append(testcases, testInput("mult", "*", 1, 2))
testcases = append(testcases, testInput("div", "/", 1, 2))
testcases = append(testcases, []byte(fmt.Sprintf("%s\n%s\n%s\n%d\n", "div", "/", "?", 1)))
testcases = append(testcases, []byte(fmt.Sprintf("%s\n%s\n%d\n%s\n", "div", "/", 1, ".")))
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, in []byte) {
_, err := parser(in)
if err != nil {
if !errors.Is(err, strconv.ErrSyntax) && !errors.Is(err, MissingInputError{}) {
t.Errorf("unexpected error returned: %v", err)
}
}
})
}
func Test_DataProcessor(t *testing.T) {
data := []struct {
name string
inputs [][]byte
expected []Result
}{
{
name: "valid operations",
inputs: [][]byte{
[]byte("sum\n+\n1\n2\n"),
[]byte("sub\n-\n3\n2\n"),
[]byte("mult\n*\n2\n5\n"),
[]byte("div\n/\n4\n3\n"),
},
expected: []Result{
{Id: "sum", Value: 3},
{Id: "sub", Value: 1},
{Id: "mult", Value: 10},
{Id: "div", Value: 1},
},
},
{
name: "invalid operations",
inputs: [][]byte{
[]byte("invalid num1\n+\n?\n2\n"),
[]byte("invalid num2\n+\n1\n?\n"),
[]byte("invalid op\n!\n1\n2\n"),
[]byte("div by 0\n/\n4\n0\n"),
},
expected: []Result{},
},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
in := make(chan []byte, len(d.inputs))
out := make(chan Result, len(d.inputs))
for _, input := range d.inputs {
in <- input
}
close(in)
go DataProcessor(in, out)
var got []Result
for res := range out {
got = append(got, res)
}
if len(got) != len(d.expected) {
t.Errorf("DataProcessor() got %v results, expected %v", len(got), len(d.expected))
}
for i, result := range got {
if diff := cmp.Diff(result, d.expected[i]); diff != "" {
t.Errorf("DataProcessor() result %v, expected %v", result, d.expected[i])
}
}
})
}
}
func Test_WriteData(t *testing.T) {
data := []struct {
name string
inputs []Result
expected []string
}{
{
name: "correct format",
inputs: []Result{
{Id: "sum", Value: 3},
{Id: "sub", Value: 1},
},
expected: []string{
"sum:3\n",
"sub:1\n",
},
},
}
var l sync.Mutex
var wg sync.WaitGroup
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
fmt.Println("in Write Data")
in := make(chan Result)
w := bytes.NewBuffer(nil)
wg.Add(1)
go func() {
defer wg.Done()
WriteData(in, w, &l)
}()
wg.Add(1)
go func() {
defer wg.Done()
for _, input := range d.inputs {
in <- input
}
close(in)
}()
wg.Wait()
out := make([]byte, 6)
count := 0
for {
_, err := w.Read(out)
if err != nil {
if count == len(d.expected) && err == io.EOF {
fmt.Println("reached EOF of writer after all inputs were read in")
break
}
t.Fatalf("unable to read data after call to WriteData(): %v", err)
}
s := string(out)
if s != d.expected[count] {
t.Errorf("WriteData result %v, expected %v", s, d.expected[count])
}
count++
}
})
}
}
type BadRequestBodyStub struct{}
func (brbs BadRequestBodyStub) Read(p []byte) (n int, err error) {
return 0, errors.New("test error")
}
func Test_NewController(t *testing.T) {
var wg sync.WaitGroup
wg.Add(5)
out := make(chan []byte, 1)
handler := NewController(out)
data := []struct {
name string
input io.Reader
expectedStatusCode int
expectedBody string
channelBusy bool
}{
{
name: "successful write to channel (1)",
input: strings.NewReader("sum\n+\n1\n2\n"),
expectedStatusCode: http.StatusAccepted,
expectedBody: "OK: 1",
channelBusy: false,
},
{
name: "successful write to channel (2)",
input: strings.NewReader("sum\n+\n1\n2\n"),
expectedStatusCode: http.StatusAccepted,
expectedBody: "OK: 2",
channelBusy: false,
},
{
name: "channel busy",
input: strings.NewReader("sum\n+\n1\n2\n"),
expectedStatusCode: http.StatusServiceUnavailable,
expectedBody: "Too Busy: 1",
channelBusy: true,
},
{
name: "bad request",
input: BadRequestBodyStub{},
expectedStatusCode: http.StatusBadRequest,
expectedBody: "Bad Input",
channelBusy: false,
},
}
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
defer wg.Done()
// fill the channel to simulate a busy state
if d.channelBusy {
fmt.Println("simulating busy channel")
go func() {
defer wg.Done()
out <- []byte("sum\n+\n1\n2\n")
}()
fmt.Println("after channel send")
} else {
// ensure the channel is empty
select {
case <-out:
default:
}
}
req := httptest.NewRequest("POST", "/", d.input)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != d.expectedStatusCode {
t.Errorf("NewController returned wrong status code: got %v want %v", rr.Code, d.expectedStatusCode)
}
if body := rr.Body.String(); body != d.expectedBody {
t.Errorf("NewController returned unexpected body: got %v, want %v", body, d.expectedBody)
}
})
}
go func() {
wg.Wait()
close(out)
}()
}