github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/vfs/fd_test.go (about)

     1  // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package vfs
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestFileWrappersHaveFd(t *testing.T) {
    17  	// Use the real filesystem so that we can test vfs.Default, which returns
    18  	// files with Fd().
    19  	tmpf, err := ioutil.TempFile("", "bitalostable-db-fd-file")
    20  	require.NoError(t, err)
    21  	filename := tmpf.Name()
    22  	defer os.Remove(filename)
    23  
    24  	// File wrapper case 1: Check if diskHealthCheckingFile has Fd().
    25  	fs2, closer := WithDiskHealthChecks(Default, 10*time.Second,
    26  		func(s string, duration time.Duration) {})
    27  	defer closer.Close()
    28  	f2, err := fs2.Open(filename)
    29  	require.NoError(t, err)
    30  	if _, ok := f2.(fdGetter); !ok {
    31  		t.Fatal("expected diskHealthCheckingFile to export Fd() method")
    32  	}
    33  	// File wrapper case 2: Check if syncingFile has Fd().
    34  	f3 := NewSyncingFile(f2, SyncingFileOptions{BytesPerSync: 8 << 10 /* 8 KB */})
    35  	if _, ok := f3.(fdGetter); !ok {
    36  		t.Fatal("expected syncingFile to export Fd() method")
    37  	}
    38  	require.NoError(t, f2.Close())
    39  }