-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_index_test.go
147 lines (125 loc) · 3.48 KB
/
rolling_index_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
package common
import (
"fmt"
"reflect"
"testing"
"github.com/bolaxy/errors"
"github.com/dgraph-io/badger"
)
func TestRollingIndex(t *testing.T) {
size := 10
testSize := 3 * size
RollingIndex := NewRollingIndex("test", size)
items := []string{}
for i := 0; i < testSize; i++ {
item := fmt.Sprintf("item%d", i)
RollingIndex.Set(item, i)
items = append(items, item)
}
cached, lastIndex := RollingIndex.GetLastWindow()
expectedLastIndex := testSize - 1
if lastIndex != expectedLastIndex {
t.Fatalf("lastIndex should be %d, not %d", expectedLastIndex, lastIndex)
}
start := (testSize / (2 * size)) * (size)
count := testSize - start
for i := 0; i < count; i++ {
if cached[i] != items[start+i] {
t.Fatalf("cached[%d] should be %s, not %s", i, items[start+i], cached[i])
}
}
err := RollingIndex.Set("ErrSkippedIndex", expectedLastIndex+2)
if err == nil || !errors.Is(err, errors.SkippedIndex) {
t.Fatalf("Should return ErrSkippedIndex")
}
_, err = RollingIndex.GetItem(9)
if err == nil || !errors.Is(err, errors.TooLate) {
t.Fatalf("Should return ErrTooLate")
}
var item interface{}
indexes := []int{10, 17, 29}
for _, i := range indexes {
item, err = RollingIndex.GetItem(i)
if err != nil {
t.Fatalf("GetItem(%d) err: %v", i, err)
}
if !reflect.DeepEqual(item, items[i]) {
t.Fatalf("GetItem error")
}
}
_, err = RollingIndex.GetItem(lastIndex + 1)
if err == nil || err.Error() != badger.ErrKeyNotFound.Error() { //!errors.Is(err, errors.KeyNotFound) {
t.Fatalf("Should return KeyNotFound")
}
//Test updating an item in place
updateIndex := 26
updateValue := "Updated Item"
err = RollingIndex.Set(updateValue, updateIndex)
if err != nil {
t.Fatalf("SetItem(%d) err: %v", updateIndex, err)
}
item, err = RollingIndex.GetItem(updateIndex)
if err != nil {
t.Fatalf("GetItem(%d) err: %v", updateIndex, err)
}
if uv := item.(string); uv != updateValue {
t.Fatalf("Updated item %d should be %s, not %s", updateIndex, updateValue, uv)
}
}
func TestRollingIndexSkip(t *testing.T) {
size := 10
testSize := 25
RollingIndex := NewRollingIndex("test", size)
_, err := RollingIndex.Get(-1)
if err != nil {
t.Fatal(err)
}
items := []string{}
for i := 0; i < testSize; i++ {
item := fmt.Sprintf("item%d", i)
RollingIndex.Set(item, i)
items = append(items, item)
}
if _, err := RollingIndex.Get(0); err != nil && !errors.Is(err, errors.TooLate) {
t.Fatalf("Skipping index 0 should return ErrTooLate")
}
skipIndex1 := 9
expected1 := items[skipIndex1+1:]
cached1, err := RollingIndex.Get(skipIndex1)
if err != nil {
t.Fatal(err)
}
convertedItems := []string{}
for _, item := range cached1 {
convertedItems = append(convertedItems, item.(string))
}
if !reflect.DeepEqual(expected1, convertedItems) {
t.Fatalf("expected and cached not equal")
}
skipIndex2 := 15
expected2 := items[skipIndex2+1:]
cached2, err := RollingIndex.Get(skipIndex2)
if err != nil {
t.Fatal(err)
}
convertedItems = []string{}
for _, item := range cached2 {
convertedItems = append(convertedItems, item.(string))
}
if !reflect.DeepEqual(expected2, convertedItems) {
t.Fatalf("expected and cached not equal")
}
skipIndex3 := 27
expected3 := []string{}
cached3, err := RollingIndex.Get(skipIndex3)
if err != nil {
t.Fatal(err)
}
convertedItems = []string{}
for _, item := range cached3 {
convertedItems = append(convertedItems, item.(string))
}
if !reflect.DeepEqual(expected3, convertedItems) {
t.Fatalf("expected and cached not equal")
}
}