github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/persist/fs/write_test.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package fs
    22  
    23  import (
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/m3db/m3/src/dbnode/persist"
    30  	"github.com/m3db/m3/src/x/ident"
    31  	xtime "github.com/m3db/m3/src/x/time"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  // TestWriteReuseAfterError was added as a regression test after it was
    36  // discovered that reusing a fileset writer after a called to Close() had
    37  // returned an error could make the fileset writer end up in a near infinite
    38  // loop when it was reused to write out a completely indepedent set of files.
    39  //
    40  // This test verifies that the fix works as expected and prevents regressions
    41  // of the issue.
    42  func TestWriteReuseAfterError(t *testing.T) {
    43  	dir := createTempDir(t)
    44  	filePathPrefix := filepath.Join(dir, "")
    45  	defer os.RemoveAll(dir)
    46  
    47  	w := newTestWriter(t, filePathPrefix)
    48  	writerOpts := DataWriterOpenOptions{
    49  		Identifier: FileSetFileIdentifier{
    50  			Namespace:   testNs1ID,
    51  			Shard:       0,
    52  			BlockStart:  xtime.Now().Truncate(time.Hour),
    53  			VolumeIndex: 0,
    54  		},
    55  		BlockSize:   time.Hour,
    56  		FileSetType: persist.FileSetFlushType,
    57  	}
    58  	data := checkedBytes([]byte{1, 2, 3})
    59  
    60  	require.NoError(t, w.Open(writerOpts))
    61  	require.NoError(t, w.Write(
    62  		persist.NewMetadataFromIDAndTags(
    63  			ident.StringID("series1"),
    64  			ident.Tags{},
    65  			persist.MetadataOptions{}),
    66  		data,
    67  		0))
    68  	require.NoError(t, w.Write(
    69  		persist.NewMetadataFromIDAndTags(
    70  			ident.StringID("series1"),
    71  			ident.Tags{},
    72  			persist.MetadataOptions{}),
    73  		data,
    74  		0))
    75  	require.Error(t, w.Close())
    76  
    77  	require.NoError(t, w.Open(writerOpts))
    78  	require.NoError(t, w.Close())
    79  }