mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
035d0fb669
Former-commit-id: 44c0c5682c0296d94943b354171e69b4c8cf5312 [formerly 54da5cb45b098f0737fb3c37964e612dfe93c751] Former-commit-id: 5d84d3c5038a01c4108e519d5a5c69033ace8ae2
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package database
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
)
|
|
|
|
var db ethdb.Database
|
|
|
|
func init() {
|
|
shardDB, err := NewShardDB("/tmp/datadir", "shardchaindata")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db = shardDB
|
|
}
|
|
|
|
// 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 := 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 := 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 := db.Put(key, []byte{1, 2, 3}); err != nil {
|
|
t.Fatalf("could not save value in db: %v", err)
|
|
}
|
|
|
|
has, err := 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 := 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 := db.Put(key, []byte{1, 2, 3}); err != nil {
|
|
t.Fatalf("could not save value in db: %v", err)
|
|
}
|
|
|
|
val, err := 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 := db.Get(key2)
|
|
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 := db.Put(key, []byte{1, 2, 3}); err != nil {
|
|
t.Fatalf("could not save value in db: %v", err)
|
|
}
|
|
|
|
if err := db.Delete(key); err != nil {
|
|
t.Errorf("could not delete key: %v", key)
|
|
}
|
|
}
|