Skip to content

Commit

Permalink
Merge pull request #482 from ahrtr/failpoint_20230505
Browse files Browse the repository at this point in the history
add failpoint 'resizeFileError' to simulate file.Truncate error
  • Loading branch information
ahrtr authored May 5, 2023
2 parents 97d2cc3 + 465077b commit 88c6eb7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,8 @@ func (db *DB) grow(sz int) error {
// https://github.com/boltdb/bolt/issues/284
if !db.NoGrowSync && !db.readOnly {
if runtime.GOOS != "windows" {
// gofail: var resizeFileError string
// return errors.New(resizeFileError)
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("file resize error: %s", err)
}
Expand Down
28 changes: 28 additions & 0 deletions tests/failpoint/db_failpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,31 @@ func TestFailpoint_mLockFail_When_remap(t *testing.T) {

require.NoError(t, err)
}

func TestFailpoint_ResizeFileFail(t *testing.T) {
db := btesting.MustCreateDB(t)

err := gofail.Enable("resizeFileError", `return("resizeFile somehow failed")`)
require.NoError(t, err)

err = db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
func(tx int, k int) []byte { return make([]byte, 100) },
)

require.Error(t, err)
require.ErrorContains(t, err, "resizeFile somehow failed")

// It should work after disabling the failpoint.
err = gofail.Disable("resizeFileError")
require.NoError(t, err)
db.MustClose()
db.MustReopen()

err = db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
func(tx int, k int) []byte { return make([]byte, 100) },
)

require.NoError(t, err)
}

0 comments on commit 88c6eb7

Please sign in to comment.