github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/mutable_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 "context" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func testMutableFileService( 25 t *testing.T, 26 newFS func() MutableFileService, 27 ) { 28 29 ctx := context.Background() 30 fs := newFS() 31 err := fs.Write(ctx, IOVector{ 32 FilePath: "foo", 33 Entries: []IOEntry{ 34 { 35 Size: 4, 36 Data: []byte("abcd"), 37 }, 38 }, 39 }) 40 assert.Nil(t, err) 41 42 mutator, err := fs.NewMutator(ctx, "foo") 43 assert.Nil(t, err) 44 defer func() { 45 assert.Nil(t, mutator.Close()) 46 }() 47 48 // overwrite 49 err = mutator.Mutate(ctx, IOEntry{ 50 Size: 3, 51 Data: []byte("123"), 52 }) 53 assert.Nil(t, err) 54 55 vec := &IOVector{ 56 FilePath: "foo", 57 Entries: []IOEntry{ 58 { 59 Offset: 0, 60 Size: 4, 61 }, 62 }, 63 } 64 err = fs.Read(ctx, vec) 65 assert.Nil(t, err) 66 assert.Equal(t, []byte("123d"), vec.Entries[0].Data) 67 68 // append 69 err = mutator.Mutate(ctx, IOEntry{ 70 Offset: 4, 71 Size: 1, 72 Data: []byte("q"), 73 }) 74 assert.Nil(t, err) 75 76 vec = &IOVector{ 77 FilePath: "foo", 78 Entries: []IOEntry{ 79 { 80 Offset: 0, 81 Size: 5, 82 }, 83 }, 84 } 85 err = fs.Read(ctx, vec) 86 assert.Nil(t, err) 87 assert.Equal(t, []byte("123dq"), vec.Entries[0].Data) 88 89 // append 90 err = mutator.Append(ctx, IOEntry{ 91 Offset: 0, 92 Size: 3, 93 Data: []byte("123"), 94 }) 95 assert.Nil(t, err) 96 97 vec = &IOVector{ 98 FilePath: "foo", 99 Entries: []IOEntry{ 100 { 101 Offset: 0, 102 Size: -1, 103 }, 104 }, 105 } 106 err = fs.Read(ctx, vec) 107 assert.Nil(t, err) 108 assert.Equal(t, []byte("123dq123"), vec.Entries[0].Data) 109 110 // context canceled 111 ctx, cancel := context.WithCancel(context.Background()) 112 cancel() 113 err = mutator.Mutate(ctx) 114 assert.ErrorIs(t, err, context.Canceled) 115 err = mutator.Append(ctx) 116 assert.ErrorIs(t, err, context.Canceled) 117 118 }