-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacciServer_test.go
339 lines (295 loc) · 9.29 KB
/
fibonacciServer_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"errors"
"fmt"
"github.com/tmjd/fibonacci"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestGettingIterationCountFromRequest(t *testing.T) {
var test_values = []struct {
expected int
expect_err error
method string
url string
body string
headerName string
headerValue string
}{
{10, nil, "POST", "http://example.com/fibonacci", "n=10", "Content-Type",
"application/x-www-form-urlencoded; param=value"},
{10, nil, "POST", "http://example.com/fibonacci?n=10", "", "Content-Type",
"application/x-www-form-urlencoded; param=value"},
{10, nil, "GET", "http://example.com/fibonacci?n=10", "", "", ""},
{10, errors.New("DontCare"), "GET", "http://example.com/fibonacci", "", "", ""},
{0, nil, "POST", "http://example.com/fibonacci", "n=0", "Content-Type",
"application/x-www-form-urlencoded; param=value"},
{21, nil, "POST", "http://example.com/fibonacci", "n=21", "Content-Type",
"application/x-www-form-urlencoded; param=value"},
{10, errors.New("DontCare"), "POST", "http://example.com/fibonacci",
"n=10", "Content-Type", "application/bad; param=value"},
{10, errors.New("DontCare"), "POST", "http://example.com/fibonacci",
"n:10", "Content-Type", "application/x-www-form-urlencoded; param=value"},
}
for i, test_val := range test_values {
req, err := http.NewRequest(test_val.method, test_val.url,
strings.NewReader(test_val.body))
if err != nil {
t.Fatal(err)
}
req.Header.Set(test_val.headerName, test_val.headerValue)
result, err := getIterationCount(req)
if err != nil {
if test_val.expect_err == nil {
t.Errorf("Iteration %d did not meet err expectation", i)
}
} else if result != test_val.expected {
t.Errorf("Iteration %d resulted in %d but expected %d", i, result, test_val.expected)
}
}
}
func TestGettingIterationCountFromMultipartRequest(t *testing.T) {
var test_values = []struct {
expected int
expect_err error
method string
body *strings.Reader
headerName string
headerValue string
}{
{10, nil, "POST",
strings.NewReader("--foo\r\n" +
"Content-Disposition: form-data; name='n'\r\n\r\n" +
"10\r\n--foo--\r\n"),
"Content-Type", "multipart/form-data; boundary=foo"},
{10, errors.New("DontCare"), "POST",
strings.NewReader("--foo\r\n" +
"Content-Disposition: form-data; name='n'\r\n\r\n" +
"10\r\n--foo--\r\n"),
"Content-Type", "multipart/mixed; boundary=foo"},
}
for i, test_val := range test_values {
req, err := http.NewRequest(test_val.method, "http://example.com/fibonacci",
test_val.body)
if err != nil {
t.Fatal(err)
}
req.Header.Set(test_val.headerName, test_val.headerValue)
result, err := getIterationCount(req)
if err != nil {
if test_val.expect_err == nil {
t.Errorf("Iteration %d caused getIterationCount to produce error: %s on %q", i, err, req)
}
} else if result != test_val.expected {
t.Errorf("Iteration %d resulted in %d but expected %d", i, result, test_val.expected)
}
}
}
func TestBuildOutput(t *testing.T) {
var test_values = []struct {
iterations int
expected string
}{
{0, "[]"},
{1, "[0]"},
{2, "[0,1]"},
{10, "[0,1,1,2,3,5,8,13,21,34]"},
}
for _, test_val := range test_values {
fg, err := fibonacci.NewGenerator(test_val.iterations)
if err != nil {
t.Errorf("Creating fibonacci generator should not have errored with %d",
test_val.iterations)
}
out_chan := make(chan fibonacci.FibNum)
go fg.Produce(out_chan)
output := buildOutput(out_chan)
if string(output[:]) != test_val.expected {
t.Errorf("Output from iteration %d did not match.\nExpected\n%s\nGot\n%s\n",
test_val.iterations, test_val.expected, string(output[:]))
}
}
}
func TestHandlerValidPostRequestResultsInSuccess(t *testing.T) {
req, err := http.NewRequest("POST", "http://example.com/fibonacci",
strings.NewReader("n=10"))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
res := httptest.NewRecorder()
frh := NewFibonacciRequestHandler("fibonacci")
frh.FibonacciRequestHandleFunc(res, req)
if res.Code != 200 {
t.Errorf("Expect success from POST command with body: %q", res)
}
}
func TestHandlerPostWithNoBodyResponseFailure(t *testing.T) {
req, err := http.NewRequest("POST", "http://example.com/fibonacci", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
res := httptest.NewRecorder()
frh := NewFibonacciRequestHandler("fibonacci")
frh.FibonacciRequestHandleFunc(res, req)
if res.Code == 200 {
t.Errorf("Expect failure from POST with no body")
}
}
func TestHandlerWithUnsupportedMethod(t *testing.T) {
req, err := http.NewRequest("DELETE", "http://example.com/fibonacci", nil)
if err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
frh := NewFibonacciRequestHandler("fibonacci")
frh.FibonacciRequestHandleFunc(res, req)
if res.Code == 200 {
t.Errorf("Expect failure from DELETE method")
}
}
func TestFibonacciHandlerCheckPath(t *testing.T) {
var test_values = []struct {
expected_code int
expected_body string
expect_log bool
handle_url string
test_url string
}{
//code, body, handle_url test_url
{200, "[0,1,1,2,3,5,8,13,21,34]", false, "fibonacci", "fibonacci"},
{404, "does not match", true, "fibonacci", "fibonacci/extra"},
{404, "does not match", true, "fibonacci", "extra"},
}
defer clearInjectionPoints()
logged := false
var msg string
writeLogMsg = func(format string, v ...interface{}) {
msg = fmt.Sprintf(format, v)
logged = true
}
for i, test_val := range test_values {
req, err := http.NewRequest("POST", fmt.Sprintf("http://example.com/%s", test_val.test_url),
strings.NewReader("n=10"))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
res := httptest.NewRecorder()
frh := NewFibonacciRequestHandler(test_val.handle_url)
logged = false
frh.FibonacciRequestHandleFunc(res, req)
if logged {
if !test_val.expect_log {
t.Errorf("Iteration %d received log msg (%s) when none was expected", i, msg)
}
} else {
if test_val.expect_log {
t.Errorf("Iteration %d expected log message but none was written", i)
}
}
if res.Code != test_val.expected_code {
t.Errorf("Iteration %d response code expected %q but got %q",
res.Code, test_val.expected_code)
}
if !strings.Contains(res.Body.String(), test_val.expected_body) {
t.Errorf("Iteration %d body did not match\nGot\n%s\nExpected\n%s",
i, test_val.expected_body, res.Body)
}
}
}
func TestStatsMonitor(t *testing.T) {
defer clearInjectionPoints()
frh := NewFibonacciRequestHandler("fibonacci")
trigger_chan := make(chan time.Time)
timeTriggerDelay = func(time.Duration) <-chan time.Time {
return trigger_chan
}
selectDone_chan := make(chan bool)
statSelectDone = func() {
selectDone_chan <- true
}
log_chan := make(chan string)
logged := false
writeLogMsg = func(format string, v ...interface{}) {
log_chan <- fmt.Sprintf(format, v)
logged = true
}
go frh.statsMonitor()
frh.activeReq <- 1
<-selectDone_chan
frh.activeReq <- 1
<-selectDone_chan
frh.activeReq <- 1
<-selectDone_chan
trigger_chan <- time.Now()
result := <-log_chan
<-selectDone_chan
if !strings.Contains(result, "Requests 3 Concurrent 3") {
t.Errorf("Result (%s) did not have expected value 3", result)
}
trigger_chan <- time.Now()
result = <-log_chan
<-selectDone_chan
if !strings.Contains(result, "Requests 0 Concurrent 3") {
t.Errorf("Result (%s) should still have expected value 3", result)
}
//Clear out all the 'active' requests
frh.activeReq <- -1
<-selectDone_chan
frh.activeReq <- -1
<-selectDone_chan
frh.activeReq <- -1
<-selectDone_chan
trigger_chan <- time.Now()
// Let log message happen but we don't care about this one
result = <-log_chan
<-selectDone_chan
logged = false
trigger_chan <- time.Now()
<-selectDone_chan
if logged {
t.Errorf("Expected nothing to have been logged recieved %s", <-log_chan)
}
// Have an active request so all subsequent time triggers will result in a log message
frh.activeReq <- 1
<-selectDone_chan
//reqState: duration, iterations
dur, _ := time.ParseDuration("7s")
frh.reqStats <- reqStat{dur, 14}
<-selectDone_chan
trigger_chan <- time.Now()
result = <-log_chan
<-selectDone_chan
if strings.Contains(result, "n=14-7s") == false {
t.Errorf("After sending a n=14, with a duration of 7s the values were not in the log message %s", result)
}
dur, _ = time.ParseDuration("8s")
frh.reqStats <- reqStat{dur, 4}
<-selectDone_chan
dur, _ = time.ParseDuration("2s")
frh.reqStats <- reqStat{dur, 15}
<-selectDone_chan
trigger_chan <- time.Now()
result = <-log_chan
<-selectDone_chan
if strings.Contains(result, "MaxElapse:n=4-8s") == false || strings.Contains(result, "MaxIterations:n=15-2s") == false {
t.Errorf("Log message %s did not have expected n=4-8s and n=15-2s", result)
}
dur, _ = time.ParseDuration("8s")
frh.reqStats <- reqStat{dur, 4}
<-selectDone_chan
dur, _ = time.ParseDuration("12s")
frh.reqStats <- reqStat{dur, 15}
<-selectDone_chan
trigger_chan <- time.Now()
result = <-log_chan
<-selectDone_chan
if strings.Contains(result, "MaxElapse:n=15-12s") == false {
t.Errorf("Log message %s did not have expected n=14-12s", result)
}
}