Skip to content

Commit

Permalink
Fixed tests for pebbleDB
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalicevic committed Jan 30, 2024
1 parent 7292b78 commit e01569d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 17 additions & 0 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

Check failure on line 8 in backend_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/cometbft/cometbft-db) --custom-order (gci)

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

Check failure on line 12 in backend_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/cometbft/cometbft-db) --custom-order (gci)
)

// Register a test backend for PrefixDB as well, with some unrelated junk data
Expand Down Expand Up @@ -135,11 +137,22 @@ func testBackendGetSetDelete(t *testing.T, backend BackendType) {
require.NoError(t, err)
require.Equal(t, []byte{}, value)

err = db.Compact(nil, nil)
if strings.Contains(string(backend), "pebbledb") {
// In pebble the strat and end will be the same so

Check failure on line 142 in backend_test.go

View workflow job for this annotation

GitHub Actions / golangci

`strat` is a misspelling of `start` (misspell)
// we expect an error
require.Error(t, err)
}

err = db.Set([]byte("y"), []byte{})
require.NoError(t, err)

err = db.Compact(nil, nil)
require.NoError(t, err)

if strings.Contains(string(backend), "pebbledb") {
time.Sleep(time.Second * 5)
}
}

func TestBackendsGetSetDelete(t *testing.T) {
Expand Down Expand Up @@ -425,6 +438,10 @@ func testDBBatch(t *testing.T, backend BackendType) {
require.Error(t, batch.WriteSync())

require.NoError(t, batch.Compact(nil, nil))

if strings.Contains(string(backend), "pebbledb") {
time.Sleep(5 * time.Second)
}
}

func assertKeyValues(t *testing.T, db DB, expect map[string][]byte) {
Expand Down
16 changes: 10 additions & 6 deletions pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,24 @@ func (db *PebbleDB) DB() *pebble.DB {
func (db *PebbleDB) Compact(start, end []byte) error {
// Currently nil,nil is an invalid range in Pebble.
// This was taken from https://github.com/cockroachdb/pebble/issues/1474
// In case the start and end keys are the same
// pebbleDB will throw an error that it cannot compact.
if start != nil && end != nil {
return db.db.Compact(start, end, true)
}

iter := db.db.NewIter(nil)
var first, last []byte

if iter.First() {
first = append(first, iter.Key()...)
if start == nil && iter.First() {
start = append(start, iter.Key()...)
}
if iter.Last() {
last = append(last, iter.Key()...)
if end == nil && iter.Last() {
end = append(end, iter.Key()...)
}
if err := iter.Close(); err != nil {
return err
}
return db.db.Compact(first, last, true)
return db.db.Compact(start, end, true)
}

// Close implements DB.
Expand Down

0 comments on commit e01569d

Please sign in to comment.