Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of a test that causes a badgerdb crash with a misleading error string
package main
import (
"os"
"testing"
"github.com/dgraph-io/badger"
)
/*
=== RUN TestBadgerClose
--- FAIL: TestBadgerClose (0.63s)
badger_close_test:62 I am a failed test
panic: runtime error: invalid memory address or nil pointer dereference
panic: Unclosed iterator at time of Txn.Discard. [recovered]
panic: Unclosed iterator at time of Txn.Discard.
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7b3162]
goroutine 7 [running]:
testing.tRunner.func1(0xc4201960f0)
/go/1.10.0/src/testing/testing.go:742 +0x29d
panic(0x92dde0, 0xa79480)
/go/1.10.0/src/runtime/panic.go:505 +0x229
domain.com/user/project/vendor/github.com/dgraph-io/badger.(*Txn).Discard(0xc4201685a0)
$HOME/src/go/src/domain.com/user/project/vendor/github.com/dgraph-io/badger/transaction.go:428 +0xdc
panic(0x95fc40, 0xd42ef0)
/go/1.10.0/src/runtime/panic.go:505 +0x229
domain.com/user/project/vendor/github.com/dgraph-io/badger/skl.(*Skiplist).IncrRef(...)
$HOME/src/go/src/domain.com/user/project/vendor/github.com/dgraph-io/badger/skl/skl.go:86
domain.com/user/project/vendor/github.com/dgraph-io/badger.(*DB).getMemTables(0xc420174700, 0x0, 0x0, 0x0, 0x0)
$HOME/src/go/src/domain.com/user/project/vendor/github.com/dgraph-io/badger/db.go:472 +0xd2
domain.com/user/project/vendor/github.com/dgraph-io/badger.(*Txn).NewIterator(0xc4201685a0, 0x0, 0x64, 0x0, 0x0)
$HOME/src/go/src/domain.com/user/project/vendor/github.com/dgraph-io/badger/iterator.go:324 +0x68
domain.com/user/project/main.TestBadgerClose.func1.1(0xc4201685a0, 0x0, 0x0)
$HOME/src/go/src/domain.com/user/project/badger_close_test.go:32 +0x68
domain.com/user/project/vendor/github.com/dgraph-io/badger.(*DB).Update(0xc420174700, 0xa32aa0, 0x0, 0x0)
$HOME/src/go/src/domain.com/user/project/vendor/github.com/dgraph-io/badger/transaction.go:579 +0x84
domain.com/user/project/main.TestBadgerClose.func1(0xc420174700, 0xc420087e90, 0xc4201960f0)
$HOME/src/go/src/domain.com/user/project/badger_close_test.go:29 +0x3b
domain.com/user/project/main.TestBadgerClose(0xc4201960f0)
$HOME/src/go/src/domain.com/user/project/badger_close_test.go:63 +0x36c
testing.tRunner(0xc4201960f0, 0xa32ab8)
/go/1.10.0/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
/go/1.10.0/src/testing/testing.go:824 +0x2e0
*/
func TestBadgerClose(t *testing.T) {
opts := badger.DefaultOptions
opts.Dir = "/tmp/badger_test"
opts.ValueDir = opts.Dir
if err := os.MkdirAll(opts.Dir, 0775); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(opts.Dir)
db, err := badger.Open(opts)
if err != nil {
t.Fatal(err)
}
defer func() {
err = db.Update(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
if err := txn.Delete(it.Item().Key()); err != nil {
return err
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}()
// Wrong order. Close happens before Update
defer db.Close()
err = db.Update(func(txn *badger.Txn) error {
for i := 0; i < 10; i++ {
if err := txn.Set([]byte{byte(i)}, []byte("value")); err != nil {
return err
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
t.Error("I am a failed test")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment