github.com/dolthub/go-mysql-server@v0.18.0/sql/memory_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 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestManager(t *testing.T) { 24 require := require.New(t) 25 m := NewMemoryManager(nil) 26 27 kv, dispose := m.NewLRUCache(5) 28 _, ok := kv.(*lruCache) 29 require.True(ok) 30 require.Len(m.caches, 1) 31 dispose() 32 require.Len(m.caches, 0) 33 34 kv, dispose = m.NewHistoryCache() 35 _, ok = kv.(*historyCache) 36 require.True(ok) 37 require.Len(m.caches, 1) 38 dispose() 39 require.Len(m.caches, 0) 40 41 rc, dispose := m.NewRowsCache() 42 _, ok = rc.(*rowsCache) 43 require.True(ok) 44 require.Len(m.caches, 1) 45 dispose() 46 require.Len(m.caches, 0) 47 48 m.addCache(disposableCache{}) 49 f := new(freeableCache) 50 m.addCache(f) 51 m.Free() 52 require.True(f.freed) 53 } 54 55 type disposableCache struct{} 56 57 func (d disposableCache) Dispose() {} 58 59 type freeableCache struct { 60 disposableCache 61 freed bool 62 } 63 64 func (f *freeableCache) Free() { f.freed = true } 65 66 func TestHasAvailable(t *testing.T) { 67 require.True(t, HasAvailableMemory(fixedReporter(2, 5))) 68 require.False(t, HasAvailableMemory(fixedReporter(6, 5))) 69 } 70 71 type mockReporter struct { 72 f func() uint64 73 max uint64 74 } 75 76 func (m mockReporter) UsedMemory() uint64 { return m.f() } 77 func (m mockReporter) MaxMemory() uint64 { return m.max } 78 79 func fixedReporter(v, max uint64) mockReporter { 80 return mockReporter{func() uint64 { 81 return v 82 }, max} 83 } 84 85 type mockMemory struct { 86 f func() 87 } 88 89 func (m mockMemory) Free() { 90 if m.f != nil { 91 m.f() 92 } 93 }