github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/disk_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 "bytes" 19 "context" 20 "io" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestDiskCache(t *testing.T) { 27 dir := t.TempDir() 28 ctx := context.Background() 29 30 // new 31 cache, err := NewDiskCache(dir, 1024) 32 assert.Nil(t, err) 33 34 // update 35 testUpdate := func(cache *DiskCache) { 36 vec := &IOVector{ 37 FilePath: "foo", 38 Entries: []IOEntry{ 39 { 40 Offset: 0, 41 Size: 1, 42 Data: []byte("a"), 43 }, 44 // no data 45 { 46 Offset: 98, 47 Size: 0, 48 }, 49 // size unknown 50 { 51 Offset: 9999, 52 Size: -1, 53 Data: []byte("abc"), 54 }, 55 }, 56 } 57 err = cache.Update(ctx, vec) 58 assert.Nil(t, err) 59 } 60 testUpdate(cache) 61 62 // update again 63 testUpdate(cache) 64 65 // read 66 testRead := func(cache *DiskCache) { 67 buf := new(bytes.Buffer) 68 var r io.ReadCloser 69 vec := &IOVector{ 70 FilePath: "foo", 71 Entries: []IOEntry{ 72 // written data 73 { 74 Offset: 0, 75 Size: 1, 76 WriterForRead: buf, 77 ReadCloserForRead: &r, 78 }, 79 // not exists 80 { 81 Offset: 1, 82 Size: 1, 83 }, 84 // bad offset 85 { 86 Offset: 999, 87 Size: 1, 88 }, 89 }, 90 } 91 err = cache.Read(ctx, vec) 92 assert.Nil(t, err) 93 defer r.Close() 94 assert.True(t, vec.Entries[0].done) 95 assert.Equal(t, []byte("a"), vec.Entries[0].Data) 96 assert.Equal(t, []byte("a"), buf.Bytes()) 97 bs, err := io.ReadAll(r) 98 assert.Nil(t, err) 99 assert.Equal(t, []byte("a"), bs) 100 assert.False(t, vec.Entries[1].done) 101 assert.False(t, vec.Entries[2].done) 102 } 103 testRead(cache) 104 105 // read again 106 testRead(cache) 107 108 // new cache instance and read 109 cache, err = NewDiskCache(dir, 1024) 110 assert.Nil(t, err) 111 testRead(cache) 112 113 // new cache instance and update 114 cache, err = NewDiskCache(dir, 1024) 115 assert.Nil(t, err) 116 testUpdate(cache) 117 118 }