-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbytes_queue_test.go
510 lines (402 loc) · 11.5 KB
/
bytes_queue_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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package bigqueue
import (
"bytes"
"fmt"
"path"
"reflect"
"runtime"
"testing"
)
func TestPushAndPop(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(10, 0, true)
entry := []byte("hello")
// when
_, err := queue.Pop()
// then
assertEqual(t, "Empty queue", err.Error())
// when
queue.Push(entry)
// then
assertEqual(t, entry, pop(queue))
}
func TestLen(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
entry := []byte("hello")
assertEqual(t, 0, queue.Len())
// when
queue.Push(entry)
// then
assertEqual(t, queue.Len(), 1)
}
func TestPeek(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
entry := []byte("hello")
// when
read, err := queue.Peek()
err2 := queue.peekCheckErr(queue.head)
// then
assertEqual(t, err, err2)
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, 0, len(read))
// when
queue.Push(entry)
read, err = queue.Peek()
err2 = queue.peekCheckErr(queue.head)
// then
assertEqual(t, err, err2)
noError(t, err)
assertEqual(t, pop(queue), read)
assertEqual(t, entry, read)
}
func TestResetFullQueue(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(10, 20, false)
// when
queue.Push(blob('a', 3))
queue.Push(blob('b', 4))
// when
assertEqual(t, blob('a', 3), pop(queue)) // space freed at the beginning
_, err := queue.Push(blob('a', 3)) // will set q.full to true
// then
assertEqual(t, err, nil)
// when
queue.Reset()
queue.Push(blob('c', 8)) // should not trigger a re-allocation
// then
assertEqual(t, blob('c', 8), pop(queue))
assertEqual(t, queue.Capacity(), 10)
}
func TestReset(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
entry := []byte("hello")
// when
queue.Push(entry)
queue.Push(entry)
queue.Push(entry)
queue.Reset()
read, err := queue.Peek()
// then
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, 0, len(read))
// when
queue.Push(entry)
read, err = queue.Peek()
// then
noError(t, err)
assertEqual(t, pop(queue), read)
assertEqual(t, entry, read)
// when
read, err = queue.Peek()
// then
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, 0, len(read))
}
func TestReuseAvailableSpace(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
// when
queue.Push(blob('a', 70))
queue.Push(blob('b', 20))
queue.Pop()
queue.Push(blob('c', 20))
// then
assertEqual(t, 100, queue.Capacity())
assertEqual(t, blob('b', 20), pop(queue))
}
func TestAllocateAdditionalSpace(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(11, 0, false)
// when
queue.Push([]byte("hello1"))
queue.Push([]byte("hello2"))
// then
assertEqual(t, 22, queue.Capacity())
}
func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereHeadIsBeforeTail(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(25, 0, false)
// when
queue.Push(blob('a', 3)) // header + entry + left margin = 5 bytes
queue.Push(blob('b', 6)) // additional 7 bytes
queue.Pop() // space freed, 4 bytes available at the beginning
queue.Push(blob('c', 6)) // 7 bytes needed, 13 bytes available at the tail
// then
assertEqual(t, 25, queue.Capacity())
assertEqual(t, blob('b', 6), pop(queue))
assertEqual(t, blob('c', 6), pop(queue))
}
func TestUnchangedEntriesIndexesAfterAdditionalMemoryAllocationWhereHeadIsBeforeTail(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(25, 0, false)
// when
queue.Push(blob('a', 3)) // header + entry + left margin = 5 bytes
index, _ := queue.Push(blob('b', 6)) // additional 7 bytes
queue.Pop() // space freed, 4 bytes available at the beginning
newestIndex, _ := queue.Push(blob('c', 6)) // 7 bytes needed, 13 available at the tail
// then
assertEqual(t, 25, queue.Capacity())
assertEqual(t, blob('b', 6), get(queue, index))
assertEqual(t, blob('c', 6), get(queue, newestIndex))
}
func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereTailIsBeforeHead(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
// when
queue.Push(blob('a', 70)) // header + entry + left margin = 72 bytes
queue.Push(blob('b', 10)) // 72 + 10 + 1 = 83 bytes
queue.Pop() // space freed at the beginning
queue.Push(blob('c', 30)) // 31 bytes used at the beginning, tail pointer is before head pointer
queue.Push(blob('d', 40)) // 41 bytes needed but no available in one segment, allocate new memory
// then
assertEqual(t, 200, queue.Capacity())
assertEqual(t, blob('c', 30), pop(queue))
// empty blob fills space between tail and head,
// created when additional memory was allocated,
// it keeps current entries indexes unchanged
assertEqual(t, blob(0, 39), pop(queue))
assertEqual(t, blob('b', 10), pop(queue))
assertEqual(t, blob('d', 40), pop(queue))
}
func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereTailIsBeforeHead128(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(200, 0, false)
// when
queue.Push(blob('a', 30)) // header + entry + left margin = 32 bytes
queue.Push(blob('b', 1)) // 32 + 128 + 1 = 161 bytes
queue.Push(blob('b', 125)) // 32 + 128 + 1 = 161 bytes
queue.Push(blob('c', 20)) // 160 + 20 + 1 = 182
queue.Pop() // space freed at the beginning
queue.Pop() // free 2 bytes
queue.Pop() // free 126
queue.Push(blob('d', 30)) // 31 bytes used at the beginning, tail pointer is before head pointer, now free space is 128 bytes
queue.Push(blob('e', 160)) // invoke allocateAdditionalMemory but fill 127 bytes free space (It should be 128 bytes, but 127 are filled, leaving one byte unfilled)
// then
assertEqual(t, 400, queue.Capacity())
assertEqual(t, blob('d', 30), pop(queue))
assertEqual(t, blob(0, 126), pop(queue)) //126 bytes data with 2bytes header only possible as empty entry
assertEqual(t, blob('c', 20), pop(queue)) //The data is not expected
assertEqual(t, blob('e', 160), pop(queue))
}
func TestUnchangedEntriesIndexesAfterAdditionalMemoryAllocationWhereTailIsBeforeHead(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(100, 0, false)
// when
queue.Push(blob('a', 70)) // header + entry + left margin = 72 bytes
index, _ := queue.Push(blob('b', 10)) // 72 + 10 + 1 = 83 bytes
queue.Pop() // space freed at the beginning
queue.Push(blob('c', 30)) // 31 bytes used at the beginning, tail pointer is before head pointer
newestIndex, _ := queue.Push(blob('d', 40)) // 41 bytes needed but no available in one segment, allocate new memory
// then
assertEqual(t, 200, queue.Capacity())
assertEqual(t, blob('b', 10), get(queue, index))
assertEqual(t, blob('d', 40), get(queue, newestIndex))
}
func TestAllocateAdditionalSpaceForValueBiggerThanInitQueue(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(11, 0, false)
// when
queue.Push(blob('a', 100))
// then
assertEqual(t, blob('a', 100), pop(queue))
// 224 = (101 + 11) * 2
assertEqual(t, 224, queue.Capacity())
}
func TestAllocateAdditionalSpaceForValueBiggerThanQueue(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(21, 0, false)
// when
queue.Push(make([]byte, 2))
queue.Push(make([]byte, 2))
queue.Push(make([]byte, 100))
// then
queue.Pop()
queue.Pop()
assertEqual(t, make([]byte, 100), pop(queue))
// 244 = (101 + 21) * 2
assertEqual(t, 244, queue.Capacity())
}
func TestPopWholeQueue(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(13, 0, false)
// when
queue.Push([]byte("a"))
queue.Push([]byte("b"))
queue.Pop()
queue.Pop()
queue.Push([]byte("c"))
// then
assertEqual(t, 13, queue.Capacity())
assertEqual(t, []byte("c"), pop(queue))
}
func TestGetEntryFromIndex(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(20, 0, false)
// when
queue.Push([]byte("a"))
index, _ := queue.Push([]byte("b"))
queue.Push([]byte("c"))
result, _ := queue.Get(index)
// then
assertEqual(t, []byte("b"), result)
}
func TestGetEntryFromInvalidIndex(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(1, 0, false)
queue.Push([]byte("a"))
// when
result, err := queue.Get(0)
err2 := queue.CheckGet(0)
// then
assertEqual(t, err, err2)
assertEqual(t, []byte(nil), result)
assertEqual(t, "Index must be greater than zero. Invalid index.", err.Error())
}
func TestGetEntryFromIndexOutOfRange(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(1, 0, false)
queue.Push([]byte("a"))
// when
result, err := queue.Get(42)
err2 := queue.CheckGet(42)
// then
assertEqual(t, err, err2)
assertEqual(t, []byte(nil), result)
assertEqual(t, "Index out of range", err.Error())
}
func TestGetEntryFromEmptyQueue(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(13, 0, false)
// when
result, err := queue.Get(1)
err2 := queue.CheckGet(1)
// then
assertEqual(t, err, err2)
assertEqual(t, []byte(nil), result)
assertEqual(t, "Empty queue", err.Error())
}
func TestMaxSizeLimit(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(30, 50, false)
// when
queue.Push(blob('a', 25))
queue.Push(blob('b', 5))
capacity := queue.Capacity()
_, err := queue.Push(blob('c', 20))
// then
assertEqual(t, 50, capacity)
assertEqual(t, "Full queue. Maximum size limit reached.", err.Error())
assertEqual(t, blob('a', 25), pop(queue))
assertEqual(t, blob('b', 5), pop(queue))
}
func TestPushEntryAfterAllocateAdditionMemory(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(9, 20, true)
// when
queue.Push([]byte("aaa"))
queue.Push([]byte("bb"))
queue.Pop()
// allocate more memory
assertEqual(t, 9, queue.Capacity())
queue.Push([]byte("c"))
assertEqual(t, 18, queue.Capacity())
// push after allocate
_, err := queue.Push([]byte("d"))
noError(t, err)
}
func TestPushEntryAfterAllocateAdditionMemoryInFull(t *testing.T) {
t.Parallel()
// given
queue := NewBytesQueue(9, 40, true)
// when
queue.Push([]byte("aaa"))
queue.Push([]byte("bb"))
_, err := queue.Pop()
noError(t, err)
queue.Push([]byte("c"))
queue.Push([]byte("d"))
queue.Push([]byte("e"))
_, err = queue.Pop()
noError(t, err)
_, err = queue.Pop()
noError(t, err)
queue.Push([]byte("fff"))
_, err = queue.Pop()
noError(t, err)
}
func pop(queue *BytesQueue) []byte {
entry, err := queue.Pop()
if err != nil {
panic(err)
}
return entry
}
func get(queue *BytesQueue, index int) []byte {
entry, err := queue.Get(index)
if err != nil {
panic(err)
}
return entry
}
func blob(char byte, len int) []byte {
return bytes.Repeat([]byte{char}, len)
}
func assertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
if !objectsAreEqual(expected, actual) {
_, file, line, _ := runtime.Caller(1)
file = path.Base(file)
t.Errorf(fmt.Sprintf("\n%s:%d: Not equal: \n"+
"expected: %T(%#v)\n"+
"actual : %T(%#v)\n",
file, line, expected, expected, actual, actual), msgAndArgs...)
}
}
func noError(t *testing.T, e error) {
if e != nil {
_, file, line, _ := runtime.Caller(1)
file = path.Base(file)
t.Errorf(fmt.Sprintf("\n%s:%d: Error is not nil: \n"+
"actual : %T(%#v)\n", file, line, e, e))
}
}
func objectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}