github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/vfs/fd_test.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 vfs 16 17 import ( 18 "os" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestFileWrappersHaveFd(t *testing.T) { 26 // Use the real filesystem so that we can test vfs.Default, which returns 27 // files with Fd(). 28 tmpf, err := os.CreateTemp("", "bitalosdb-db-fd-file") 29 require.NoError(t, err) 30 filename := tmpf.Name() 31 defer os.Remove(filename) 32 33 // File wrapper case 1: Check if diskHealthCheckingFile has Fd(). 34 fs2 := WithDiskHealthChecks(Default, 10*time.Second, func(s string, duration time.Duration) {}) 35 f2, err := fs2.Open(filename) 36 require.NoError(t, err) 37 if _, ok := f2.(fdGetter); !ok { 38 t.Fatal("expected diskHealthCheckingFile to export Fd() method") 39 } 40 // File wrapper case 2: Check if syncingFile has Fd(). 41 f3 := NewSyncingFile(f2, SyncingFileOptions{BytesPerSync: 8 << 10 /* 8 KB */}) 42 if _, ok := f3.(fdGetter); !ok { 43 t.Fatal("expected syncingFile to export Fd() method") 44 } 45 require.NoError(t, f2.Close()) 46 }