add failpoint 'resizeFileError' to simulate file.Truncate error

Signed-off-by: Benjamin Wang <wachao@vmware.com>
pull/482/head
Benjamin Wang 2023-05-05 15:47:04 +08:00
parent 97d2cc34ac
commit 465077b9e2
2 changed files with 30 additions and 0 deletions

2
db.go
View File

@ -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)
}

View File

@ -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)
}