Skip to content

Commit

Permalink
Backport add test cases to simulate mlock failure.
Browse files Browse the repository at this point in the history
Signed-off-by: James Blair <mail@jamesblair.net>
  • Loading branch information
jmhbnz committed Mar 30, 2023
1 parent 7a13798 commit 95acc50
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/failpoint/db_failpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package failpoint

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"

bolt "go.etcd.io/bbolt"
"go.etcd.io/bbolt/internal/btesting"
gofail "go.etcd.io/gofail/runtime"
)

Expand Down Expand Up @@ -46,3 +48,49 @@ func TestFailpoint_UnmapFail_DbClose(t *testing.T) {
err = db.Close()
require.NoError(t, err)
}

func TestFailpoint_mLockFail(t *testing.T) {
err := gofail.Enable("mlockError", `return("mlock somehow failed")`)
require.NoError(t, err)

f := filepath.Join(t.TempDir(), "db")
_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
require.Error(t, err)
require.ErrorContains(t, err, "mlock somehow failed")

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

_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
require.NoError(t, err)
}

func TestFailpoint_mLockFail_When_remap(t *testing.T) {
db := btesting.MustCreateDB(t)
db.Mlock = true

err := gofail.Enable("mlockError", `return("mlock somehow failed in allocate")`)
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, "mlock somehow failed in allocate")

// It should work after disabling the failpoint.
err = gofail.Disable("mlockError")
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 95acc50

Please sign in to comment.