github.com/dolthub/go-mysql-server@v0.18.0/sql/cache_test.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sql 16 17 import ( 18 "errors" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestLRUCache(t *testing.T) { 25 t.Run("basic methods", func(t *testing.T) { 26 require := require.New(t) 27 28 cache := newLRUCache(mockMemory{}, fixedReporter(5, 50), 10) 29 30 require.NoError(cache.Put(1, "foo")) 31 v, err := cache.Get(1) 32 require.NoError(err) 33 require.Equal("foo", v) 34 35 _, err = cache.Get(2) 36 require.Error(err) 37 require.True(errors.Is(err, ErrKeyNotFound)) 38 39 // Free the cache and check previous entry disappeared. 40 cache.Free() 41 42 _, err = cache.Get(1) 43 require.Error(err) 44 require.True(errors.Is(err, ErrKeyNotFound)) 45 46 cache.Dispose() 47 require.Panics(func() { 48 _, _ = cache.Get(1) 49 }) 50 }) 51 52 t.Run("no memory available", func(t *testing.T) { 53 require := require.New(t) 54 cache := newLRUCache(mockMemory{}, fixedReporter(51, 50), 5) 55 56 require.NoError(cache.Put(1, "foo")) 57 _, err := cache.Get(1) 58 require.Error(err) 59 require.True(errors.Is(err, ErrKeyNotFound)) 60 }) 61 62 t.Run("free required to add entry", func(t *testing.T) { 63 require := require.New(t) 64 var freed bool 65 cache := newLRUCache( 66 mockMemory{func() { 67 freed = true 68 }}, 69 mockReporter{func() uint64 { 70 if freed { 71 return 0 72 } 73 return 51 74 }, 50}, 75 5, 76 ) 77 require.NoError(cache.Put(1, "foo")) 78 v, err := cache.Get(1) 79 require.NoError(err) 80 require.Equal("foo", v) 81 require.True(freed) 82 }) 83 } 84 85 func TestHistoryCache(t *testing.T) { 86 t.Run("basic methods", func(t *testing.T) { 87 require := require.New(t) 88 89 cache := newHistoryCache(mockMemory{}, fixedReporter(5, 50)) 90 91 require.NoError(cache.Put(1, "foo")) 92 v, err := cache.Get(1) 93 require.NoError(err) 94 require.Equal("foo", v) 95 96 _, err = cache.Get(2) 97 require.Error(err) 98 require.True(errors.Is(err, ErrKeyNotFound)) 99 100 cache.Dispose() 101 require.Panics(func() { 102 _ = cache.Put(2, "foo") 103 }) 104 }) 105 106 t.Run("no memory available", func(t *testing.T) { 107 require := require.New(t) 108 cache := newHistoryCache(mockMemory{}, fixedReporter(51, 50)) 109 110 err := cache.Put(1, "foo") 111 require.Error(err) 112 require.True(ErrNoMemoryAvailable.Is(err)) 113 }) 114 115 t.Run("free required to add entry", func(t *testing.T) { 116 require := require.New(t) 117 var freed bool 118 cache := newHistoryCache( 119 mockMemory{func() { 120 freed = true 121 }}, 122 mockReporter{func() uint64 { 123 if freed { 124 return 0 125 } 126 return 51 127 }, 50}, 128 ) 129 require.NoError(cache.Put(1, "foo")) 130 v, err := cache.Get(1) 131 require.NoError(err) 132 require.Equal("foo", v) 133 require.True(freed) 134 }) 135 } 136 137 func TestRowsCache(t *testing.T) { 138 t.Run("basic methods", func(t *testing.T) { 139 require := require.New(t) 140 141 cache := newRowsCache(mockMemory{}, fixedReporter(5, 50)) 142 143 require.NoError(cache.Add(Row{1})) 144 require.Len(cache.Get(), 1) 145 146 cache.Dispose() 147 require.Panics(func() { 148 _ = cache.Add(Row{2}) 149 }) 150 }) 151 152 t.Run("no memory available", func(t *testing.T) { 153 require := require.New(t) 154 cache := newRowsCache(mockMemory{}, fixedReporter(51, 50)) 155 156 err := cache.Add(Row{1, "foo"}) 157 require.Error(err) 158 require.True(ErrNoMemoryAvailable.Is(err)) 159 }) 160 161 t.Run("free required to add entry", func(t *testing.T) { 162 require := require.New(t) 163 var freed bool 164 cache := newRowsCache( 165 mockMemory{func() { 166 freed = true 167 }}, 168 mockReporter{func() uint64 { 169 if freed { 170 return 0 171 } 172 return 51 173 }, 50}, 174 ) 175 require.NoError(cache.Add(Row{1, "foo"})) 176 require.Len(cache.Get(), 1) 177 require.True(freed) 178 }) 179 }