Skip to content

Commit

Permalink
Use require.NoError in btesting.go
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Tabor <[email protected]>
  • Loading branch information
ptabor committed Dec 28, 2022
1 parent 93380c5 commit 37d72cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
10 changes: 2 additions & 8 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestOpen_Size_Large(t *testing.T) {
}

// Close database and grab the size.
if err := db.DB.Close(); err != nil {
if err := db.Close(); err != nil {
t.Fatal(err)
}
sz := fileSize(path)
Expand Down Expand Up @@ -463,7 +463,7 @@ func TestDB_Open_ReadOnly(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
if err := db.DB.Close(); err != nil {
if err := db.Close(); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -556,9 +556,6 @@ func TestOpen_RecoverFreeList(t *testing.T) {
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
if err := db.DB.Close(); err != nil {
t.Fatal(err)
}
db.MustClose()

// Record freelist count from opening with NoFreelistSync.
Expand All @@ -567,9 +564,6 @@ func TestOpen_RecoverFreeList(t *testing.T) {
if freepages == 0 {
t.Fatalf("no free pages on NoFreelistSync reopen")
}
if err := db.DB.Close(); err != nil {
t.Fatal(err)
}
db.MustClose()

// Check free page count is reconstructed when opened with freelist sync.
Expand Down
32 changes: 12 additions & 20 deletions internal/btesting/btesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ func MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB {
o.FreelistType = freelistType

db, err := bolt.Open(f, 0666, o)
if err != nil {
panic(err)
}
require.NoError(t, err)
resDB := &DB{
DB: db,
f: f,
Expand Down Expand Up @@ -92,9 +90,8 @@ func (db *DB) Close() error {

// MustClose closes the database but does NOT delete the underlying file.
func (db *DB) MustClose() {
if err := db.Close(); err != nil {
panic(err)
}
err := db.Close()
require.NoError(db.t, err)
}

func (db *DB) MustDeleteFile() {
Expand All @@ -113,15 +110,13 @@ func (db *DB) MustReopen() {
}
db.t.Logf("Reopening bbolt DB at: %s", db.f)
indb, err := bolt.Open(db.Path(), 0666, db.o)
if err != nil {
panic(err)
}
require.NoError(db.t, err)
db.DB = indb
}

// MustCheck runs a consistency check on the database and panics if any errors are found.
func (db *DB) MustCheck() {
if err := db.Update(func(tx *bolt.Tx) error {
err := db.Update(func(tx *bolt.Tx) error {
// Collect all the errors.
var errors []error
for err := range tx.Check() {
Expand All @@ -134,9 +129,8 @@ func (db *DB) MustCheck() {
// If errors occurred, copy the DB and print the errors.
if len(errors) > 0 {
var path = filepath.Join(db.t.TempDir(), "db.backup")
if err := tx.CopyFile(path, 0600); err != nil {
panic(err)
}
err := tx.CopyFile(path, 0600)
require.NoError(db.t, err)

// Print errors.
fmt.Print("\n\n")
Expand All @@ -152,9 +146,8 @@ func (db *DB) MustCheck() {
}

return nil
}); err != nil && err != bolt.ErrDatabaseNotOpen {
panic(err)
}
})
require.NoError(db.t, err)
}

// Fill - fills the DB using numTx transactions and numKeysPerTx.
Expand Down Expand Up @@ -185,11 +178,10 @@ func (db *DB) Path() string {
// CopyTempFile copies a database to a temporary file.
func (db *DB) CopyTempFile() {
path := filepath.Join(db.t.TempDir(), "db.copy")
if err := db.View(func(tx *bolt.Tx) error {
err := db.View(func(tx *bolt.Tx) error {
return tx.CopyFile(path, 0600)
}); err != nil {
panic(err)
}
})
require.NoError(db.t, err)
fmt.Println("db copied to: ", path)
}

Expand Down
1 change: 0 additions & 1 deletion tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
// TestTx_Check_ReadOnly tests consistency checking on a ReadOnly database.
func TestTx_Check_ReadOnly(t *testing.T) {
db := btesting.MustCreateDB(t)
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
Expand Down

0 comments on commit 37d72cc

Please sign in to comment.