diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-27 22:38:28 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-27 22:38:28 +0800 |
commit | 437cf4b3acf1b5a4efde64aacaacdf14289010d1 (patch) | |
tree | aea6cf153def4fae3ef217ac9112334fc8de2067 /p2p/discover/database_test.go | |
parent | a136e2bb222ed0eaa9b5e5a31a07fcc664de8eb7 (diff) | |
download | dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar.gz dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar.bz2 dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar.lz dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar.xz dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.tar.zst dexon-437cf4b3acf1b5a4efde64aacaacdf14289010d1.zip |
p2p/discover: add node expirer and related tests
Diffstat (limited to 'p2p/discover/database_test.go')
-rw-r--r-- | p2p/discover/database_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/p2p/discover/database_test.go b/p2p/discover/database_test.go index 0412a4770..f327cf73b 100644 --- a/p2p/discover/database_test.go +++ b/p2p/discover/database_test.go @@ -264,3 +264,50 @@ func TestNodeDBPersistency(t *testing.T) { } db.close() } + +var nodeDBExpirationNodes = []struct { + node Node + pong time.Time + exp bool +}{ + { + node: Node{ + ID: MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + IP: []byte{127, 0, 0, 1}, + }, + pong: time.Now().Add(-nodeDBNodeExpiration + time.Minute), + exp: false, + }, { + node: Node{ + ID: MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), + IP: []byte{127, 0, 0, 2}, + }, + pong: time.Now().Add(-nodeDBNodeExpiration - time.Minute), + exp: true, + }, +} + +func TestNodeDBExpiration(t *testing.T) { + db, _ := newNodeDB("", Version) + 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 some of them, and check the rest + if err := db.expireNodes(); err != nil { + t.Fatalf("failed to expire nodes: %v", err) + } + for i, seed := range nodeDBExpirationNodes { + node := db.node(seed.node.ID) + if (node == nil && !seed.exp) || (node != nil && seed.exp) { + t.Errorf("node %d: expiration mismatch: have %v, want %v", i, node, seed.exp) + } + } +} |