forked from rewardStyle/kinetic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckpointer_test.go
483 lines (442 loc) · 13.8 KB
/
checkpointer_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
package kinetic
import (
"container/list"
"context"
"fmt"
"math/rand"
"sort"
"strconv"
"sync/atomic"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
// initialize the randomization seed for the random number generator.
func init() {
rand.Seed(time.Now().UnixNano())
}
// sortInt is an alias to a slice array of int that implements the sort interface for sorting.
type sortInt []int
// Len implements the Len function for hte sort interface.
func (k sortInt) Len() int {
return len(k)
}
// Less implements the Less function for the sort interface.
func (k sortInt) Less(i, j int) bool {
return k[i] < k[j]
}
// Swap implements the Swap function for the sort interface.
func (k sortInt) Swap(i, j int) {
k[i], k[j] = k[j], k[i]
}
// sortUnique is a helper method used to return a unique and sorted slice of keys.
func sortUnique(keys sortInt) sortInt {
keyMap := make(map[int]bool)
for _, key := range keys {
if _, ok := keyMap[key]; !ok {
keyMap[key] = true
}
}
var sortedKeys sortInt
for key := range keyMap {
sortedKeys = append(sortedKeys, key)
}
sort.Sort(sortedKeys)
return sortedKeys
}
// randomInts is helper method used to generate a random set of keys for testing.
func randomInts(size, max int) (ints []int) {
for i := 0; i < size; i++ {
ints = append(ints, int(rand.Intn(max)))
}
return ints
}
// intPointer is a helper method used to return the address of an int
func intPointer(x int) *int {
return &x
}
func TestCheckpointListInsert(t *testing.T) {
var tests = []struct {
scenario string
numbers []int
}{
{"insert into an empty list", []int{1}},
{"insert duplicate element", []int{1, 1}},
{"insert in ascending order", []int{1, 2, 3, 4, 5}},
{"insert in descending order", []int{5, 4, 3, 2, 1}},
{"insert in random order", randomInts(100, 1000)},
}
for _, test := range tests {
c := &checkpointList{list.New()}
Convey(fmt.Sprintf("running checkpointList test suite on scenario: [%s]", test.scenario), t, func() {
Convey("inserting numbers into list", func() {
var errCount int
for _, number := range test.numbers {
if err := c.insert(number); err != nil {
errCount++
}
}
So(errCount, ShouldEqual, 0)
Convey("confirm the length and order of the list", func() {
sortedUniqueNumbers := sortUnique(test.numbers)
var actualNumbers sortInt
for e := c.Front(); e != nil; e = e.Next() {
actualNumbers = append(actualNumbers, e.Value.(*checkpointElement).seqNum)
}
So(len(actualNumbers), ShouldEqual, len(sortedUniqueNumbers))
So(sort.IsSorted(actualNumbers), ShouldBeTrue)
})
})
})
}
}
func TestCheckpointListFind(t *testing.T) {
testNumbers := []int{3, 2, 4, 5, 1}
var tests = []struct {
scenario string
numbers []int
find int
shouldFind bool
}{
{"find on an empty list", []int{}, 0, false},
{"find on an element that does not exist", testNumbers, 0, false},
{"find the biggeset element", testNumbers, 5, true},
{"find the smallest element", testNumbers, 1, true},
{"find a middle element", testNumbers, 3, true},
}
for _, test := range tests {
c := &checkpointList{list.New()}
Convey(fmt.Sprintf("running find test suite on scenario: [%s]", test.scenario), t, func() {
Convey("inserting numbers into list", func() {
var errCount int
for _, number := range test.numbers {
if err := c.insert(number); err != nil {
errCount++
}
}
So(errCount, ShouldEqual, 0)
Convey("find should behave as expected", func() {
el, found := c.find(test.find)
So(found, ShouldEqual, test.shouldFind)
if found {
So(el.Value.(*checkpointElement).seqNum, ShouldEqual, test.find)
}
})
})
})
}
}
func TestCheckpointerInsertAndSize(t *testing.T) {
var tests = []struct {
scenario string
numbers []int
}{
{"insert into an empty list", []int{1}},
{"insert duplicate elements", []int{1, 1}},
{"insert into an empty list", []int{1}},
{"insert in ascending order", []int{11, 22, 33, 44, 55}},
{"insert in descending order", []int{55, 44, 33, 22, 11}},
{"insert in random order", randomInts(100, 1000)},
}
for _, test := range tests {
c := newCheckpointer()
Convey(fmt.Sprintf("running insert and size test suite on scenario: [%s]", test.scenario), t, func() {
Convey("inserting numbers into list", func() {
var errCount int
for _, number := range test.numbers {
if err := c.insert(strconv.Itoa(number)); err != nil {
errCount++
}
}
So(errCount, ShouldEqual, 0)
Convey("confirm the size and order of the list", func() {
sortedUniqueNumbers := sortUnique(test.numbers)
var actualNumbers sortInt
for e := c.list.Front(); e != nil; e = e.Next() {
actualNumbers = append(actualNumbers, e.Value.(*checkpointElement).seqNum)
}
So(c.size(), ShouldEqual, len(sortedUniqueNumbers))
So(len(actualNumbers), ShouldEqual, len(sortedUniqueNumbers))
So(sort.IsSorted(actualNumbers), ShouldBeTrue)
})
})
})
}
}
func TestCheckpointerMarkDoneAndCheck(t *testing.T) {
testEmpty := []int{}
testNumbers := []int{5, 2, 6, 4, 0, 1, 3}
var tests = []struct {
scenario string
numbers []int
doneNumbers []int
shouldErrorOnMarkDone bool
expectedSize *int
shouldFindCheckpoint bool
expectedCheckpoint *int
}{
{
scenario: "markDone on an empty list",
numbers: testEmpty,
doneNumbers: []int{1},
shouldErrorOnMarkDone: true,
expectedSize: nil,
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone on non existent number",
numbers: testNumbers,
doneNumbers: []int{-1},
shouldErrorOnMarkDone: true,
expectedSize: nil,
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "check when no numbers are inserted",
numbers: testEmpty,
doneNumbers: testEmpty,
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testEmpty)),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "check when no numbers are marked done",
numbers: testNumbers,
doneNumbers: testEmpty,
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers)),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone on an existing number (not smallest number) and check",
numbers: testNumbers,
doneNumbers: []int{1},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers)),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone twice on an existing number (not smallest number) and check ",
numbers: testNumbers,
doneNumbers: []int{1, 1},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers)),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone on two sequential numbers (not smallest number, ascending) and check",
numbers: testNumbers,
doneNumbers: []int{1, 2},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers) - 1),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone on two sequential numbers (not smallest number, descending) and check",
numbers: testNumbers,
doneNumbers: []int{2, 1},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers) - 1),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "markDone on three sequential numbers (not smallest number) and check",
numbers: testNumbers,
doneNumbers: []int{3, 1, 2},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(len(testNumbers) - 2),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "check when the smallest number is not marked done",
numbers: testNumbers,
doneNumbers: []int{1, 2, 3, 4, 5, 6},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(2),
shouldFindCheckpoint: false,
expectedCheckpoint: nil,
},
{
scenario: "check when a chain (including the smallest number) is marked done",
numbers: testNumbers,
doneNumbers: []int{2, 1, 3, 0},
shouldErrorOnMarkDone: false,
expectedSize: intPointer(4),
shouldFindCheckpoint: true,
expectedCheckpoint: intPointer(3),
},
{
scenario: "markDone on all numbers and check",
numbers: testNumbers,
doneNumbers: testNumbers,
shouldErrorOnMarkDone: false,
expectedSize: intPointer(1),
shouldFindCheckpoint: true,
expectedCheckpoint: intPointer(6),
},
}
for _, test := range tests {
c := newCheckpointer()
Convey(fmt.Sprintf("running markDone and check test suite on scenario: [%s]", test.scenario), t, func() {
Convey("inserting numbers into list", func() {
var errCount int
for _, number := range test.numbers {
if err := c.insert(strconv.Itoa(number)); err != nil {
errCount++
}
}
So(errCount, ShouldEqual, 0)
Convey("mark numbers done should behave as expected", func() {
var doneErrCount int
for _, doneNumber := range test.doneNumbers {
if err := c.markDone(strconv.Itoa(doneNumber)); err != nil {
doneErrCount++
}
}
if test.shouldErrorOnMarkDone {
So(doneErrCount, ShouldBeGreaterThan, 0)
return
}
So(doneErrCount, ShouldEqual, 0)
So(c.size(), ShouldEqual, *test.expectedSize)
Convey("call check should behave as expected", func() {
cp, found := c.check()
if test.shouldFindCheckpoint {
So(found, ShouldBeTrue)
So(cp, ShouldEqual, strconv.Itoa(*test.expectedCheckpoint))
} else {
So(found, ShouldBeFalse)
}
})
})
})
})
}
}
func TestCheckpointerAutoCheckpointing(t *testing.T) {
testNumbers := []int{1, -2, 3, -4, 5}
Convey("test auto checkpoint using autoCheckpointCount", t, func() {
autoCheckpointCount := 5
Convey("instantiating a new checkpoint with options", func() {
var checkpointFnCalled uint64
cp := newCheckpointer(
checkpointCountCheckFreq(time.Millisecond),
checkpointAutoCheckpointFreq(time.Minute),
checkpointAutoCheckpointCount(autoCheckpointCount),
checkpointCheckpointFn(func(seqNum string) error {
atomic.AddUint64(&checkpointFnCalled, 1)
return nil
}),
)
So(cp, ShouldNotBeNil)
So(cp.autoCheckpointCount, ShouldEqual, autoCheckpointCount)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
cp.startup(ctx)
Convey("inserting and marking all keys as done", func() {
var failedCount int
for _, testKey := range testNumbers {
key := strconv.Itoa(testKey)
if err := cp.insert(key); err != nil {
failedCount++
}
if err := cp.markDone(key); err != nil {
failedCount++
}
}
So(failedCount, ShouldEqual, 0)
<-time.After(2 * time.Millisecond)
Convey("confirming that checkpoint was called", func() {
So(atomic.LoadUint64(&checkpointFnCalled), ShouldBeGreaterThan, 0)
})
})
})
})
Convey("test auto checkpoint using autoCheckpointFreq", t, func() {
autoCheckpointFreq := 100 * time.Millisecond
Convey("instantiating a new checkpoint with options", func() {
var checkpointFnCalled uint64
cp := newCheckpointer(
checkpointAutoCheckpointFreq(autoCheckpointFreq),
checkpointAutoCheckpointCount(1000),
checkpointCheckpointFn(func(seqNum string) error {
atomic.AddUint64(&checkpointFnCalled, 1)
return nil
}),
)
So(cp, ShouldNotBeNil)
So(cp.autoCheckpointFreq, ShouldEqual, autoCheckpointFreq)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
cp.startup(ctx)
Convey("inserting and marking all keys as done", func() {
var failedCount int
for _, testKey := range testNumbers {
key := strconv.Itoa(testKey)
if err := cp.insert(key); err != nil {
failedCount++
}
if err := cp.markDone(key); err != nil {
failedCount++
}
}
So(failedCount, ShouldEqual, 0)
<-time.After(time.Duration(2) * autoCheckpointFreq)
Convey("confirming that checkpoint was called", func() {
So(atomic.LoadUint64(&checkpointFnCalled), ShouldBeGreaterThan, 0)
})
})
})
})
Convey("test calling checkpoint function", t, func() {
testKeys := []int{1, -2, 3, -4, 5}
var checkpointFnCalled uint64
cp := newCheckpointer(
checkpointCheckpointFn(func(seqNum string) error {
atomic.AddUint64(&checkpointFnCalled, 1)
return nil
}),
)
So(cp, ShouldNotBeNil)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
cp.startup(ctx)
Convey("inserting and marking all keys as done", func() {
var failedCount int
for _, testKey := range testKeys {
key := strconv.Itoa(testKey)
if err := cp.insert(key); err != nil {
failedCount++
}
if err := cp.markDone(key); err != nil {
failedCount++
}
}
So(failedCount, ShouldEqual, 0)
Convey("calling checkpoint", func() {
err := cp.checkpoint()
So(err, ShouldBeNil)
<-time.After(500 * time.Millisecond)
Convey("confirming that checkpointFn was called", func() {
So(atomic.LoadUint64(&checkpointFnCalled), ShouldBeGreaterThan, 0)
})
})
})
})
}
func TestCheckpointerOffNominal(t *testing.T) {
Convey("insert a bogus number into the list", t, func() {
c := newCheckpointer()
err := c.insert("bogus data")
So(err, ShouldNotBeNil)
})
}