mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 06:06:49 +00:00

* [NOD-885] Create database.Key type * [NOD-885] Rename FullKey()->FullKeyBytes() and Key()->KeyBytes() * [NOD-885] Make Key.String return a hex string * [NOD-885] Rename key parts * [NOD-885] Rename separator->bucketSeparator * [NOD-885] Rename SuffixBytes->Suffix and PrefixBytes->Prefix * [NOD-885] Change comments * [NOD-885] Change key prefix to bucket * [NOD-885] Don't use database.NewKey inside dbaccess * [NOD-885] Fix nil bug in Bucket.Path() * [NOD-885] Rename helpers.go -> keys.go * [NOD-885] Unexport database.NewKey * [NOD-885] Remove redundant code in Bucket.Path()
163 lines
4.2 KiB
Go
163 lines
4.2 KiB
Go
package ldb
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/database"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestLevelDBSanity(t *testing.T) {
|
|
// Open a test db
|
|
path, err := ioutil.TempDir("", "TestLevelDBSanity")
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBSanity: TempDir unexpectedly "+
|
|
"failed: %s", err)
|
|
}
|
|
ldb, err := NewLevelDB(path)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBSanity: NewLevelDB "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
defer func() {
|
|
err := ldb.Close()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBSanity: Close "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
}()
|
|
|
|
// Put something into the db
|
|
key := database.MakeBucket().Key([]byte("key"))
|
|
putData := []byte("Hello world!")
|
|
err = ldb.Put(key, putData)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBSanity: Put returned "+
|
|
"unexpected error: %s", err)
|
|
}
|
|
|
|
// Get from the key previously put to
|
|
getData, err := ldb.Get(key)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBSanity: Get returned "+
|
|
"unexpected error: %s", err)
|
|
}
|
|
|
|
// Make sure that the put data and the get data are equal
|
|
if !reflect.DeepEqual(getData, putData) {
|
|
t.Fatalf("TestLevelDBSanity: get data and "+
|
|
"put data are not equal. Put: %s, got: %s",
|
|
string(putData), string(getData))
|
|
}
|
|
}
|
|
|
|
func TestLevelDBTransactionSanity(t *testing.T) {
|
|
// Open a test db
|
|
path, err := ioutil.TempDir("", "TestLevelDBTransactionSanity")
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: TempDir unexpectedly "+
|
|
"failed: %s", err)
|
|
}
|
|
ldb, err := NewLevelDB(path)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: NewLevelDB "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
defer func() {
|
|
err := ldb.Close()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Close "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
}()
|
|
|
|
// Case 1. Write in tx and then read directly from the DB
|
|
// Begin a new transaction
|
|
tx, err := ldb.Begin()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Begin "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
|
|
// Put something into the transaction
|
|
key := database.MakeBucket().Key([]byte("key"))
|
|
putData := []byte("Hello world!")
|
|
err = tx.Put(key, putData)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Put "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
|
|
// Get from the key previously put to. Since the tx is not
|
|
// yet committed, this should return ErrNotFound.
|
|
getData, err := ldb.Get(key)
|
|
if err == nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Get " +
|
|
"unexpectedly succeeded")
|
|
}
|
|
if !database.IsNotFoundError(err) {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Get "+
|
|
"returned wrong error: %s", err)
|
|
}
|
|
|
|
// Commit the transaction
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Commit "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
|
|
// Get from the key previously put to. Now that the tx was
|
|
// committed, this should succeed.
|
|
getData, err = ldb.Get(key)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Get "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
|
|
// Make sure that the put data and the get data are equal
|
|
if !reflect.DeepEqual(getData, putData) {
|
|
t.Fatalf("TestLevelDBTransactionSanity: get "+
|
|
"data and put data are not equal. Put: %s, got: %s",
|
|
string(putData), string(getData))
|
|
}
|
|
|
|
// Case 2. Write directly to the DB and then read from a tx
|
|
// Put something into the db
|
|
key = database.MakeBucket().Key([]byte("key2"))
|
|
putData = []byte("Goodbye world!")
|
|
err = ldb.Put(key, putData)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Put "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
|
|
// Begin a new transaction
|
|
tx, err = ldb.Begin()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Begin "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
|
|
// Get from the key previously put to
|
|
getData, err = tx.Get(key)
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: Get "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
|
|
// Make sure that the put data and the get data are equal
|
|
if !reflect.DeepEqual(getData, putData) {
|
|
t.Fatalf("TestLevelDBTransactionSanity: get "+
|
|
"data and put data are not equal. Put: %s, got: %s",
|
|
string(putData), string(getData))
|
|
}
|
|
|
|
// Rollback the transaction
|
|
err = tx.Rollback()
|
|
if err != nil {
|
|
t.Fatalf("TestLevelDBTransactionSanity: rollback "+
|
|
"returned unexpected error: %s", err)
|
|
}
|
|
}
|