Skip to content

Commit

Permalink
*: introduce failpoint beforeBucketPut
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Dec 12, 2023
1 parent a7a791c commit 93ca794
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ func (b *Bucket) Put(key []byte, value []byte) error {
return errors.ErrIncompatibleValue
}

// gofail: var beforeBucketPut struct{}

// Insert into node.
// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent
// it from being marked as leaking, and accordingly cannot be allocated on stack.
Expand Down
21 changes: 21 additions & 0 deletions tests/failpoint/db_failpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -155,3 +156,23 @@ func TestFailpoint_LackOfDiskSpace(t *testing.T) {
require.Error(t, err)
require.ErrorIs(t, err, errors.ErrTxClosed)
}

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

err := gofail.Enable("beforeBucketPut", `sleep(3000)`)
require.NoError(t, err)

tx, err := db.Begin(true)
require.NoError(t, err)

b, err := tx.CreateBucket([]byte(t.Name()))
require.NoError(t, err)

start := time.Now()
require.NoError(t, b.Put([]byte("1"), []byte("1")))
assert.True(t, time.Since(start) >= 5*time.Second)

require.NoError(t, gofail.Disable("beforeBucketPut"))
require.NoError(t, tx.Commit())
}

0 comments on commit 93ca794

Please sign in to comment.