github.com/matrixorigin/matrixone@v0.7.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  	"github.com/stretchr/testify/require"
    19  	"testing"
    20  )
    21  
    22  func Test_BasicGet(t *testing.T) {
    23  	pc := newPlanCache(5)
    24  
    25  	pc.cache("abc", nil, nil)
    26  	require.True(t, pc.isCached("abc"))
    27  	require.Equal(t, pc.get("abc").sql, "abc")
    28  
    29  	pc.cache("abcd", nil, nil)
    30  	require.True(t, pc.isCached("abcd"))
    31  	require.Equal(t, pc.get("abcd").sql, "abcd")
    32  
    33  	require.False(t, pc.isCached("abcde"))
    34  }
    35  
    36  func Test_LRU(t *testing.T) {
    37  	pc := newPlanCache(3)
    38  
    39  	pc.cache("1", nil, nil)
    40  	pc.cache("2", nil, nil)
    41  	pc.cache("3", nil, nil)
    42  	require.True(t, pc.isCached("1"))
    43  	require.True(t, pc.isCached("2"))
    44  	require.True(t, pc.isCached("3"))
    45  
    46  	pc.cache("4", nil, nil)
    47  	require.True(t, pc.isCached("4"))
    48  	require.False(t, pc.isCached("1"))
    49  
    50  	require.Equal(t, pc.get("2").sql, "2")
    51  	pc.cache("5", nil, nil)
    52  	require.True(t, pc.isCached("5"))
    53  	require.True(t, pc.isCached("4"))
    54  	require.True(t, pc.isCached("2"))
    55  	require.False(t, pc.isCached("3"))
    56  }
    57  
    58  func Test_CleanCache(t *testing.T) {
    59  	pc := newPlanCache(3)
    60  
    61  	pc.cache("1", nil, nil)
    62  	pc.cache("2", nil, nil)
    63  	pc.cache("3", nil, nil)
    64  	require.True(t, pc.isCached("1"))
    65  	require.True(t, pc.isCached("2"))
    66  	require.True(t, pc.isCached("3"))
    67  
    68  	pc.clean()
    69  
    70  	require.False(t, pc.isCached("1"))
    71  	require.False(t, pc.isCached("2"))
    72  	require.False(t, pc.isCached("3"))
    73  }