github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/ledger/complete/wal/syncrename_test.go (about)

     1  package wal
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/rs/zerolog"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func Test_RenameHappensAfterClosing(t *testing.T) {
    16  
    17  	unittest.RunWithTempDir(t, func(dir string) {
    18  
    19  		filename := "target.filename"
    20  		tmpName := "tmp.filename"
    21  		fullFileName := path.Join(dir, filename)
    22  		fullTmpName := path.Join(dir, tmpName)
    23  
    24  		require.NoFileExists(t, fullFileName)
    25  		require.NoFileExists(t, fullTmpName)
    26  
    27  		file, err := os.Create(fullTmpName)
    28  		require.NoError(t, err)
    29  
    30  		writer := bufio.NewWriter(file)
    31  
    32  		logger := zerolog.Nop()
    33  		syncer := &SyncOnCloseRenameFile{
    34  			file:       file,
    35  			targetName: fullFileName,
    36  			Writer:     writer,
    37  			logger:     logger,
    38  		}
    39  
    40  		sampleBytes := []byte{2, 1, 3, 7}
    41  		_, err = syncer.Write(sampleBytes)
    42  		require.NoError(t, err)
    43  
    44  		require.FileExists(t, fullTmpName)
    45  		require.NoFileExists(t, fullFileName)
    46  
    47  		err = syncer.Close()
    48  		require.NoError(t, err)
    49  
    50  		require.NoFileExists(t, fullTmpName)
    51  		require.FileExists(t, fullFileName)
    52  
    53  		readBytes, err := os.ReadFile(fullFileName)
    54  		require.NoError(t, err)
    55  
    56  		require.Equal(t, readBytes, sampleBytes)
    57  	})
    58  }