-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers_test.go
320 lines (304 loc) · 10.6 KB
/
numbers_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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"sync"
"testing"
"time"
)
// Constructs HTTP request handler with predefined response and timeout
func makeStubHandler(data string, timeout int) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(timeout) * time.Millisecond)
w.Write([]byte(data))
}
}
// ReadTimeout prevents from getting stuck while reading from clients.
// Althought it's totally fine to have thouse timeouts in production, in tests it might hide real problem.
// Yep, TestHTTP gonna stuck forever w/o this option, rarely and randomly.
// TODO: Further investigation required.
func startServer(t *testing.T, port int) *http.Server {
server := &http.Server{
Addr: "127.0.0.1:8080",
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
go func() {
if err := server.ListenAndServe(); err != nil {
t.Log(err)
}
}()
return server
}
// reflect.DeepEqual compares empty slices in a strange way
// And everybody are saying it's extremely slow :)
func numbers_cmp(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
// Wrapper for passing http requests and comparing responses with expected values
func numbers_get(t *testing.T, url string, expected_status int, expected_numbers []int) {
g := NumbersGetterHttp{}
// rough, 600 = 500 + routines overhead + transport overhead. Everything is in a single process.
ctx, _ := context.WithTimeout(context.Background(), 600*time.Millisecond)
result, status, err := g.get(ctx, url)
if ctx.Err() != nil {
t.Errorf("Request failed: %s", ctx.Err())
return
}
if err != nil && expected_status != -1 { // -1 is used when we're expecting something malformed, so let's silently accept the error
t.Error(err)
return
}
if status != expected_status {
t.Errorf("Got status %d, expected %d (%s)", status, expected_status, url)
return
}
if expected_status != -1 && !numbers_cmp(result, expected_numbers) {
t.Errorf("Got numbers %v, expected %v (%s)", result, expected_numbers, url)
return
}
}
// Process and check httptest request to /numbers endpoint
// TODO: figure out how to reuse NumbersGetterHttp.get() and numbers_get() for this purpose instead of having numbers_rr
func numbers_rr(t *testing.T, url string, handler http.HandlerFunc, expected_status int, expected_numbers []int) {
rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Error(err)
return
}
handler.ServeHTTP(rr, req)
if rr.Code != expected_status {
t.Errorf("Got status (%s): %d, expected: %d", url, rr.Code, expected_status)
return
}
if rr.Code != http.StatusOK {
return
}
body, err := ioutil.ReadAll(rr.Body)
if err != nil {
t.Error(err)
return
}
numbers := Numbers{}
err = json.Unmarshal(body, &numbers)
if err != nil {
t.Errorf("Unexpected error while parsing json from %s: %s -- %s", url, err, body)
return
}
if !reflect.DeepEqual(numbers.Numbers, expected_numbers) {
t.Errorf("Got numbers (%s): %v, expected: %v", url, numbers.Numbers, expected_numbers)
return
}
}
// NumbersGetterStub(Cfg) implements mock for fetching data provided by /test-endpoints in memory w/o network layer
type NumbersGetterStubCfg struct {
Numbers []int
Timeout time.Duration
}
func stubConfig(numbers []int, timeout int) NumbersGetterStubCfg {
return NumbersGetterStubCfg{
Numbers: numbers,
Timeout: time.Duration(timeout) * time.Millisecond,
}
}
type NumbersGetterStub struct {
Config map[string]NumbersGetterStubCfg
}
func (g NumbersGetterStub) get(ctx context.Context, url string) ([]int, int, error) {
n, ok := g.Config[url]
if ok {
time.Sleep(n.Timeout)
return n.Numbers, 200, nil
} else {
return nil, 404, nil
}
}
func TestCollectNumbers(t *testing.T) {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
cases := []struct {
Input [][]int
Result []int
}{{
Input: [][]int{},
Result: []int{},
}, {
Input: [][]int{[]int{}},
Result: []int{},
}, {
Input: [][]int{[]int{}, []int{}},
Result: []int{},
}, {
Input: [][]int{[]int{1}},
Result: []int{1},
}, {
Input: [][]int{[]int{1}, []int{1}},
Result: []int{1},
}, {
Input: [][]int{[]int{1, 3}, []int{2}},
Result: []int{1, 2, 3},
}, {
Input: [][]int{[]int{9, 1}, []int{1}, []int{5, 1, 42}},
Result: []int{1, 5, 9, 42},
}}
channel := make(chan []int, 10)
for _, c := range cases {
for _, i := range c.Input {
channel <- i
}
r := collectNumbers(context.Background(), len(c.Input), channel)
if !numbers_cmp(r, c.Result) {
t.Errorf("collect %v, got %v, expected %v", c.Input, r, c.Result)
}
}
channel <- []int{1, 2}
channel <- []int{0, 0}
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
result := collectNumbers(ctx, 999, channel)
if !numbers_cmp(result, []int{0, 1, 2}) {
t.Errorf("collect %v, got %v, expected %v", [][]int{[]int{1, 2}, []int{0, 0}}, result, []int{0, 1, 2})
}
}
type TestCase struct {
Url string
Numbers []int
Status int
}
func TestBasic(t *testing.T) {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
s := NumbersGetterStub{ // configure numbers sources (stubs)
Config: map[string]NumbersGetterStubCfg{
"/test1": stubConfig([]int{1, 2, 3, 4}, 0),
"/test2": stubConfig([]int{5, 6, 1, 1}, 100),
"/test3": stubConfig([]int{1, 2}, 300),
"/test4": stubConfig([]int{11, 12}, 400),
"/test5": stubConfig([]int{101, 102}, 450),
"/test6": stubConfig([]int{1001, 1002}, 550),
},
}
testCases := []TestCase{
{Url: "/numbers?u=/wrong", Numbers: nil, Status: 200},
{Url: "/numbers?u=/test1", Numbers: []int{1, 2, 3, 4}, Status: 200},
{Url: "/numbers?u=/test1&u=/test2", Numbers: []int{1, 2, 3, 4, 5, 6}, Status: 200},
{Url: "/numbers?u=/test3&u=/test4&u=/test5&u=/test6", Numbers: []int{1, 2, 11, 12, 101, 102}, Status: 200},
}
handler := http.HandlerFunc(makeNumbersHandler(s))
for _, test := range testCases {
numbers_rr(t, test.Url, handler, test.Status, test.Numbers)
}
}
// Send lots of concurent requests to /numbers
// This test might fail! It depends on amount of CPUs/$GOMAXPROCS.
// If it doesn't fail, just increase the `loops` :) Btw 500 seems to be stable for my laptop.
// How its fail:
// 1. /numbers cuts processing in ~500msecs.
// 2. Some of endpoints (/test5 and sometimes /test4) are still in progress.
// 3. Test expects to receive numbers from these endpoints in time, and fails.
// Likely it's an issue in test handlers but not in the /numbers implementation.
// Maybe it's even bad idea to launch so many routines?
// Imaginary solutions, tests side:
// * Control amount of routines that maintain test requests (like to have no more than 1k routines at the same time)
// * Process several requests by one routine, don't know how to achive that with blocking IO.
// Backend side:
// * Reserch advanced techniques of handling requests in Go.
func TestConcurent(t *testing.T) {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
s := NumbersGetterStub{ // configure numbers sources (stubs)
Config: map[string]NumbersGetterStubCfg{
"/test1": stubConfig([]int{1, 2, 3, 4}, 0),
"/test2": stubConfig([]int{5, 6, 1, 1}, 100),
"/test3": stubConfig([]int{1, 2}, 300),
"/test4": stubConfig([]int{11, 12}, 400),
"/test5": stubConfig([]int{101, 102}, 450),
"/test6": stubConfig([]int{1001, 1002}, 550),
},
}
testCases := []TestCase{
{Url: "/numbers?u=/wrong", Numbers: nil, Status: 200},
{Url: "/numbers?u=/test1", Numbers: []int{1, 2, 3, 4}, Status: 200},
{Url: "/numbers?u=/test1&u=/test2", Numbers: []int{1, 2, 3, 4, 5, 6}, Status: 200},
{Url: "/numbers?u=/test3&u=/test4&u=/test5&u=/test6", Numbers: []int{1, 2, 11, 12, 101, 102}, Status: 200},
}
handler := http.HandlerFunc(makeNumbersHandler(s))
loops := 500
var wg sync.WaitGroup
for i := 0; i < loops; i++ {
for _, test := range testCases {
wg.Add(1)
go func(test TestCase) {
defer wg.Done()
start := time.Now()
numbers_rr(t, test.Url, handler, test.Status, test.Numbers)
elapsed := time.Now().Sub(start)
if elapsed > 550*time.Millisecond {
t.Errorf("Requesting %s have taken %v", test.Url, elapsed)
}
}(test)
}
}
wg.Wait()
}
// No mocks anymore. Real http endpoints for /numbers and /tests-s
// This test fails in ~1% cases. Random connection to /numbers or /test gest refused.
// TODO: Further investigation required.
func TestHTTP(t *testing.T) {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
http.HandleFunc("/numbers", makeHttpNumbersHandler())
http.HandleFunc("/malformed1", makeStubHandler("pshh", 0))
http.HandleFunc("/malformed2", makeStubHandler(`{"numbers: 42"}`, 200))
http.HandleFunc("/test1", makeStubHandler(`{"numbers": [1, 2, 3, 4], "extra": [99]}`, 0))
http.HandleFunc("/test2", makeStubHandler(`{"numbers": [5, 6, 1, 1]}`, 100))
http.HandleFunc("/test3", makeStubHandler(`{"numbers": [1, 2]}`, 300))
http.HandleFunc("/test4", makeStubHandler(`{"numbers": [11, 12]}`, 400))
http.HandleFunc("/test5", makeStubHandler(`{"numbers": [101, 102]}`, 450))
http.HandleFunc("/test6", makeStubHandler(`{"numbers": [1001, 1002]}`, 550))
startServer(t, 8080)
testCases := []TestCase{
{Url: "http://127.0.0.1:8080/malformed1", Numbers: nil, Status: -1},
{Url: "http://127.0.0.1:8080/malformed2", Numbers: nil, Status: -1},
{Url: "http://127.0.0.1:8080/unimplemented", Numbers: nil, Status: 404},
{Url: "http://127.0.0.1:8080/numbers?u=http://127.0.0.1:8080/wrong", Numbers: nil, Status: 200},
{Url: "http://127.0.0.1:8080/numbers?u=127.0.0.1:8080/test1", Numbers: nil, Status: 200}, // wrong url in `u`
{Url: "http://127.0.0.1:8080/numbers?u=http://127.0.0.1:8080/test1", Numbers: []int{1, 2, 3, 4}, Status: 200},
{Url: "http://127.0.0.1:8080/numbers?u=http://127.0.0.1:8080/test1&u=http://127.0.0.1:8080/test2", Numbers: []int{1, 2, 3, 4, 5, 6}, Status: 200},
{Url: "http://127.0.0.1:8080/numbers?u=http://127.0.0.1:8080/test3&u=http://127.0.0.1:8080/test4&u=http://127.0.0.1:8080/test5&u=http://127.0.0.1:8080/test6", Numbers: []int{1, 2, 11, 12, 101, 102}, Status: 200},
}
var wg sync.WaitGroup
for _, test := range testCases {
wg.Add(1)
go func(test TestCase) {
defer wg.Done()
numbers_get(t, test.Url, test.Status, test.Numbers)
}(test)
}
wg.Wait()
// Make "recursive" request. Use url.Values to properly URL-encode `u` arguments.
q := url.Values{}
q.Add("u", "http://127.0.0.1:8080/test5")
q.Add("u", "http://127.0.0.1:8080/numbers?u=http://127.0.0.1:8080/test2&u=http://127.0.0.1:8080/test4")
u := url.URL{
Scheme: "http",
Host: "127.0.0.1:8080",
Path: "numbers",
RawQuery: q.Encode(),
}
numbers_get(t, u.String(), 200, []int{1, 5, 6, 11, 12, 101, 102})
}