| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package cacher |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
) |
| 24 |
|
| 25 |
func TestStoreListOrdered(t *testing.T) { |
| 26 |
store := newThreadedBtreeStoreIndexer(nil, btreeDegree) |
| 27 |
assert.NoError(t, store.Add(testStorageElement("foo3", "bar3", 1))) |
| 28 |
assert.NoError(t, store.Add(testStorageElement("foo1", "bar2", 2))) |
| 29 |
assert.NoError(t, store.Add(testStorageElement("foo2", "bar1", 3))) |
| 30 |
assert.Equal(t, []interface{}{ |
| 31 |
testStorageElement("foo1", "bar2", 2), |
| 32 |
testStorageElement("foo2", "bar1", 3), |
| 33 |
testStorageElement("foo3", "bar3", 1), |
| 34 |
}, store.List()) |
| 35 |
} |
| 36 |
|
| 37 |
func TestStoreListPrefix(t *testing.T) { |
| 38 |
store := newThreadedBtreeStoreIndexer(nil, btreeDegree) |
| 39 |
assert.NoError(t, store.Add(testStorageElement("foo3", "bar3", 1))) |
| 40 |
assert.NoError(t, store.Add(testStorageElement("foo1", "bar2", 2))) |
| 41 |
assert.NoError(t, store.Add(testStorageElement("foo2", "bar1", 3))) |
| 42 |
assert.NoError(t, store.Add(testStorageElement("bar", "baz", 4))) |
| 43 |
|
| 44 |
items := store.ListPrefix("foo", "") |
| 45 |
assert.Equal(t, []interface{}{ |
| 46 |
testStorageElement("foo1", "bar2", 2), |
| 47 |
testStorageElement("foo2", "bar1", 3), |
| 48 |
testStorageElement("foo3", "bar3", 1), |
| 49 |
}, items) |
| 50 |
|
| 51 |
items = store.ListPrefix("foo2", "") |
| 52 |
assert.Equal(t, []interface{}{ |
| 53 |
testStorageElement("foo2", "bar1", 3), |
| 54 |
}, items) |
| 55 |
|
| 56 |
items = store.ListPrefix("foo", "foo1\x00") |
| 57 |
assert.Equal(t, []interface{}{ |
| 58 |
testStorageElement("foo2", "bar1", 3), |
| 59 |
testStorageElement("foo3", "bar3", 1), |
| 60 |
}, items) |
| 61 |
|
| 62 |
items = store.ListPrefix("foo", "foo2\x00") |
| 63 |
assert.Equal(t, []interface{}{ |
| 64 |
testStorageElement("foo3", "bar3", 1), |
| 65 |
}, items) |
| 66 |
|
| 67 |
items = store.ListPrefix("bar", "") |
| 68 |
assert.Equal(t, []interface{}{ |
| 69 |
testStorageElement("bar", "baz", 4), |
| 70 |
}, items) |
| 71 |
} |
| 72 |
|
| 73 |
func TestStoreSnapshotter(t *testing.T) { |
| 74 |
cache := newStoreSnapshotter() |
| 75 |
cache.Add(10, fakeOrderedLister{rv: 10}) |
| 76 |
cache.Add(20, fakeOrderedLister{rv: 20}) |
| 77 |
cache.Add(30, fakeOrderedLister{rv: 30}) |
| 78 |
cache.Add(40, fakeOrderedLister{rv: 40}) |
| 79 |
assert.Equal(t, 4, cache.snapshots.Len()) |
| 80 |
|
| 81 |
t.Log("No snapshot from before first RV") |
| 82 |
_, found := cache.GetLessOrEqual(9) |
| 83 |
assert.False(t, found) |
| 84 |
|
| 85 |
t.Log("Get snapshot from first RV") |
| 86 |
snapshot, found := cache.GetLessOrEqual(10) |
| 87 |
assert.True(t, found) |
| 88 |
assert.Equal(t, 10, snapshot.(fakeOrderedLister).rv) |
| 89 |
|
| 90 |
t.Log("Get first snapshot by larger RV") |
| 91 |
snapshot, found = cache.GetLessOrEqual(11) |
| 92 |
assert.True(t, found) |
| 93 |
assert.Equal(t, 10, snapshot.(fakeOrderedLister).rv) |
| 94 |
|
| 95 |
t.Log("Get second snapshot by larger RV") |
| 96 |
snapshot, found = cache.GetLessOrEqual(22) |
| 97 |
assert.True(t, found) |
| 98 |
assert.Equal(t, 20, snapshot.(fakeOrderedLister).rv) |
| 99 |
|
| 100 |
t.Log("Get third snapshot for future revision") |
| 101 |
snapshot, found = cache.GetLessOrEqual(100) |
| 102 |
assert.True(t, found) |
| 103 |
assert.Equal(t, 40, snapshot.(fakeOrderedLister).rv) |
| 104 |
|
| 105 |
t.Log("Remove snapshot less than 30") |
| 106 |
cache.RemoveLess(30) |
| 107 |
|
| 108 |
assert.Equal(t, 2, cache.snapshots.Len()) |
| 109 |
_, found = cache.GetLessOrEqual(10) |
| 110 |
assert.False(t, found) |
| 111 |
|
| 112 |
_, found = cache.GetLessOrEqual(20) |
| 113 |
assert.False(t, found) |
| 114 |
|
| 115 |
snapshot, found = cache.GetLessOrEqual(30) |
| 116 |
assert.True(t, found) |
| 117 |
assert.Equal(t, 30, snapshot.(fakeOrderedLister).rv) |
| 118 |
|
| 119 |
t.Log("Remove removing all RVs") |
| 120 |
cache.Reset() |
| 121 |
assert.Equal(t, 0, cache.snapshots.Len()) |
| 122 |
_, found = cache.GetLessOrEqual(30) |
| 123 |
assert.False(t, found) |
| 124 |
_, found = cache.GetLessOrEqual(40) |
| 125 |
assert.False(t, found) |
| 126 |
} |
| 127 |
|
| 128 |
type fakeOrderedLister struct { |
| 129 |
rv int |
| 130 |
} |
| 131 |
|
| 132 |
func (f fakeOrderedLister) Add(obj interface{}) error { return nil } |
| 133 |
func (f fakeOrderedLister) Update(obj interface{}) error { return nil } |
| 134 |
func (f fakeOrderedLister) Delete(obj interface{}) error { return nil } |
| 135 |
func (f fakeOrderedLister) Clone() orderedLister { return f } |
| 136 |
func (f fakeOrderedLister) ListPrefix(prefixKey, continueKey string) []interface{} { |
| 137 |
return nil |
| 138 |
} |
| 139 |
func (f fakeOrderedLister) Count(prefixKey, continueKey string) int { return 0 } |
| 140 |
|
| 141 |
type fakeSnapshotter struct { |
| 142 |
getLessOrEqual func(rv uint64) (orderedLister, bool) |
| 143 |
} |
| 144 |
|
| 145 |
var _ Snapshotter = (*fakeSnapshotter)(nil) |
| 146 |
|
| 147 |
func (f *fakeSnapshotter) Reset() {} |
| 148 |
func (f *fakeSnapshotter) GetLessOrEqual(rv uint64) (orderedLister, bool) { |
| 149 |
if f.getLessOrEqual == nil { |
| 150 |
return nil, false |
| 151 |
} |
| 152 |
return f.getLessOrEqual(rv) |
| 153 |
} |
| 154 |
func (f *fakeSnapshotter) Add(rv uint64, indexer orderedLister) {} |
| 155 |
func (f *fakeSnapshotter) RemoveLess(rv uint64) {} |
| 156 |
func (f *fakeSnapshotter) Len() int { |
| 157 |
return 0 |
| 158 |
} |