diff --git a/lib/mergeset/table_test.go b/lib/mergeset/table_test.go index 5c8445cff3..189813e75e 100644 --- a/lib/mergeset/table_test.go +++ b/lib/mergeset/table_test.go @@ -95,13 +95,9 @@ func TestTableCreateSnapshotAt(t *testing.T) { if err := os.RemoveAll(path); err != nil { t.Fatalf("cannot remove %q: %s", path, err) } - defer func() { - _ = os.RemoveAll(path) - }() var isReadOnly uint32 tb := MustOpenTable(path, nil, nil, &isReadOnly) - defer tb.MustClose() // Write a lot of items into the table, so background merges would start. const itemsCount = 3e5 @@ -109,7 +105,11 @@ func TestTableCreateSnapshotAt(t *testing.T) { item := []byte(fmt.Sprintf("item %d", i)) tb.AddItems([][]byte{item}) } - tb.DebugFlush() + + // Close and open the table in order to flush all the data to disk before creating snapshots. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4272#issuecomment-1550221840 + tb.MustClose() + tb = MustOpenTable(path, nil, nil, &isReadOnly) // Create multiple snapshots. snapshot1 := path + "-test-snapshot1" @@ -120,24 +120,15 @@ func TestTableCreateSnapshotAt(t *testing.T) { if err := tb.CreateSnapshotAt(snapshot2, 0); err != nil { t.Fatalf("cannot create snapshot2: %s", err) } - defer func() { - _ = os.RemoveAll(snapshot1) - _ = os.RemoveAll(snapshot2) - }() // Verify snapshots contain all the data. tb1 := MustOpenTable(snapshot1, nil, nil, &isReadOnly) - defer tb1.MustClose() - tb2 := MustOpenTable(snapshot2, nil, nil, &isReadOnly) - defer tb2.MustClose() var ts, ts1, ts2 TableSearch ts.Init(tb) ts1.Init(tb1) - defer ts1.MustClose() ts2.Init(tb2) - defer ts2.MustClose() for i := 0; i < itemsCount; i++ { key := []byte(fmt.Sprintf("item %d", i)) if err := ts.FirstItemWithPrefix(key); err != nil { @@ -159,6 +150,17 @@ func TestTableCreateSnapshotAt(t *testing.T) { t.Fatalf("unexpected item found for key=%q in snapshot2; got %q", key, ts2.Item) } } + ts1.MustClose() + ts2.MustClose() + + // Close and remove tables. + tb2.MustClose() + tb1.MustClose() + tb.MustClose() + + _ = os.RemoveAll(snapshot2) + _ = os.RemoveAll(snapshot1) + _ = os.RemoveAll(path) } func TestTableAddItemsConcurrent(t *testing.T) {