github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/fifocache/fifo_test.go (about)

     1  // Copyright 2024 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 fifocache
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestCacheSetGet(t *testing.T) {
    24  	cache := New[int, int](8, nil, ShardInt[int])
    25  
    26  	cache.Set(1, 1, 1)
    27  	n, ok := cache.Get(1)
    28  	assert.True(t, ok)
    29  	assert.Equal(t, 1, n)
    30  
    31  	cache.Set(1, 1, 1)
    32  	n, ok = cache.Get(1)
    33  	assert.True(t, ok)
    34  	assert.Equal(t, 1, n)
    35  
    36  	_, ok = cache.Get(2)
    37  	assert.False(t, ok)
    38  }
    39  
    40  func TestCacheEvict(t *testing.T) {
    41  	cache := New[int, int](8, nil, ShardInt[int])
    42  	for i := 0; i < 64; i++ {
    43  		cache.Set(i, i, 1)
    44  		if cache.used1+cache.used2 > cache.capacity {
    45  			t.Fatalf("capacity %v, used1 %v used2 %v", cache.capacity, cache.used1, cache.used2)
    46  		}
    47  	}
    48  }
    49  
    50  func TestCacheEvict2(t *testing.T) {
    51  	cache := New[int, int](2, nil, ShardInt[int])
    52  	cache.Set(1, 1, 1)
    53  	cache.Set(2, 2, 1)
    54  
    55  	// 1 will be evicted
    56  	cache.Set(3, 3, 1)
    57  	v, ok := cache.Get(2)
    58  	assert.True(t, ok)
    59  	assert.Equal(t, 2, v)
    60  	v, ok = cache.Get(3)
    61  	assert.True(t, ok)
    62  	assert.Equal(t, 3, v)
    63  
    64  	// get 2, set 4, 3 will be evicted first
    65  	cache.Get(2)
    66  	cache.Get(2)
    67  	cache.Set(4, 4, 1)
    68  	v, ok = cache.Get(2)
    69  	assert.True(t, ok)
    70  	assert.Equal(t, 2, v)
    71  	v, ok = cache.Get(4)
    72  	assert.True(t, ok)
    73  	assert.Equal(t, 4, v)
    74  	assert.Equal(t, 1, cache.used1)
    75  	assert.Equal(t, 1, cache.used2)
    76  }
    77  
    78  func TestCacheEvict3(t *testing.T) {
    79  	nEvict := 0
    80  	cache := New[int, bool](1024, func(_ int, _ bool) {
    81  		nEvict++
    82  	}, ShardInt[int])
    83  	for i := 0; i < 1024; i++ {
    84  		cache.Set(i, true, 1)
    85  		cache.Get(i)
    86  		cache.Get(i)
    87  		assert.True(t, cache.used1+cache.used2 <= 1024)
    88  		//assert.True(t, len(cache.values) <= 1024)
    89  	}
    90  	assert.Equal(t, 0, nEvict)
    91  
    92  	for i := 0; i < 1024; i++ {
    93  		cache.Set(10000+i, true, 1)
    94  		assert.True(t, cache.used1+cache.used2 <= 1024)
    95  		//assert.True(t, len(cache.values) <= 1024)
    96  	}
    97  	assert.Equal(t, 102, cache.used1)
    98  	assert.Equal(t, 922, cache.used2)
    99  	assert.Equal(t, 1024, nEvict)
   100  }