[no-release-notes] go/store/nbs: Close files in more places to tests can run faster on windows and cleanup after themselves.

This commit is contained in:
Aaron Son
2023-03-01 11:15:24 -08:00
parent 39fc9f3a26
commit 293413e1bd
6 changed files with 36 additions and 8 deletions

View File

@@ -108,8 +108,9 @@ func (suite *BlockStoreSuite) TestChunkStoreMissingDir() {
func (suite *BlockStoreSuite) TestChunkStoreNotDir() {
existingFile := filepath.Join(suite.dir, "path-exists-but-is-a-file")
_, err := os.Create(existingFile)
f, err := os.Create(existingFile)
suite.NoError(err)
defer f.Close()
_, err = NewLocalStore(context.Background(), constants.FormatDefaultString, existingFile, testMemTableSize, NewUnlimitedMemQuotaProvider())
suite.Error(err)

View File

@@ -183,10 +183,17 @@ func TestFSTablePersisterConjoinAllDups(t *testing.T) {
sources[2], err = sources[0].clone()
require.NoError(t, err)
src, _, err := fts.ConjoinAll(ctx, sources, &Stats{})
src, cleanup, err := fts.ConjoinAll(ctx, sources, &Stats{})
require.NoError(t, err)
defer src.close()
// After ConjoinAll runs, we can close the sources and
// call the cleanup func.
for _, s := range sources {
s.close()
}
cleanup()
if assert.True(mustUint32(src.count()) > 0) {
buff, err := os.ReadFile(filepath.Join(dir, src.hash().String()))
require.NoError(t, err)

View File

@@ -117,7 +117,7 @@ func (s journalChunkSource) hash() addr {
// reader implements chunkSource.
func (s journalChunkSource) reader(context.Context) (io.ReadCloser, uint64, error) {
rdr, sz, err := s.journal.snapshot()
return io.NopCloser(rdr), uint64(sz), err
return rdr, uint64(sz), err
}
func (s journalChunkSource) getRecordRanges(requests []getRecord) (map[hash.Hash]Range, error) {

View File

@@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/dolthub/dolt/go/libraries/utils/file"
"github.com/dolthub/dolt/go/store/chunks"
"github.com/dolthub/dolt/go/store/types"
)
@@ -33,6 +34,7 @@ func makeTestChunkJournal(t *testing.T) *chunkJournal {
ctx := context.Background()
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
t.Cleanup(func() { file.RemoveAll(dir) })
m, err := getFileManifest(ctx, dir, syncFlush)
require.NoError(t, err)
q := NewUnlimitedMemQuotaProvider()
@@ -40,6 +42,7 @@ func makeTestChunkJournal(t *testing.T) *chunkJournal {
nbf := types.Format_Default.VersionString()
j, err := newChunkJournal(ctx, nbf, dir, m, p.(*fsTablePersister))
require.NoError(t, err)
t.Cleanup(func() { j.Close() })
return j
}
@@ -98,6 +101,7 @@ func TestReadRecordRanges(t *testing.T) {
rdr, sz, err := jcs.(journalChunkSource).journal.snapshot()
require.NoError(t, err)
defer rdr.Close()
buf = make([]byte, sz)
n, err := rdr.Read(buf)

View File

@@ -399,9 +399,18 @@ func (wr *journalWriter) maybeFlush() (err error) {
return wr.flush()
}
type journalWriterSnapshot struct {
io.Reader
closer func() error
}
func (s journalWriterSnapshot) Close() error {
return s.closer()
}
// snapshot returns an io.Reader with a consistent view of
// the current state of the journal file.
func (wr *journalWriter) snapshot() (io.Reader, int64, error) {
func (wr *journalWriter) snapshot() (io.ReadCloser, int64, error) {
wr.lock.Lock()
defer wr.lock.Unlock()
if err := wr.flush(); err != nil {
@@ -413,7 +422,12 @@ func (wr *journalWriter) snapshot() (io.Reader, int64, error) {
if err != nil {
return nil, 0, err
}
return io.LimitReader(f, wr.off), wr.off, nil
return journalWriterSnapshot{
io.LimitReader(f, wr.off),
func() error {
return f.Close()
},
}, wr.off, nil
}
func (wr *journalWriter) offset() int64 {
@@ -444,6 +458,9 @@ func (wr *journalWriter) Close() (err error) {
if err = wr.flush(); err != nil {
return err
}
if wr.index != nil {
wr.index.Close()
}
if cerr := wr.journal.Sync(); cerr != nil {
err = cerr
}

View File

@@ -44,8 +44,10 @@ func TestStats(t *testing.T) {
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer file.RemoveAll(dir)
store, err := NewLocalStore(context.Background(), constants.FormatDefaultString, dir, testMemTableSize, NewUnlimitedMemQuotaProvider())
require.NoError(t, err)
defer store.Close()
assert.EqualValues(1, stats(store).OpenLatency.Samples())
@@ -147,7 +149,4 @@ func TestStats(t *testing.T) {
assert.Equal(uint64(1), stats(store).ConjoinLatency.Samples())
// TODO: Once random conjoin hack is out, test other conjoin stats
defer store.Close()
defer file.RemoveAll(dir)
}