-
Notifications
You must be signed in to change notification settings - Fork 52
/
baskets_mem_test.go
450 lines (372 loc) · 14.9 KB
/
baskets_mem_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func createTestPOSTRequest(reqURL string, content string, contentType string) *http.Request {
request := new(http.Request)
request.Method = "POST"
request.URL, _ = url.Parse(reqURL)
request.Body = ioutil.NopCloser(strings.NewReader(content))
request.ContentLength = int64(len(content))
request.Header = make(http.Header)
request.Header.Add("Content-Type", contentType)
request.Header.Add("User-Agent", "Unit-Test")
request.Header.Add("Accept", "application/json")
return request
}
func TestMemoryDatabase_Create(t *testing.T) {
name := "test1"
db := NewMemoryDatabase()
defer db.Release()
auth, err := db.Create(name, BasketConfig{Capacity: 20})
if assert.NoError(t, err) {
assert.NotEmpty(t, auth.Token, "basket token may not be empty")
assert.False(t, len(auth.Token) < 30, "weak basket token: %v", auth.Token)
}
}
func TestMemoryDatabase_Create_NameConflict(t *testing.T) {
name := "test2"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 20})
auth, err := db.Create(name, BasketConfig{Capacity: 20})
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "'"+name+"'", "error is not detailed enough")
assert.Empty(t, auth.Token, "basket token is not expected")
}
}
func TestMemoryDatabase_Get(t *testing.T) {
name := "test3"
db := NewMemoryDatabase()
defer db.Release()
auth, err := db.Create(name, BasketConfig{Capacity: 16})
assert.NoError(t, err)
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
assert.True(t, basket.Authorize(auth.Token), "basket authorization has failed")
assert.Equal(t, 16, basket.Config().Capacity, "wrong capacity")
}
}
func TestMemoryDatabase_Get_NotFound(t *testing.T) {
name := "test4"
db := NewMemoryDatabase()
defer db.Release()
basket := db.Get(name)
assert.Nil(t, basket, "basket with name: %v is not expected", name)
}
func TestMemoryDatabase_Delete(t *testing.T) {
name := "test5"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 10})
assert.NotNil(t, db.Get(name), "basket with name: %v is expected", name)
db.Delete(name)
assert.Nil(t, db.Get(name), "basket with name: %v is not expected", name)
}
func TestMemoryDatabase_Delete_Multi(t *testing.T) {
name := "test6"
db := NewMemoryDatabase()
defer db.Release()
config := BasketConfig{Capacity: 10}
for i := 0; i < 10; i++ {
db.Create(fmt.Sprintf("test%v", i), config)
}
assert.NotNil(t, db.Get(name), "basket with name: %v is expected", name)
assert.Equal(t, 10, db.Size(), "wrong database size")
db.Delete(name)
assert.Nil(t, db.Get(name), "basket with name: %v is not expected", name)
assert.Equal(t, 9, db.Size(), "wrong database size")
}
func TestMemoryDatabase_Size(t *testing.T) {
db := NewMemoryDatabase()
defer db.Release()
config := BasketConfig{Capacity: 15}
for i := 0; i < 25; i++ {
db.Create(fmt.Sprintf("test%v", i), config)
}
assert.Equal(t, 25, db.Size(), "wrong database size")
}
func TestMemoryDatabase_GetNames(t *testing.T) {
db := NewMemoryDatabase()
defer db.Release()
config := BasketConfig{Capacity: 15}
for i := 0; i < 45; i++ {
db.Create(fmt.Sprintf("test%v", i), config)
}
// Get and validate page 1 (test0 - test9)
page1 := db.GetNames(10, 0)
assert.Equal(t, 45, page1.Count, "wrong baskets count")
assert.True(t, page1.HasMore, "expected more names")
assert.Len(t, page1.Names, 10, "wrong page size")
assert.Equal(t, "test2", page1.Names[2], "wrong basket name at index #2")
// Get and validate page 5 (test40 - test44)
page5 := db.GetNames(10, 40)
assert.Equal(t, 45, page5.Count, "wrong baskets count")
assert.False(t, page5.HasMore, "no more names are expected")
assert.Len(t, page5.Names, 5, "wrong page size")
assert.Equal(t, "test40", page5.Names[0], "wrong basket name at index #0")
// Corner cases
assert.Empty(t, db.GetNames(0, 0).Names, "names are not expected")
assert.False(t, db.GetNames(5, 40).HasMore, "no more names are expected")
}
func TestMemoryDatabase_FindNames(t *testing.T) {
db := NewMemoryDatabase()
defer db.Release()
config := BasketConfig{Capacity: 5}
for i := 0; i < 35; i++ {
db.Create(fmt.Sprintf("test%v", i), config)
}
res1 := db.FindNames("test2", 20, 0)
assert.False(t, res1.HasMore, "no more names are expected")
assert.Len(t, res1.Names, 11, "wrong number of found names")
for _, name := range res1.Names {
assert.Contains(t, name, "test2", "invalid name among search results")
}
res2 := db.FindNames("test1", 5, 0)
assert.True(t, res2.HasMore, "more names are expected")
assert.Len(t, res2.Names, 5, "wrong number of found names")
// Corner cases
assert.Len(t, db.FindNames("test1", 5, 10).Names, 1, "wrong number of returned names")
assert.Empty(t, db.FindNames("test2", 5, 20).Names, "names in this page are not expected")
assert.False(t, db.FindNames("test3", 5, 6).HasMore, "no more names are expected")
assert.False(t, db.FindNames("abc", 5, 0).HasMore, "no more names are expected")
assert.Empty(t, db.FindNames("xyz", 5, 0).Names, "names are not expected")
}
func TestMemoryBasket_Add(t *testing.T) {
name := "test101"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 20})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// add 1st HTTP request
content := "{ \"user\": \"tester\", \"age\": 24 }"
data := basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v/demo?name=abc&ver=12", name), content, "application/json"))
assert.Equal(t, 1, basket.Size(), "wrong basket size")
// detailed http.Request to RequestData tests should be covered by test of ToRequestData function
assert.Equal(t, content, data.Body, "wrong body")
assert.Equal(t, int64(len(content)), data.ContentLength, "wrong content length")
// add 2nd HTTP request
basket.Add(createTestPOSTRequest(fmt.Sprintf("http://localhost/%v/demo", name), "Hellow world", "text/plain"))
assert.Equal(t, 2, basket.Size(), "wrong basket size")
}
}
func TestMemoryBasket_Add_ExceedLimit(t *testing.T) {
name := "test102"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 10})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// fill basket
for i := 0; i < 35; i++ {
basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v/demo", name), fmt.Sprintf("test%v", i), "text/plain"))
}
assert.Equal(t, 10, basket.Size(), "wrong basket size")
}
}
func TestMemoryBasket_Clear(t *testing.T) {
name := "test103"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 20})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// fill basket
for i := 0; i < 15; i++ {
basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v/demo", name), fmt.Sprintf("test%v", i), "text/plain"))
}
assert.Equal(t, 15, basket.Size(), "wrong basket size")
// clean basket
basket.Clear()
assert.Equal(t, 0, basket.Size(), "wrong basket size, empty basket is expected")
}
}
func TestMemoryBasket_Update_Shrink(t *testing.T) {
name := "test104"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 30})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// fill basket
for i := 0; i < 25; i++ {
basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v/demo", name), fmt.Sprintf("test%v", i), "text/plain"))
}
assert.Equal(t, 25, basket.Size(), "wrong basket size")
// update config with lower capacity
config := basket.Config()
config.Capacity = 12
basket.Update(config)
assert.Equal(t, config.Capacity, basket.Size(), "wrong basket size")
}
}
func TestMemoryBasket_GetRequests(t *testing.T) {
name := "test105"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 25})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// fill basket
for i := 1; i <= 35; i++ {
basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v/demo?id=%v", name, i), fmt.Sprintf("req%v", i), "text/plain"))
}
assert.Equal(t, 25, basket.Size(), "wrong basket size")
// Get and validate last 10 requests
page1 := basket.GetRequests(10, 0)
assert.True(t, page1.HasMore, "expected more requests")
assert.Len(t, page1.Requests, 10, "wrong page size")
assert.Equal(t, 25, page1.Count, "wrong requests count")
assert.Equal(t, 35, page1.TotalCount, "wrong requests total count")
assert.Equal(t, "req35", page1.Requests[0].Body, "last request #35 is expected at index #0")
// Get and validate 10 requests, skip 20
page3 := basket.GetRequests(10, 20)
assert.False(t, page3.HasMore, "no more requests are expected")
assert.Len(t, page3.Requests, 5, "wrong page size")
assert.Equal(t, 25, page3.Count, "wrong requests count")
assert.Equal(t, 35, page3.TotalCount, "wrong requests total count")
assert.Equal(t, "req15", page3.Requests[0].Body, "request #15 is expected at index #0")
}
}
func TestMemoryBasket_FindRequests(t *testing.T) {
name := "test106"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 100})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// fill basket
for i := 1; i <= 30; i++ {
r := createTestPOSTRequest(fmt.Sprintf("http://localhost/%v?id=%v", name, i), fmt.Sprintf("req%v", i), "text/plain")
r.Header.Add("HeaderId", fmt.Sprintf("header%v", i))
if i <= 10 {
r.Header.Add("ChocoPie", "yummy")
}
if i <= 20 {
r.Header.Add("Muffin", "tasty")
}
basket.Add(r)
}
assert.Equal(t, 30, basket.Size(), "wrong basket size")
// search everywhere
s1 := basket.FindRequests("req1", "any", 30, 0)
assert.False(t, s1.HasMore, "no more results are expected")
assert.Len(t, s1.Requests, 11, "wrong number of found requests")
for _, r := range s1.Requests {
assert.Contains(t, r.Body, "req1", "incorrect request among results")
}
// search everywhere (limited output)
s2 := basket.FindRequests("req2", "any", 5, 5)
assert.True(t, s2.HasMore, "more results are expected")
assert.Len(t, s2.Requests, 5, "wrong number of found requests")
// search in body (positive)
assert.Len(t, basket.FindRequests("req3", "body", 100, 0).Requests, 2, "wrong number of found requests")
// search in body (negative)
assert.Empty(t, basket.FindRequests("yummy", "body", 100, 0).Requests, "found unexpected requests")
// search in headers (positive)
assert.Len(t, basket.FindRequests("yummy", "headers", 100, 0).Requests, 10, "wrong number of found requests")
assert.Len(t, basket.FindRequests("tasty", "headers", 100, 0).Requests, 20, "wrong number of found requests")
// search in headers (negative)
assert.Empty(t, basket.FindRequests("req1", "headers", 100, 0).Requests, "found unexpected requests")
// search in query (positive)
assert.Len(t, basket.FindRequests("id=1", "query", 100, 0).Requests, 11, "wrong number of found requests")
// search in query (negative)
assert.Empty(t, basket.FindRequests("tasty", "query", 100, 0).Requests, "found unexpected requests")
}
}
func TestMemoryBasket_SetResponse(t *testing.T) {
name := "test107"
method := "POST"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 20})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// Ensure no response
assert.Nil(t, basket.GetResponse(method))
// Set response
basket.SetResponse(method, ResponseConfig{Status: 201, Body: "{ 'message' : 'created' }"})
// Get and validate
response := basket.GetResponse(method)
if assert.NotNil(t, response, "response for method: %v is expected", method) {
assert.Equal(t, 201, response.Status, "wrong HTTP response status")
assert.Equal(t, "{ 'message' : 'created' }", response.Body, "wrong HTTP response body")
assert.False(t, response.IsTemplate, "template is not expected")
}
}
}
func TestMemoryBasket_SetResponse_Update(t *testing.T) {
name := "test108"
method := "GET"
db := NewMemoryDatabase()
defer db.Release()
db.Create(name, BasketConfig{Capacity: 20})
basket := db.Get(name)
if assert.NotNil(t, basket, "basket with name: %v is expected", name) {
// Set response
basket.SetResponse(method, ResponseConfig{Status: 200, Body: ""})
// Update response
basket.SetResponse(method, ResponseConfig{Status: 200, Body: "welcome", IsTemplate: true})
// Get and validate
response := basket.GetResponse(method)
if assert.NotNil(t, response, "response for method: %v is expected", method) {
assert.Equal(t, 200, response.Status, "wrong HTTP response status")
assert.Equal(t, "welcome", response.Body, "wrong HTTP response body")
assert.True(t, response.IsTemplate, "template is expected")
}
}
}
func TestMemoryDatabase_GetStats(t *testing.T) {
name := "test130"
db := NewMemoryDatabase()
defer db.Release()
config := BasketConfig{Capacity: 5}
for i := 0; i < 10; i++ {
bname := fmt.Sprintf("%s_%v", name, i)
db.Create(bname, config)
// fill basket
basket := db.Get(bname)
for j := 0; j < 9-i; j++ {
basket.Add(createTestPOSTRequest(
fmt.Sprintf("http://localhost/%v?id=%v", bname, j), fmt.Sprintf("req%v", j), "text/plain"))
}
time.Sleep(20 * time.Millisecond)
}
// get stats
stats := db.GetStats(3)
if assert.NotNil(t, stats, "database statistics is expected") {
assert.Equal(t, 10, stats.BasketsCount, "wrong BasketsCount stats")
assert.Equal(t, 1, stats.EmptyBasketsCount, "wrong EmptyBasketsCount stats")
assert.Equal(t, 9, stats.MaxBasketSize, "wrong MaxBasketSize stats")
assert.Equal(t, 35, stats.RequestsCount, "wrong RequestsCount stats")
assert.Equal(t, 45, stats.RequestsTotalCount, "wrong RequestsTotalCount stats")
assert.Equal(t, 5, stats.AvgBasketSize, "wrong AvgBasketSize stats")
// top 3 by date
if assert.NotNil(t, stats.TopBasketsByDate, "top baskets by date are expected") {
assert.Equal(t, 3, len(stats.TopBasketsByDate), "unexpected number of top baskets")
test_validateBasketStats(t, stats.TopBasketsByDate[0], fmt.Sprintf("%s_%v", name, 8), 1, 1)
test_validateBasketStats(t, stats.TopBasketsByDate[1], fmt.Sprintf("%s_%v", name, 7), 2, 2)
test_validateBasketStats(t, stats.TopBasketsByDate[2], fmt.Sprintf("%s_%v", name, 6), 3, 3)
}
// top 3 by size
if assert.NotNil(t, stats.TopBasketsBySize, "top baskets by size are expected") {
assert.Equal(t, 3, len(stats.TopBasketsBySize), "unexpected number of top baskets")
test_validateBasketStats(t, stats.TopBasketsBySize[0], fmt.Sprintf("%s_%v", name, 0), 5, 9)
test_validateBasketStats(t, stats.TopBasketsBySize[1], fmt.Sprintf("%s_%v", name, 1), 5, 8)
test_validateBasketStats(t, stats.TopBasketsBySize[2], fmt.Sprintf("%s_%v", name, 2), 5, 7)
}
}
}