github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/caching_file_service_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  	"bytes"
    19  	"context"
    20  	"encoding/gob"
    21  	"io"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func testCachingFileService(
    28  	t *testing.T,
    29  	newFS func() CachingFileService,
    30  ) {
    31  
    32  	fs := newFS()
    33  	ctx := context.Background()
    34  
    35  	buf := new(bytes.Buffer)
    36  	err := gob.NewEncoder(buf).Encode(map[int]int{
    37  		42: 42,
    38  	})
    39  	assert.Nil(t, err)
    40  	data := buf.Bytes()
    41  
    42  	err = fs.Write(ctx, IOVector{
    43  		FilePath: "foo",
    44  		Entries: []IOEntry{
    45  			{
    46  				Size: int64(len(data)),
    47  				Data: data,
    48  			},
    49  		},
    50  	})
    51  	assert.Nil(t, err)
    52  
    53  	vec := &IOVector{
    54  		FilePath: "foo",
    55  		Entries: []IOEntry{
    56  			{
    57  				Size: int64(len(data)),
    58  				ToObject: func(r io.Reader, data []byte) (any, int64, error) {
    59  					bs, err := io.ReadAll(r)
    60  					assert.Nil(t, err)
    61  					if len(data) > 0 {
    62  						assert.Equal(t, bs, data)
    63  					}
    64  					var m map[int]int
    65  					if err := gob.NewDecoder(bytes.NewReader(bs)).Decode(&m); err != nil {
    66  						return nil, 0, err
    67  					}
    68  					return m, 1, nil
    69  				},
    70  			},
    71  		},
    72  	}
    73  
    74  	err = fs.Read(ctx, vec)
    75  	assert.Nil(t, err)
    76  	m, ok := vec.Entries[0].Object.(map[int]int)
    77  	assert.True(t, ok)
    78  	assert.Equal(t, 1, len(m))
    79  	assert.Equal(t, 42, m[42])
    80  	assert.Equal(t, int64(1), vec.Entries[0].ObjectSize)
    81  
    82  	// read again
    83  	err = fs.Read(ctx, vec)
    84  	assert.Nil(t, err)
    85  	m, ok = vec.Entries[0].Object.(map[int]int)
    86  	assert.True(t, ok)
    87  	assert.Equal(t, 1, len(m))
    88  	assert.Equal(t, 42, m[42])
    89  	assert.Equal(t, int64(1), vec.Entries[0].ObjectSize)
    90  
    91  	stats := fs.CacheStats()
    92  	assert.Equal(t, stats.NumRead, int64(2))
    93  	assert.Equal(t, stats.NumHit, int64(1))
    94  
    95  	// flush
    96  	fs.FlushCache()
    97  
    98  }