add base test for all store

This commit is contained in:
siddontang 2014-07-28 09:25:40 +08:00
parent d1362552c0
commit 1c66c64a01
5 changed files with 135 additions and 47 deletions

30
store/goleveldb_test.go Normal file
View File

@ -0,0 +1,30 @@
package store
import (
"os"
"testing"
)
func newTestGoLevelDB() *DB {
cfg := new(Config)
cfg.Name = GoLevelDBName
cfg.Path = "/tmp/testdb/goleveldb"
os.RemoveAll(cfg.Path)
db, err := Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
return db
}
func TestGoLevelDB(t *testing.T) {
db := newTestGoLevelDB()
testStore(db, t)
db.Close()
}

32
store/leveldb_test.go Normal file
View File

@ -0,0 +1,32 @@
// +build leveldb
package store
import (
"os"
"testing"
)
func newTestLevelDB() *DB {
cfg := new(Config)
cfg.Name = LevelDBName
cfg.Path = "/tmp/testdb/leveldb"
os.RemoveAll(cfg.Path)
db, err := Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
return db
}
func TestLevelDB(t *testing.T) {
db := newTestLevelDB()
testStore(db, t)
db.Close()
}

32
store/lmdb_test.go Normal file
View File

@ -0,0 +1,32 @@
package store
import (
"os"
"testing"
)
func newTestLMDB() *DB {
cfg := new(Config)
cfg.Name = LMDBName
cfg.Path = "/tmp/testdb/lmdb"
cfg.MapSize = 20 * 1024 * 1024
os.RemoveAll(cfg.Path)
db, err := Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
return db
}
func TestLMDB(t *testing.T) {
db := newTestLMDB()
testStore(db, t)
db.Close()
}

32
store/rocksdb_test.go Normal file
View File

@ -0,0 +1,32 @@
// +build rocksdb
package store
import (
"os"
"testing"
)
func newTestRocksDB() *DB {
cfg := new(Config)
cfg.Name = RocksDBName
cfg.Path = "/tmp/testdb/rocksdb"
os.RemoveAll(cfg.Path)
db, err := Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
return db
}
func TestRocksDB(t *testing.T) {
db := newTestRocksDB()
testStore(db, t)
db.Close()
}

View File

@ -3,35 +3,20 @@ package store
import (
"bytes"
"fmt"
"os"
"sync"
"testing"
)
var testOnce sync.Once
var testDB *DB
func TestStore(t *testing.T) {
func getTestDB() *DB {
f := func() {
var err error
cfg := new(Config)
cfg.Path = "/tmp/testdb"
testDB, err = Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
}
testOnce.Do(f)
return testDB
func testStore(db *DB, t *testing.T) {
testSimple(db, t)
testBatch(db, t)
testIterator(db, t)
}
func TestSimple(t *testing.T) {
db := getTestDB()
func testSimple(db *DB, t *testing.T) {
key := []byte("key")
value := []byte("hello world")
if err := db.Put(key, value); err != nil {
@ -54,9 +39,7 @@ func TestSimple(t *testing.T) {
}
}
func TestBatch(t *testing.T) {
db := getTestDB()
func testBatch(db *DB, t *testing.T) {
key1 := []byte("key1")
key2 := []byte("key2")
@ -122,9 +105,7 @@ func checkIterator(it *RangeLimitIterator, cv ...int) error {
return nil
}
func TestIterator(t *testing.T) {
db := getTestDB()
func testIterator(db *DB, t *testing.T) {
i := db.NewIterator()
for i.SeekToFirst(); i.Valid(); i.Next() {
db.Delete(i.Key())
@ -219,22 +200,3 @@ func TestIterator(t *testing.T) {
}
it.Close()
}
func TestCloseMore(t *testing.T) {
cfg := new(Config)
cfg.Path = "/tmp/testdb1234"
cfg.CacheSize = 4 * 1024 * 1024
os.RemoveAll(cfg.Path)
for i := 0; i < 100; i++ {
db, err := Open(cfg)
if err != nil {
t.Fatal(err)
}
db.Put([]byte("key"), []byte("value"))
db.Close()
}
os.RemoveAll(cfg.Path)
}