aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/table_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-05-21 08:11:41 +0800
committerFelix Lange <fjl@twurst.com>2015-05-25 07:17:14 +0800
commit9f38ef5d970d1ccb50d2a7697562ea547ff625c8 (patch)
tree2bfcfd4d9d8b778328f02f5ad9b2582d7b8eb67f /p2p/discover/table_test.go
parent64564da20b24f465abfa5bd95fc9deb1c32ec640 (diff)
downloaddexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar.gz
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar.bz2
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar.lz
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar.xz
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.tar.zst
dexon-9f38ef5d970d1ccb50d2a7697562ea547ff625c8.zip
p2p/discover: add ReadRandomNodes
Diffstat (limited to 'p2p/discover/table_test.go')
-rw-r--r--p2p/discover/table_test.go35
1 files changed, 34 insertions, 1 deletions
diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go
index aa5267928..da398d137 100644
--- a/p2p/discover/table_test.go
+++ b/p2p/discover/table_test.go
@@ -210,6 +210,36 @@ func TestTable_closest(t *testing.T) {
}
}
+func TestTable_ReadRandomNodesGetAll(t *testing.T) {
+ cfg := &quick.Config{
+ MaxCount: 200,
+ Rand: quickrand,
+ Values: func(args []reflect.Value, rand *rand.Rand) {
+ args[0] = reflect.ValueOf(make([]*Node, rand.Intn(1000)))
+ },
+ }
+ test := func(buf []*Node) bool {
+ tab := newTable(nil, NodeID{}, &net.UDPAddr{}, "")
+ for i := 0; i < len(buf); i++ {
+ ld := quickrand.Intn(len(tab.buckets))
+ tab.add([]*Node{nodeAtDistance(tab.self.sha, ld)})
+ }
+ gotN := tab.ReadRandomNodes(buf)
+ if gotN != tab.len() {
+ t.Errorf("wrong number of nodes, got %d, want %d", gotN, tab.len())
+ return false
+ }
+ if hasDuplicates(buf[:gotN]) {
+ t.Errorf("result contains duplicates")
+ return false
+ }
+ return true
+ }
+ if err := quick.Check(test, cfg); err != nil {
+ t.Error(err)
+ }
+}
+
type closeTest struct {
Self NodeID
Target common.Hash
@@ -517,7 +547,10 @@ func (n *preminedTestnet) mine(target NodeID) {
func hasDuplicates(slice []*Node) bool {
seen := make(map[NodeID]bool)
- for _, e := range slice {
+ for i, e := range slice {
+ if e == nil {
+ panic(fmt.Sprintf("nil *Node at %d", i))
+ }
if seen[e.ID] {
return true
}