Skip to content

Commit

Permalink
database: Use TempDir to create temp test dirs.
Browse files Browse the repository at this point in the history
This modifies several of the tests to use t.TempDir.  The directory it
creates is automatically removed when the test and all its subtests
complete.

It also changes out some of the cleanup logic for closing the database
to use t.Cleanup instead of defer to ensure the close happens during
test cleanup after the directories created by t.TempDir have been
removed.
  • Loading branch information
davecgh committed Jun 14, 2023
1 parent c6d89f3 commit 0c54fb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
18 changes: 8 additions & 10 deletions database/ffldb/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
package ffldb

import (
"os"
"path/filepath"
"testing"

"github.com/decred/dcrd/chaincfg/v3"
Expand All @@ -20,14 +18,14 @@ import (
func BenchmarkBlockHeader(b *testing.B) {
// Start by creating a new database and populating it with the mainnet
// genesis block.
dbPath := filepath.Join(os.TempDir(), "ffldb-benchblkhdr")
_ = os.RemoveAll(dbPath)
dbPath := b.TempDir()
db, err := database.Create("ffldb", dbPath, blockDataNet)
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(dbPath)
defer db.Close()
b.Cleanup(func() {
db.Close()
})
mainNetParams := chaincfg.MainNetParams()
err = db.Update(func(tx database.Tx) error {
block := dcrutil.NewBlock(mainNetParams.GenesisBlock)
Expand Down Expand Up @@ -62,14 +60,14 @@ func BenchmarkBlockHeader(b *testing.B) {
func BenchmarkBlock(b *testing.B) {
// Start by creating a new database and populating it with the mainnet
// genesis block.
dbPath := filepath.Join(os.TempDir(), "ffldb-benchblk")
_ = os.RemoveAll(dbPath)
dbPath := b.TempDir()
db, err := database.Create("ffldb", dbPath, blockDataNet)
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(dbPath)
defer db.Close()
b.Cleanup(func() {
db.Close()
})
mainNetParams := chaincfg.MainNetParams()
err = db.Update(func(tx database.Tx) error {
block := dcrutil.NewBlock(mainNetParams.GenesisBlock)
Expand Down
22 changes: 9 additions & 13 deletions database/ffldb/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package ffldb_test

import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -103,14 +101,12 @@ func TestCreateOpenFail(t *testing.T) {

// Ensure operations against a closed database return the expected
// error.
dbPath := filepath.Join(os.TempDir(), "ffldb-createfail-v2")
_ = os.RemoveAll(dbPath)
dbPath := t.TempDir()
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Create: unexpected error: %v", err)
return
}
defer os.RemoveAll(dbPath)
db.Close()

wantErrKind = database.ErrDbNotOpen
Expand Down Expand Up @@ -154,15 +150,15 @@ func TestPersistence(t *testing.T) {
t.Parallel()

// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-persistencetest-v2")
_ = os.RemoveAll(dbPath)
dbPath := t.TempDir()
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer db.Close()
t.Cleanup(func() {
db.Close()
})

// Create a bucket, put some values into it, and store a block so they
// can be tested for existence on re-open.
Expand Down Expand Up @@ -261,15 +257,15 @@ func TestInterface(t *testing.T) {
t.Parallel()

// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-interfacetest-v2")
_ = os.RemoveAll(dbPath)
dbPath := t.TempDir()
db, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer db.Close()
t.Cleanup(func() {
db.Close()
})

// Ensure the driver type is the expected value.
gotDbType := db.Type()
Expand Down
12 changes: 5 additions & 7 deletions database/ffldb/whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestCornerCases(t *testing.T) {
t.Parallel()

// Create a file at the database path to force the open below to fail.
dbPath := filepath.Join(os.TempDir(), "ffldb-errors-v2")
dbPath := t.TempDir()
_ = os.RemoveAll(dbPath)
fi, err := os.Create(dbPath)
if err != nil {
Expand All @@ -159,7 +159,6 @@ func TestCornerCases(t *testing.T) {
if err == nil {
idb.Close()
}
_ = os.RemoveAll(dbPath)
return
}

Expand All @@ -171,8 +170,9 @@ func TestCornerCases(t *testing.T) {
t.Errorf("openDB: unexpected error: %v", err)
return
}
defer os.RemoveAll(dbPath)
defer idb.Close()
t.Cleanup(func() {
idb.Close()
})

// Ensure attempting to write to a file that can't be created returns
// the expected error.
Expand Down Expand Up @@ -578,14 +578,12 @@ func testCorruption(tc *testContext) bool {
// correctly.
func TestFailureScenarios(t *testing.T) {
// Create a new database to run tests against.
dbPath := filepath.Join(os.TempDir(), "ffldb-failurescenarios-v2")
_ = os.RemoveAll(dbPath)
dbPath := t.TempDir()
idb, err := database.Create(dbType, dbPath, blockDataNet)
if err != nil {
t.Errorf("Failed to create test database (%s) %v", dbType, err)
return
}
defer os.RemoveAll(dbPath)
defer idb.Close()

// Create a test context to pass around.
Expand Down

0 comments on commit 0c54fb8

Please sign in to comment.