|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/list" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestEvictionPolicy(t *testing.T) { |
| 11 | + c := &cache{keyList: list.New()} |
| 12 | + EvictionPolicy(LeastRecentlyUsed)(c) |
| 13 | + accessed, added := c.recordAccess("foo"), c.recordAdd("foo") |
| 14 | + assert.NotNil(t, accessed) |
| 15 | + assert.Nil(t, added) |
| 16 | + |
| 17 | + c = &cache{keyList: list.New()} |
| 18 | + EvictionPolicy(LeastRecentlyAdded)(c) |
| 19 | + accessed, added = c.recordAccess("foo"), c.recordAdd("foo") |
| 20 | + assert.Nil(t, accessed) |
| 21 | + assert.NotNil(t, added) |
| 22 | +} |
| 23 | + |
| 24 | +func TestNew(t *testing.T) { |
| 25 | + optionApplied := false |
| 26 | + option := func(*cache) { |
| 27 | + optionApplied = true |
| 28 | + } |
| 29 | + |
| 30 | + c := New(314159, option).(*cache) |
| 31 | + |
| 32 | + assert.Equal(t, uint64(314159), c.cap) |
| 33 | + assert.Equal(t, uint64(0), c.size) |
| 34 | + assert.NotNil(t, c.items) |
| 35 | + assert.NotNil(t, c.keyList) |
| 36 | + assert.True(t, optionApplied) |
| 37 | + |
| 38 | + accessed, added := c.recordAccess("foo"), c.recordAdd("foo") |
| 39 | + assert.NotNil(t, accessed) |
| 40 | + assert.Nil(t, added) |
| 41 | +} |
| 42 | + |
| 43 | +type testItem uint64 |
| 44 | + |
| 45 | +func (ti testItem) Size() uint64 { |
| 46 | + return uint64(ti) |
| 47 | +} |
| 48 | + |
| 49 | +func TestPutGetRemoveSize(t *testing.T) { |
| 50 | + keys := []string{"foo", "bar", "baz"} |
| 51 | + testCases := []struct { |
| 52 | + label string |
| 53 | + cache Cache |
| 54 | + useCache func(c Cache) |
| 55 | + expectedSize uint64 |
| 56 | + expectedItems []Item |
| 57 | + }{{ |
| 58 | + label: "Items added, key doesn't exist", |
| 59 | + cache: New(10000), |
| 60 | + useCache: func(c Cache) { |
| 61 | + c.Put("foo", testItem(1)) |
| 62 | + }, |
| 63 | + expectedSize: 1, |
| 64 | + expectedItems: []Item{testItem(1), nil, nil}, |
| 65 | + }, { |
| 66 | + label: "Items added, key exists", |
| 67 | + cache: New(10000), |
| 68 | + useCache: func(c Cache) { |
| 69 | + c.Put("foo", testItem(1)) |
| 70 | + c.Put("foo", testItem(10)) |
| 71 | + }, |
| 72 | + expectedSize: 10, |
| 73 | + expectedItems: []Item{testItem(10), nil, nil}, |
| 74 | + }, { |
| 75 | + label: "Items added, LRA eviction", |
| 76 | + cache: New(2, EvictionPolicy(LeastRecentlyAdded)), |
| 77 | + useCache: func(c Cache) { |
| 78 | + c.Put("foo", testItem(1)) |
| 79 | + c.Put("bar", testItem(1)) |
| 80 | + c.Get("foo") |
| 81 | + c.Put("baz", testItem(1)) |
| 82 | + }, |
| 83 | + expectedSize: 2, |
| 84 | + expectedItems: []Item{nil, testItem(1), testItem(1)}, |
| 85 | + }, { |
| 86 | + label: "Items added, LRU eviction", |
| 87 | + cache: New(2, EvictionPolicy(LeastRecentlyUsed)), |
| 88 | + useCache: func(c Cache) { |
| 89 | + c.Put("foo", testItem(1)) |
| 90 | + c.Put("bar", testItem(1)) |
| 91 | + c.Get("foo") |
| 92 | + c.Put("baz", testItem(1)) |
| 93 | + }, |
| 94 | + expectedSize: 2, |
| 95 | + expectedItems: []Item{testItem(1), nil, testItem(1)}, |
| 96 | + }, { |
| 97 | + label: "Items removed, key doesn't exist", |
| 98 | + cache: New(10000), |
| 99 | + useCache: func(c Cache) { |
| 100 | + c.Put("foo", testItem(1)) |
| 101 | + c.Remove("baz") |
| 102 | + }, |
| 103 | + expectedSize: 1, |
| 104 | + expectedItems: []Item{testItem(1), nil, nil}, |
| 105 | + }, { |
| 106 | + label: "Items removed, key exists", |
| 107 | + cache: New(10000), |
| 108 | + useCache: func(c Cache) { |
| 109 | + c.Put("foo", testItem(1)) |
| 110 | + c.Remove("foo") |
| 111 | + }, |
| 112 | + expectedSize: 0, |
| 113 | + expectedItems: []Item{nil, nil, nil}, |
| 114 | + }} |
| 115 | + |
| 116 | + for _, testCase := range testCases { |
| 117 | + t.Log(testCase.label) |
| 118 | + testCase.useCache(testCase.cache) |
| 119 | + assert.Equal(t, testCase.expectedSize, testCase.cache.Size()) |
| 120 | + assert.Equal(t, testCase.expectedItems, testCase.cache.Get(keys...)) |
| 121 | + } |
| 122 | +} |
0 commit comments