prysm-pulse/sharding/database/database_test.go
Preston Van Loon 77267169ea Add More Lint Rules With gometalinter (#284)
* turn up linter to 11

* Added extra linting rules and fixed all lint issues

* add deadline of 10m


Former-commit-id: 632999fcaa8370516d6fe76c28c503cb2e3319b7 [formerly d4354f631b1eb4f7918cde28bf063f756ae92a54]
Former-commit-id: 3aaf78a56b76850b0123d61551ac554ccecfe9ad
2018-07-15 15:06:36 -04:00

129 lines
3.1 KiB
Go

package database
import (
"strconv"
"testing"
"github.com/prysmaticlabs/geth-sharding/sharding/types"
logTest "github.com/sirupsen/logrus/hooks/test"
leveldberrors "github.com/syndtr/goleveldb/leveldb/errors"
)
// Verifies that ShardDB implements the sharding Service inteface.
var _ = types.Service(&ShardDB{})
var testDB *ShardDB
func init() {
shardDB, _ := NewShardDB("/tmp/datadir", "shardchaindata", false)
testDB = shardDB
testDB.Start()
}
func TestLifecycle(t *testing.T) {
hook := logTest.NewGlobal()
s, err := NewShardDB("/tmp/datadir", "shardchaindb", false)
if err != nil {
t.Fatalf("could not initialize a new sb: %v", err)
}
s.Start()
msg := hook.LastEntry().Message
if msg != "Starting shardDB service" {
t.Errorf("incorrect log, expected %s, got %s", "Starting shardDB service", msg)
}
s.Stop()
msg = hook.LastEntry().Message
if msg != "Stopping shardDB service" {
t.Errorf("incorrect log, expected %s, got %s", "Stopping shardDB service", msg)
}
// Access DB after it's stopped, this should fail
_, err = s.db.Get([]byte("ralph merkle"))
if err.Error() != "leveldb: closed" {
t.Fatalf("shardDB close function did not work")
}
}
// Testing the concurrency of the shardDB with multiple processes attempting to write.
func Test_DBConcurrent(t *testing.T) {
for i := 0; i < 100; i++ {
go func(val string) {
if err := testDB.db.Put([]byte("ralph merkle"), []byte(val)); err != nil {
t.Errorf("could not save value in db: %v", err)
}
}(strconv.Itoa(i))
}
}
func Test_DBPut(t *testing.T) {
if err := testDB.db.Put([]byte("ralph merkle"), []byte{1, 2, 3}); err != nil {
t.Errorf("could not save value in db: %v", err)
}
}
func Test_DBHas(t *testing.T) {
key := []byte("ralph merkle")
if err := testDB.db.Put(key, []byte{1, 2, 3}); err != nil {
t.Fatalf("could not save value in db: %v", err)
}
has, err := testDB.db.Has(key)
if err != nil {
t.Errorf("could not check if db has key: %v", err)
}
if !has {
t.Errorf("db should have key: %v", key)
}
key2 := []byte{}
has2, err := testDB.db.Has(key2)
if err != nil {
t.Errorf("could not check if db has key: %v", err)
}
if has2 {
t.Errorf("db should not have non-existent key: %v", key2)
}
}
func Test_DBGet(t *testing.T) {
key := []byte("ralph merkle")
if err := testDB.db.Put(key, []byte{1, 2, 3}); err != nil {
t.Fatalf("could not save value in db: %v", err)
}
val, err := testDB.db.Get(key)
if err != nil {
t.Errorf("get failed: %v", err)
}
if len(val) == 0 {
t.Errorf("no value stored for key")
}
key2 := []byte{}
val2, err := testDB.db.Get(key2)
if err == nil || err.Error() != leveldberrors.ErrNotFound.Error() {
t.Errorf("Expected error %v but got %v", leveldberrors.ErrNotFound, err)
}
if len(val2) != 0 {
t.Errorf("non-existent key should not have a value. key=%v, value=%v", key2, val2)
}
}
func Test_DBDelete(t *testing.T) {
key := []byte("ralph merkle")
if err := testDB.db.Put(key, []byte{1, 2, 3}); err != nil {
t.Fatalf("could not save value in db: %v", err)
}
if err := testDB.db.Delete(key); err != nil {
t.Errorf("could not delete key: %v", key)
}
}