diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-05-22 00:41:46 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-05-22 00:41:46 +0800 |
commit | cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a (patch) | |
tree | a016eb8074805db7c81c5e3aba52bccc51884db6 /p2p/discover/database_test.go | |
parent | af24c271c7a757f80968a4d50a65a416e2ac9cdd (diff) | |
download | go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar.gz go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar.bz2 go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar.lz go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar.xz go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.tar.zst go-tangerine-cbd3ae6906ece36b1b3e5e7af4d7cb55e784818a.zip |
p2p/discover: fix #838, evacuate self entries from the node db
Diffstat (limited to 'p2p/discover/database_test.go')
-rw-r--r-- | p2p/discover/database_test.go | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/p2p/discover/database_test.go b/p2p/discover/database_test.go index 9c543cd5f..88f5d2155 100644 --- a/p2p/discover/database_test.go +++ b/p2p/discover/database_test.go @@ -63,7 +63,7 @@ var nodeDBInt64Tests = []struct { } func TestNodeDBInt64(t *testing.T) { - db, _ := newNodeDB("", Version) + db, _ := newNodeDB("", Version, NodeID{}) defer db.close() tests := nodeDBInt64Tests @@ -94,7 +94,7 @@ func TestNodeDBFetchStore(t *testing.T) { ) inst := time.Now() - db, _ := newNodeDB("", Version) + db, _ := newNodeDB("", Version, NodeID{}) defer db.close() // Check fetch/store operations on a node ping object @@ -165,7 +165,7 @@ var nodeDBSeedQueryNodes = []struct { } func TestNodeDBSeedQuery(t *testing.T) { - db, _ := newNodeDB("", Version) + db, _ := newNodeDB("", Version, NodeID{}) defer db.close() // Insert a batch of nodes for querying @@ -205,7 +205,7 @@ func TestNodeDBSeedQuery(t *testing.T) { } func TestNodeDBSeedQueryContinuation(t *testing.T) { - db, _ := newNodeDB("", Version) + db, _ := newNodeDB("", Version, NodeID{}) defer db.close() // Insert a batch of nodes for querying @@ -230,6 +230,32 @@ func TestNodeDBSeedQueryContinuation(t *testing.T) { } } +func TestNodeDBSelfSeedQuery(t *testing.T) { + // Assign a node as self to verify evacuation + self := nodeDBSeedQueryNodes[0].node.ID + db, _ := newNodeDB("", Version, self) + defer db.close() + + // Insert a batch of nodes for querying + for i, seed := range nodeDBSeedQueryNodes { + if err := db.updateNode(seed.node); err != nil { + t.Fatalf("node %d: failed to insert: %v", i, err) + } + } + // Retrieve the entire batch and check that self was evacuated + seeds := db.querySeeds(2 * len(nodeDBSeedQueryNodes)) + if len(seeds) != len(nodeDBSeedQueryNodes)-1 { + t.Errorf("seed count mismatch: have %v, want %v", len(seeds), len(nodeDBSeedQueryNodes)-1) + } + have := make(map[NodeID]struct{}) + for _, seed := range seeds { + have[seed.ID] = struct{}{} + } + if _, ok := have[self]; ok { + t.Errorf("self not evacuated") + } +} + func TestNodeDBPersistency(t *testing.T) { root, err := ioutil.TempDir("", "nodedb-") if err != nil { @@ -243,7 +269,7 @@ func TestNodeDBPersistency(t *testing.T) { ) // Create a persistent database and store some values - db, err := newNodeDB(filepath.Join(root, "database"), Version) + db, err := newNodeDB(filepath.Join(root, "database"), Version, NodeID{}) if err != nil { t.Fatalf("failed to create persistent database: %v", err) } @@ -253,7 +279,7 @@ func TestNodeDBPersistency(t *testing.T) { db.close() // Reopen the database and check the value - db, err = newNodeDB(filepath.Join(root, "database"), Version) + db, err = newNodeDB(filepath.Join(root, "database"), Version, NodeID{}) if err != nil { t.Fatalf("failed to open persistent database: %v", err) } @@ -263,7 +289,7 @@ func TestNodeDBPersistency(t *testing.T) { db.close() // Change the database version and check flush - db, err = newNodeDB(filepath.Join(root, "database"), Version+1) + db, err = newNodeDB(filepath.Join(root, "database"), Version+1, NodeID{}) if err != nil { t.Fatalf("failed to open persistent database: %v", err) } @@ -300,7 +326,7 @@ var nodeDBExpirationNodes = []struct { } func TestNodeDBExpiration(t *testing.T) { - db, _ := newNodeDB("", Version) + db, _ := newNodeDB("", Version, NodeID{}) defer db.close() // Add all the test nodes and set their last pong time @@ -323,3 +349,34 @@ func TestNodeDBExpiration(t *testing.T) { } } } + +func TestNodeDBSelfExpiration(t *testing.T) { + // Find a node in the tests that shouldn't expire, and assign it as self + var self NodeID + for _, node := range nodeDBExpirationNodes { + if !node.exp { + self = node.node.ID + break + } + } + db, _ := newNodeDB("", Version, self) + defer db.close() + + // Add all the test nodes and set their last pong time + for i, seed := range nodeDBExpirationNodes { + if err := db.updateNode(seed.node); err != nil { + t.Fatalf("node %d: failed to insert: %v", i, err) + } + if err := db.updateLastPong(seed.node.ID, seed.pong); err != nil { + t.Fatalf("node %d: failed to update pong: %v", i, err) + } + } + // Expire the nodes and make sure self has been evacuated too + if err := db.expireNodes(); err != nil { + t.Fatalf("failed to expire nodes: %v", err) + } + node := db.node(self) + if node != nil { + t.Errorf("self not evacuated") + } +} |