bbolt/tests/failpoint/db_failpoint_test.go
caojiamingalan 505fc0f7af complete all cleanup operations in db.close() even if there is an error in the middle
Signed-off-by: caojiamingalan <alan.c.19971111@gmail.com>
2023-02-11 07:21:13 +08:00

49 lines
1.2 KiB
Go

package failpoint
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
gofail "go.etcd.io/gofail/runtime"
)
func TestFailpoint_MapFail(t *testing.T) {
err := gofail.Enable("mapError", `return("map somehow failed")`)
require.NoError(t, err)
defer func() {
err = gofail.Disable("mapError")
require.NoError(t, err)
}()
f := filepath.Join(t.TempDir(), "db")
_, err = bolt.Open(f, 0666, nil)
require.Error(t, err)
require.ErrorContains(t, err, "map somehow failed")
}
// ensures when munmap fails, the flock is unlocked
func TestFailpoint_UnmapFail_DbClose(t *testing.T) {
//unmap error on db close
//we need to open the db first, and then enable the error.
//otherwise the db cannot be opened.
f := filepath.Join(t.TempDir(), "db")
err := gofail.Enable("unmapError", `return("unmap somehow failed")`)
require.NoError(t, err)
_, err = bolt.Open(f, 0666, nil)
require.Error(t, err)
require.ErrorContains(t, err, "unmap somehow failed")
//disable the error, and try to reopen the db
err = gofail.Disable("unmapError")
require.NoError(t, err)
db, err := bolt.Open(f, 0666, &bolt.Options{Timeout: 30 * time.Second})
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
}