Skip to content

Commit

Permalink
ensure we are closing iterators in our tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Sep 26, 2024
1 parent bf48313 commit 7945c89
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test-all: test-clean
test-with-reset: db-reset test-all

test-clean:
GOGC=off go clean -testcache
GOGC=off go clean -testcache && rm -rf test_db tmp_db

bench:
@cd _benchmarks && go test -timeout=25m -bench=.
Expand Down
3 changes: 3 additions & 0 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func TestBond_BackupRestore(t *testing.T) {
// make sure both db has same keys and values.
itr := db.Iter(&IterOptions{})
itr2 := db2.Iter(&IterOptions{})
defer itr.Close()
defer itr2.Close()

for _, _ = itr.First(), itr2.First(); itr.Valid(); _, _ = itr.Next(), itr2.Next() {
require.Equal(t, itr.Key(), itr2.Key())
require.Equal(t, itr.Value(), itr2.Value())
Expand Down
5 changes: 5 additions & 0 deletions bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ func (db *_db) Dump(_ context.Context, path string, tables []TableID, withIndex
defer snapshot.Close()

grp := new(errgroup.Group)

// write all the table data to the sst file.
for _, tableID := range tables {
tablePath := filepath.Join(path, fmt.Sprintf("table_%d", tableID))
Expand All @@ -413,6 +414,7 @@ func (db *_db) Dump(_ context.Context, path string, tables []TableID, withIndex
if !withIndex {
continue
}

// write all the index data to sst file.
indexes := db.getIndexIDS(tableID)
for _, index := range indexes {
Expand Down Expand Up @@ -518,12 +520,14 @@ func (db *_db) getIndexIDS(tableID TableID) []IndexID {
}
prefix[1] = indexID + 1
}
itr.Close()
return indexIDS
}

// write all the key/value of iterator to the SST file.
func iteratorToSST(itr Iterator, path string) error {
defer itr.Close()

// sst reader
currentFileID := 1
file, err := vfs.Default.Create(filepath.Join(path, fmt.Sprintf("%d.sst", currentFileID)), vfs.WriteCategoryUnspecified)
Expand All @@ -538,6 +542,7 @@ func iteratorToSST(itr Iterator, path string) error {

for itr.First(); itr.Valid(); itr.Next() {
if err := writer.Set(itr.Key(), itr.Value()); err != nil {
writer.Close()
return err
}

Expand Down
18 changes: 15 additions & 3 deletions bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func setupDB(name string, serializer ...Serializer[any]) DB {
options.Serializer = serializer[0]
}

db, _ := Open(name, options)
db, err := Open(name, options)
if err != nil {
panic(err)
}
return db
}

Expand All @@ -31,8 +34,17 @@ func tearDownDatabase(db DB) {
}

func tearDownDB(name string, db DB) {
_ = db.Close()
_ = os.RemoveAll(name)
// NOTE: we intentionally panic on Close here, as it will catch any
// leaking iterators which would otherwise be difficult to debug,
// and are relevant to ensure we close.
err := db.Close()
if err != nil {
panic(err)
}
err = os.RemoveAll(name)
if err != nil {
panic(err)
}
}

func TestBond_Open(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ func TestBond_Table_Index_Insert(t *testing.T) {
copy(buff, it.Key())
keys = append(keys, buff)
}
it.Close()

require.Equal(t, 8, len(keys))
assert.True(t, strings.Contains(string(keys[3]), "0xtestAccount1"))
Expand All @@ -1287,6 +1288,7 @@ func TestBond_Table_Index_Insert(t *testing.T) {
for it.First(); it.Valid(); it.Next() {
fmt.Printf("0x%x(%s): %s\n", it.Key(), it.Key(), it.Value())
}
it.Close()
}

func TestBond_Table_Index_Insert_Ordered(t *testing.T) {
Expand Down Expand Up @@ -1395,6 +1397,7 @@ func TestBond_Table_Index_Insert_Ordered(t *testing.T) {
copy(buff, it.Key())
keys = append(keys, buff)
}
it.Close()

require.Equal(t, 8, len(keys))
assert.True(t, strings.Contains(string(keys[3]), "0xtestAccount1") && bytes.Contains(keys[3], []byte{0xF0}))
Expand All @@ -1414,6 +1417,7 @@ func TestBond_Table_Index_Insert_Ordered(t *testing.T) {
for it.First(); it.Valid(); it.Next() {
fmt.Printf("0x%x(%s): %s\n", it.Key(), it.Key(), it.Value())
}
it.Close()
}

func TestBond_Table_Index_Update(t *testing.T) {
Expand Down Expand Up @@ -1532,6 +1536,7 @@ func TestBond_Table_Index_Update(t *testing.T) {
copy(buff, it.Key())
keys = append(keys, buff)
}
it.Close()

require.Equal(t, 8, len(keys))
assert.True(t, strings.Contains(string(keys[3]), "0xtestAccount3"))
Expand All @@ -1551,6 +1556,7 @@ func TestBond_Table_Index_Update(t *testing.T) {
for it.First(); it.Valid(); it.Next() {
fmt.Printf("0x%x(%s): %s\n", it.Key(), it.Key(), it.Value())
}
it.Close()
}

func TestBond_Table_Index_Update_Ordered(t *testing.T) {
Expand Down Expand Up @@ -1673,6 +1679,7 @@ func TestBond_Table_Index_Update_Ordered(t *testing.T) {
copy(buff, it.Key())
keys = append(keys, buff)
}
it.Close()

require.Equal(t, 8, len(keys))
assert.True(t, strings.Contains(string(keys[3]), "0xtestAccount3") && bytes.Contains(keys[3], []byte{0xF0}))
Expand All @@ -1692,6 +1699,7 @@ func TestBond_Table_Index_Update_Ordered(t *testing.T) {
for it.First(); it.Valid(); it.Next() {
fmt.Printf("0x%x(%s): %s\n", it.Key(), it.Key(), it.Value())
}
it.Close()
}

func TestBond_Table_Index_Delete(t *testing.T) {
Expand Down Expand Up @@ -1800,6 +1808,7 @@ func TestBond_Table_Index_Delete(t *testing.T) {
})
require.NoError(t, err)
assert.False(t, it.First())
it.Close()

it, err = db.Backend().NewIter(&pebble.IterOptions{
LowerBound: []byte{byte(TokenBalanceTableID)},
Expand All @@ -1812,6 +1821,7 @@ func TestBond_Table_Index_Delete(t *testing.T) {
for it.First(); it.Valid(); it.Next() {
fmt.Printf("0x%x(%s): %s\n", it.Key(), it.Key(), it.Value())
}
it.Close()
}

func TestBond_Table_Reindex(t *testing.T) {
Expand Down
19 changes: 18 additions & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func TestBondTable_Insert(t *testing.T) {
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
require.NoError(t, err)
defer it.Close()

for it.First(); it.Valid(); it.Next() {
rawData := it.Value()
Expand Down Expand Up @@ -539,6 +540,7 @@ func TestBondTable_Insert_When_Exist(t *testing.T) {
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
require.NoError(t, err)
defer it.Close()

for it.First(); it.Valid(); it.Next() {
rawData := it.Value()
Expand Down Expand Up @@ -780,8 +782,13 @@ func TestBondTable_Upsert(t *testing.T) {

var tokenBalanceAccountFromDB TokenBalance
err = db.Serializer().Deserialize(rawData, &tokenBalanceAccountFromDB)
if err != nil {
// panic(err)
break
}
tokenBalances = append(tokenBalances, &tokenBalanceAccountFromDB)
}
require.NoError(t, err)

_ = it.Close()

Expand Down Expand Up @@ -872,8 +879,12 @@ func TestBondTable_Upsert_Context_Canceled(t *testing.T) {

var tokenBalanceAccountFromDB TokenBalance
err = db.Serializer().Deserialize(rawData, &tokenBalanceAccountFromDB)
if err != nil {
break
}
tokenBalances = append(tokenBalances, &tokenBalanceAccountFromDB)
}
require.NoError(t, err)

_ = it.Close()

Expand Down Expand Up @@ -978,8 +989,12 @@ func TestBondTable_Upsert_OnConflict(t *testing.T) {

var tokenBalanceAccountFromDB TokenBalance
err = db.Serializer().Deserialize(rawData, &tokenBalanceAccountFromDB)
if err != nil {
break
}
tokenBalances = append(tokenBalances, &tokenBalanceAccountFromDB)
}
require.NoError(t, err)

_ = it.Close()

Expand Down Expand Up @@ -1128,7 +1143,7 @@ func TestBondTable_Update_No_Such_Entry(t *testing.T) {
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
require.NoError(t, err)

defer it.Close()
assert.False(t, it.First())
}

Expand Down Expand Up @@ -1168,6 +1183,7 @@ func TestBondTable_Delete(t *testing.T) {
UpperBound: []byte{byte(TokenBalanceTableID + 1)},
})
require.NoError(t, err)
defer it.Close()
assert.False(t, it.First())
}

Expand Down Expand Up @@ -1475,4 +1491,5 @@ func TestBond_Batch(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tokenBalance, &tokenBalanceAccountFromDB)
}
it.Close()
}

0 comments on commit 7945c89

Please sign in to comment.