From 1c66c64a011bac4ae7a9eebab9d388bbc51d9719 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 28 Jul 2014 09:25:40 +0800 Subject: [PATCH] add base test for all store --- store/goleveldb_test.go | 30 ++++++++++++++++++++++ store/leveldb_test.go | 32 +++++++++++++++++++++++ store/lmdb_test.go | 32 +++++++++++++++++++++++ store/rocksdb_test.go | 32 +++++++++++++++++++++++ store/store_test.go | 56 +++++++---------------------------------- 5 files changed, 135 insertions(+), 47 deletions(-) create mode 100644 store/goleveldb_test.go create mode 100644 store/leveldb_test.go create mode 100644 store/lmdb_test.go create mode 100644 store/rocksdb_test.go diff --git a/store/goleveldb_test.go b/store/goleveldb_test.go new file mode 100644 index 0000000..1adbbb9 --- /dev/null +++ b/store/goleveldb_test.go @@ -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() +} diff --git a/store/leveldb_test.go b/store/leveldb_test.go new file mode 100644 index 0000000..b62747f --- /dev/null +++ b/store/leveldb_test.go @@ -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() +} diff --git a/store/lmdb_test.go b/store/lmdb_test.go new file mode 100644 index 0000000..8e70d13 --- /dev/null +++ b/store/lmdb_test.go @@ -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() +} diff --git a/store/rocksdb_test.go b/store/rocksdb_test.go new file mode 100644 index 0000000..471323a --- /dev/null +++ b/store/rocksdb_test.go @@ -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() +} diff --git a/store/store_test.go b/store/store_test.go index b911cb5..f37c496 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -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 TestSimple(t *testing.T) { - db := getTestDB() +func testStore(db *DB, t *testing.T) { + testSimple(db, t) + testBatch(db, t) + testIterator(db, t) +} +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) -}