github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/mem_cache_test.go (about)

     1  // Copyright 2022 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 fileservice
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestMemCacheLeak(t *testing.T) {
    26  	ctx := context.Background()
    27  
    28  	fs, err := NewMemoryFS("test")
    29  	assert.Nil(t, err)
    30  	err = fs.Write(ctx, IOVector{
    31  		FilePath: "foo",
    32  		Entries: []IOEntry{
    33  			{
    34  				Size: 3,
    35  				Data: []byte("foo"),
    36  			},
    37  		},
    38  	})
    39  	assert.Nil(t, err)
    40  
    41  	m := NewMemCache(4)
    42  
    43  	vec := &IOVector{
    44  		FilePath: "foo",
    45  		Entries: []IOEntry{
    46  			{
    47  				Size: 3,
    48  				ToObject: func(reader io.Reader, data []byte) (any, int64, error) {
    49  					return 42, 1, nil
    50  				},
    51  			},
    52  		},
    53  	}
    54  	err = m.Read(ctx, vec)
    55  	assert.Nil(t, err)
    56  	err = fs.Read(ctx, vec)
    57  	assert.Nil(t, err)
    58  	err = m.Update(ctx, vec)
    59  	assert.Nil(t, err)
    60  	assert.Equal(t, int64(1), m.lru.size)
    61  	assert.Equal(t, int64(1), vec.Entries[0].ObjectSize)
    62  
    63  	// read from cache
    64  	vec = &IOVector{
    65  		FilePath: "foo",
    66  		Entries: []IOEntry{
    67  			{
    68  				Size: 3,
    69  				ToObject: func(reader io.Reader, data []byte) (any, int64, error) {
    70  					return 42, 1, nil
    71  				},
    72  			},
    73  		},
    74  	}
    75  	err = m.Read(ctx, vec)
    76  	assert.Nil(t, err)
    77  	err = fs.Read(ctx, vec)
    78  	assert.Nil(t, err)
    79  	err = m.Update(ctx, vec)
    80  	assert.Nil(t, err)
    81  	assert.Equal(t, int64(1), m.lru.size)
    82  	assert.Equal(t, int64(1), vec.Entries[0].ObjectSize)
    83  
    84  }