github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/plan_cache_test.go (about) 1 // Copyright 2021 Matrix Origin 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 frontend 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func Test_BasicGet(t *testing.T) { 24 pc := newPlanCache(5) 25 26 pc.cache("abc", nil, nil) 27 require.True(t, pc.isCached("abc")) 28 require.Equal(t, pc.get("abc").sql, "abc") 29 30 pc.cache("abcd", nil, nil) 31 require.True(t, pc.isCached("abcd")) 32 require.Equal(t, pc.get("abcd").sql, "abcd") 33 34 require.False(t, pc.isCached("abcde")) 35 } 36 37 func Test_LRU(t *testing.T) { 38 pc := newPlanCache(3) 39 40 pc.cache("1", nil, nil) 41 pc.cache("2", nil, nil) 42 pc.cache("3", nil, nil) 43 require.True(t, pc.isCached("1")) 44 require.True(t, pc.isCached("2")) 45 require.True(t, pc.isCached("3")) 46 47 pc.cache("4", nil, nil) 48 require.True(t, pc.isCached("4")) 49 require.False(t, pc.isCached("1")) 50 51 require.Equal(t, pc.get("2").sql, "2") 52 pc.cache("5", nil, nil) 53 require.True(t, pc.isCached("5")) 54 require.True(t, pc.isCached("4")) 55 require.True(t, pc.isCached("2")) 56 require.False(t, pc.isCached("3")) 57 } 58 59 func Test_CleanCache(t *testing.T) { 60 pc := newPlanCache(3) 61 62 pc.cache("1", nil, nil) 63 pc.cache("2", nil, nil) 64 pc.cache("3", nil, nil) 65 pc.cache("4", nil, nil) 66 require.False(t, pc.isCached("1")) 67 require.True(t, pc.isCached("2")) 68 require.True(t, pc.isCached("3")) 69 require.True(t, pc.isCached("4")) 70 71 pc.clean() 72 73 require.Nil(t, pc.get("1")) 74 require.Nil(t, pc.get("2")) 75 require.Nil(t, pc.get("3")) 76 require.Nil(t, pc.get("4")) 77 require.False(t, pc.isCached("1")) 78 require.False(t, pc.isCached("2")) 79 require.False(t, pc.isCached("3")) 80 require.False(t, pc.isCached("3")) 81 82 pc.cache("1", nil, nil) 83 pc.cache("2", nil, nil) 84 pc.cache("3", nil, nil) 85 pc.cache("4", nil, nil) 86 require.False(t, pc.isCached("1")) 87 require.True(t, pc.isCached("2")) 88 require.True(t, pc.isCached("3")) 89 require.True(t, pc.isCached("4")) 90 require.Nil(t, pc.get("1")) 91 require.NotNil(t, pc.get("2")) 92 require.NotNil(t, pc.get("3")) 93 require.NotNil(t, pc.get("4")) 94 }