Skip to content

Commit

Permalink
Merge pull request #26 from shane-ns1/staticcheck-test-fixes
Browse files Browse the repository at this point in the history
Fix issues reported by staticcheck in tests
  • Loading branch information
wojas authored Oct 31, 2023
2 parents 0ff3481 + 8d1748e commit 2b2b787
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 57 deletions.
13 changes: 6 additions & 7 deletions exp/lmdbsync/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ func TestHandlerChain(t *testing.T) {
}
}

type retryHandler struct{}

func (*retryHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
return ctx, ErrTxnRetry

}

type passthroughHandler struct{}

func (*passthroughHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
Expand Down Expand Up @@ -92,6 +85,9 @@ func TestMapFullHandler(t *testing.T) {
if ctx1 != ctx {
t.Errorf("ctx changed: %q (!= %q)", ctx1, ctx)
}
if err != errother {
t.Errorf("unexpected error: %v", err)
}

errmapfull := &lmdb.OpError{
Op: "lmdbsync_test_op",
Expand Down Expand Up @@ -127,6 +123,9 @@ func TestMapResizedHandler(t *testing.T) {

errother := fmt.Errorf("testerr")
_, err = handler.HandleTxnErr(ctx, env, errother)
if err != errother {
t.Errorf("unexpected error: %v", err)
}

errmapresized := &lmdb.OpError{
Op: "lmdbsync_test_op",
Expand Down
29 changes: 0 additions & 29 deletions lmdb/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,12 +1605,6 @@ func openBenchDBI(b *testing.B, env *Env) DBI {
return dbi
}

func randBytes(n int) []byte {
p := make([]byte, n)
crand.Read(p)
return p
}

func bMust(b *testing.B, err error, action string) {
if err != nil {
b.Fatalf("error %s: %v", action, err)
Expand Down Expand Up @@ -1672,14 +1666,6 @@ func (c *randSourceCursor) NBytes(n int) []byte {
return randSource[i : i+n]
}

func populateDBIString(t testing.TB, env *Env, dbi DBI, records []testRecordString) (ok bool) {
return populateDBI(t, env, dbi, testRecordSetString(records))
}

func populateDBIBytes(t testing.TB, env *Env, dbi DBI, records []testRecordBytes) (ok bool) {
return populateDBI(t, env, dbi, testRecordSetBytes(records))
}

func populateDBI(t testing.TB, env *Env, dbi DBI, records testRecordSet) (ok bool) {
err := env.SetMapSize(benchDBMapSize)
if err != nil {
Expand Down Expand Up @@ -1742,16 +1728,6 @@ type testRecordSet interface {
TestRecord(i int) testRecord
}

type testRecordSetString []testRecordString

func (s testRecordSetString) Len() int { return len(s) }
func (s testRecordSetString) TestRecord(i int) testRecord { return s[i] }

type testRecordSetBytes []testRecordBytes

func (s testRecordSetBytes) Len() int { return len(s) }
func (s testRecordSetBytes) TestRecord(i int) testRecord { return s[i] }

type testRecordGen struct {
n int
fn testRecordFn
Expand All @@ -1771,8 +1747,3 @@ type testRecordBytes [2][]byte

func (r testRecordBytes) Key() []byte { return r[0] }
func (r testRecordBytes) Data() []byte { return r[1] }

type testRecordString [2][]byte

func (r testRecordString) Key() []byte { return []byte(r[0]) }
func (r testRecordString) Data() []byte { return []byte(r[1]) }
3 changes: 3 additions & 0 deletions lmdb/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ func TestCursor_Get_DupFixed(t *testing.T) {

for i := int64(0); i < int64(numitems); i++ {
err = txn.Put(dbi, key, []byte(fmt.Sprintf("%016x", i)), 0)
if err != nil {
return err
}
}

return nil
Expand Down
11 changes: 2 additions & 9 deletions lmdb/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ import (
var EnvEx *lmdb.Env
var DBIEx lmdb.DBI

// These values can only be used is code-only examples (no test output).
// These values can only be used in code-only examples (no test output).
var env *lmdb.Env
var txn *lmdb.Txn
var dbi lmdb.DBI
var dbname string
var err error
var stop chan struct{}

// These values can be used as no-op placeholders in examples.
func doUpdate(txn *lmdb.Txn) error { return nil }
func doUpdate1(txn *lmdb.Txn) error { return nil }
func doUpdate2(txn *lmdb.Txn) error { return nil }
func doView(txn *lmdb.Txn) error { return nil }
func doUpdate(txn *lmdb.Txn) error { return nil }

// This example demonstrates a complete workflow for a simple application
// working with LMDB. First, an Env is configured and mapped to memory. Once
Expand Down Expand Up @@ -220,8 +215,6 @@ retry:
// ...
}

func backupFailed(err error) {}

// This example uses env.Copy to periodically create atomic backups of an
// environment. A real implementation would need to solve problems with
// failures, remote persistence, purging old backups, etc. But the core loop
Expand Down
14 changes: 6 additions & 8 deletions lmdb/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestTxn_ID(t *testing.T) {
if err != nil {
t.Error(err)
}
if 0 != id0 {
if id0 != 0 {
t.Errorf("unexpected readonly id (before update): %v (!= %v)", id0, 0)
}

Expand All @@ -34,7 +34,7 @@ func TestTxn_ID(t *testing.T) {
return
}
defer txnCached.Abort()
if 0 != txnCached.ID() {
if txnCached.ID() != 0 {
t.Errorf("unexpected readonly id (before update): %v (!= %v)", txnCached.ID(), 0)
}
if txnCached.getID() != txnCached.ID() {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestTxn_ID(t *testing.T) {
if id1 != id2 {
t.Errorf("unexpected readonly id: %v (!= %v)", id2, id1)
}
if 0 != id3 {
if id3 != 0 {
t.Errorf("unexpected invalid id: %v (!= %v)", id3, 0)
}
if id1 != txnCached.ID() {
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestTxn_Renew(t *testing.T) {
return
}
defer txn.Abort()
val, err := txn.Get(dbroot, []byte("k"))
_, err = txn.Get(dbroot, []byte("k"))
if !IsNotFound(err) {
t.Errorf("get: %v", err)
}
Expand All @@ -794,7 +794,7 @@ func TestTxn_Renew(t *testing.T) {
t.Error(err)
}

val, err = txn.Get(dbroot, []byte("k"))
_, err = txn.Get(dbroot, []byte("k"))
if !IsNotFound(err) {
t.Errorf("get: %v", err)
}
Expand All @@ -804,7 +804,7 @@ func TestTxn_Renew(t *testing.T) {
if err != nil {
t.Error(err)
}
val, err = txn.Get(dbroot, []byte("k"))
val, err := txn.Get(dbroot, []byte("k"))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -1097,8 +1097,6 @@ func BenchmarkTxn_Sub_abort(b *testing.B) {
defer b.StopTimer()
for i := 0; i < b.N; i++ {
txn.Sub(func(txn *Txn) (err error) { return e })
if e == nil {
}
}
return nil
})
Expand Down
4 changes: 0 additions & 4 deletions lmdbscan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/PowerDNS/lmdb-go/lmdb"
)

type errcheck func(err error) (ok bool)

var pIsNil = func(err error) bool { return err == nil }

func TestScanner_err(t *testing.T) {
env, err := lmdbtest.NewEnv(nil)
if err != nil {
Expand Down

0 comments on commit 2b2b787

Please sign in to comment.