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 ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"sync"
"testing" "testing"
) )
var testOnce sync.Once func TestStore(t *testing.T) {
var testDB *DB
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) func testStore(db *DB, t *testing.T) {
return testDB testSimple(db, t)
testBatch(db, t)
testIterator(db, t)
} }
func TestSimple(t *testing.T) { func testSimple(db *DB, t *testing.T) {
db := getTestDB()
key := []byte("key") key := []byte("key")
value := []byte("hello world") value := []byte("hello world")
if err := db.Put(key, value); err != nil { if err := db.Put(key, value); err != nil {
@ -54,9 +39,7 @@ func TestSimple(t *testing.T) {
} }
} }
func TestBatch(t *testing.T) { func testBatch(db *DB, t *testing.T) {
db := getTestDB()
key1 := []byte("key1") key1 := []byte("key1")
key2 := []byte("key2") key2 := []byte("key2")
@ -122,9 +105,7 @@ func checkIterator(it *RangeLimitIterator, cv ...int) error {
return nil return nil
} }
func TestIterator(t *testing.T) { func testIterator(db *DB, t *testing.T) {
db := getTestDB()
i := db.NewIterator() i := db.NewIterator()
for i.SeekToFirst(); i.Valid(); i.Next() { for i.SeekToFirst(); i.Valid(); i.Next() {
db.Delete(i.Key()) db.Delete(i.Key())
@ -219,22 +200,3 @@ func TestIterator(t *testing.T) {
} }
it.Close() 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)
}