aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/database_test.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-27 20:28:17 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-27 20:28:17 +0800
commita136e2bb222ed0eaa9b5e5a31a07fcc664de8eb7 (patch)
treea2daed4ae55908b685efdbca7f230d3e5ce55dc1 /p2p/discover/database_test.go
parent75fd738dea37387e465e8fc05b163adfb5ab34ce (diff)
downloadgo-tangerine-a136e2bb222ed0eaa9b5e5a31a07fcc664de8eb7.tar.gz
go-tangerine-a136e2bb222ed0eaa9b5e5a31a07fcc664de8eb7.tar.zst
go-tangerine-a136e2bb222ed0eaa9b5e5a31a07fcc664de8eb7.zip
p2p/discover: parametrize nodedb version, add persistency tests
Diffstat (limited to 'p2p/discover/database_test.go')
-rw-r--r--p2p/discover/database_test.go59
1 files changed, 55 insertions, 4 deletions
diff --git a/p2p/discover/database_test.go b/p2p/discover/database_test.go
index b067d458d..0412a4770 100644
--- a/p2p/discover/database_test.go
+++ b/p2p/discover/database_test.go
@@ -2,7 +2,10 @@ package discover
import (
"bytes"
+ "io/ioutil"
"net"
+ "os"
+ "path/filepath"
"testing"
"time"
)
@@ -59,7 +62,8 @@ var nodeDBInt64Tests = []struct {
}
func TestNodeDBInt64(t *testing.T) {
- db, _ := newNodeDB("")
+ db, _ := newNodeDB("", Version)
+ defer db.close()
tests := nodeDBInt64Tests
for i := 0; i < len(tests); i++ {
@@ -87,7 +91,9 @@ func TestNodeDBFetchStore(t *testing.T) {
TCPPort: 30303,
}
inst := time.Now()
- db, _ := newNodeDB("")
+
+ db, _ := newNodeDB("", Version)
+ defer db.close()
// Check fetch/store operations on a node ping object
if stored := db.lastPing(node.ID); stored.Unix() != 0 {
@@ -151,7 +157,8 @@ var nodeDBSeedQueryNodes = []struct {
}
func TestNodeDBSeedQuery(t *testing.T) {
- db, _ := newNodeDB("")
+ db, _ := newNodeDB("", Version)
+ defer db.close()
// Insert a batch of nodes for querying
for i, seed := range nodeDBSeedQueryNodes {
@@ -190,7 +197,8 @@ func TestNodeDBSeedQuery(t *testing.T) {
}
func TestNodeDBSeedQueryContinuation(t *testing.T) {
- db, _ := newNodeDB("")
+ db, _ := newNodeDB("", Version)
+ defer db.close()
// Insert a batch of nodes for querying
for i, seed := range nodeDBSeedQueryNodes {
@@ -213,3 +221,46 @@ func TestNodeDBSeedQueryContinuation(t *testing.T) {
}
}
}
+
+func TestNodeDBPersistency(t *testing.T) {
+ root, err := ioutil.TempDir("", "nodedb-")
+ if err != nil {
+ t.Fatalf("failed to create temporary data folder: %v", err)
+ }
+ defer os.RemoveAll(root)
+
+ var (
+ testKey = []byte("somekey")
+ testInt = int64(314)
+ )
+
+ // Create a persistent database and store some values
+ db, err := newNodeDB(filepath.Join("root", "database"), Version)
+ if err != nil {
+ t.Fatalf("failed to create persistent database: %v", err)
+ }
+ if err := db.storeInt64(testKey, testInt); err != nil {
+ t.Fatalf("failed to store value: %v.", err)
+ }
+ db.close()
+
+ // Reopen the database and check the value
+ db, err = newNodeDB(filepath.Join("root", "database"), Version)
+ if err != nil {
+ t.Fatalf("failed to open persistent database: %v", err)
+ }
+ if val := db.fetchInt64(testKey); val != testInt {
+ t.Fatalf("value mismatch: have %v, want %v", val, testInt)
+ }
+ db.close()
+
+ // Change the database version and check flush
+ db, err = newNodeDB(filepath.Join("root", "database"), Version+1)
+ if err != nil {
+ t.Fatalf("failed to open persistent database: %v", err)
+ }
+ if val := db.fetchInt64(testKey); val != 0 {
+ t.Fatalf("value mismatch: have %v, want %v", val, 0)
+ }
+ db.close()
+}