Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
9915d3c3
Commit
9915d3c3
authored
Feb 09, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discover: deflake UDP tests
parent
028775a0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
123 additions
and
59 deletions
+123
-59
table.go
p2p/discover/table.go
+2
-1
table_test.go
p2p/discover/table_test.go
+12
-0
udp.go
p2p/discover/udp.go
+2
-3
udp_test.go
p2p/discover/udp_test.go
+107
-55
No files found.
p2p/discover/table.go
View file @
9915d3c3
...
...
@@ -87,9 +87,10 @@ func (tab *Table) Lookup(target NodeID) []*Node {
reply
=
make
(
chan
[]
*
Node
,
alpha
)
pendingQueries
=
0
)
// don't query further if we hit the target.
// don't query further if we hit the target
or ourself
.
// unlikely to happen often in practice.
asked
[
target
]
=
true
asked
[
tab
.
self
.
ID
]
=
true
tab
.
mutex
.
Lock
()
// update last lookup stamp (for refresh logic)
...
...
p2p/discover/table_test.go
View file @
9915d3c3
...
...
@@ -14,6 +14,18 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)
func
TestTable_bumpOrAddBucketAssign
(
t
*
testing
.
T
)
{
tab
:=
newTable
(
nil
,
NodeID
{},
&
net
.
UDPAddr
{})
for
i
:=
1
;
i
<
len
(
tab
.
buckets
);
i
++
{
tab
.
bumpOrAdd
(
randomID
(
tab
.
self
.
ID
,
i
),
&
net
.
UDPAddr
{})
}
for
i
,
b
:=
range
tab
.
buckets
{
if
i
>
0
&&
len
(
b
.
entries
)
!=
1
{
t
.
Errorf
(
"bucket %d has %d entries, want 1"
,
i
,
len
(
b
.
entries
))
}
}
}
func
TestTable_bumpOrAddPingReplace
(
t
*
testing
.
T
)
{
pingC
:=
make
(
pingC
)
tab
:=
newTable
(
pingC
,
NodeID
{},
&
net
.
UDPAddr
{})
...
...
p2p/discover/udp.go
View file @
9915d3c3
...
...
@@ -179,10 +179,9 @@ func (t *udp) findnode(to *Node, target NodeID) ([]*Node, error) {
nreceived
:=
0
errc
:=
t
.
pending
(
to
.
ID
,
neighborsPacket
,
func
(
r
interface
{})
bool
{
reply
:=
r
.
(
*
neighbors
)
for
i
:=
0
;
i
<
len
(
reply
.
Nodes
);
i
++
{
for
_
,
n
:=
range
reply
.
Nodes
{
nreceived
++
n
:=
reply
.
Nodes
[
i
]
if
n
.
ID
!=
t
.
self
.
ID
&&
n
.
isValid
()
{
if
n
.
isValid
()
{
nodes
=
append
(
nodes
,
n
)
}
}
...
...
p2p/discover/udp_test.go
View file @
9915d3c3
package
discover
import
(
"fmt"
logpkg
"log"
"net"
"os"
"reflect"
"testing"
"time"
...
...
@@ -53,16 +53,37 @@ func TestUDP_findnode(t *testing.T) {
defer
n1
.
Close
()
defer
n2
.
Close
()
entry
:=
MustParseNode
(
"enode://9d8a19597e312ef32d76e6b4903bb43d7bcd892d17b769d30b404bd3a4c2dca6c86184b17d0fdeeafe3b01e0e912d990ddc853db3f325d5419f31446543c30be@127.0.0.1:54194"
)
n2
.
add
([]
*
Node
{
entry
})
// put a few nodes into n2. the exact distribution shouldn't
// matter much, altough we need to take care not to overflow
// any bucket.
target
:=
randomID
(
n1
.
self
.
ID
,
100
)
nodes
:=
&
nodesByDistance
{
target
:
target
}
for
i
:=
0
;
i
<
bucketSize
;
i
++
{
n2
.
add
([]
*
Node
{
&
Node
{
IP
:
net
.
IP
{
1
,
2
,
3
,
byte
(
i
)},
DiscPort
:
i
+
2
,
TCPPort
:
i
+
2
,
ID
:
randomID
(
n2
.
self
.
ID
,
i
+
2
),
}})
}
n2
.
add
(
nodes
.
entries
)
n2
.
bumpOrAdd
(
n1
.
self
.
ID
,
&
net
.
UDPAddr
{
IP
:
n1
.
self
.
IP
,
Port
:
n1
.
self
.
DiscPort
})
expected
:=
n2
.
closest
(
target
,
bucketSize
)
err
:=
runUDP
(
10
,
func
()
error
{
result
,
_
:=
n1
.
net
.
findnode
(
n2
.
self
,
target
)
if
len
(
result
)
!=
1
{
t
.
Fatalf
(
"wrong number of results: got %d, want 1"
,
len
(
result
))
if
len
(
result
)
!=
bucketSize
{
return
fmt
.
Errorf
(
"wrong number of results: got %d, want %d"
,
len
(
result
),
bucketSize
)
}
for
i
:=
range
result
{
if
result
[
i
]
.
ID
!=
expected
.
entries
[
i
]
.
ID
{
return
fmt
.
Errorf
(
"result mismatch at %d:
\n
got: %v
\n
want: %v"
,
i
,
result
[
i
],
expected
.
entries
[
i
])
}
if
!
reflect
.
DeepEqual
(
result
[
0
],
entry
)
{
t
.
Errorf
(
"wrong result: got %v, want %v"
,
result
[
0
],
entry
)
}
return
nil
})
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
...
...
@@ -101,6 +122,7 @@ func TestUDP_findnodeMultiReply(t *testing.T) {
defer
n1
.
Close
()
defer
n2
.
Close
()
err
:=
runUDP
(
10
,
func
()
error
{
nodes
:=
make
([]
*
Node
,
bucketSize
)
for
i
:=
range
nodes
{
nodes
[
i
]
=
&
Node
{
...
...
@@ -113,13 +135,15 @@ func TestUDP_findnodeMultiReply(t *testing.T) {
// ask N2 for neighbors. it will send an empty reply back.
// the request will wait for up to bucketSize replies.
resultC
:=
make
(
chan
[]
*
Node
)
resultc
:=
make
(
chan
[]
*
Node
)
errc
:=
make
(
chan
error
)
go
func
()
{
ns
,
err
:=
n1
.
net
.
findnode
(
n2
.
self
,
n1
.
self
.
ID
)
if
err
!=
nil
{
t
.
Error
(
"findnode error:"
,
err
)
errc
<-
err
}
else
{
resultc
<-
ns
}
resultC
<-
ns
}()
// send a few more neighbors packets to N1.
...
...
@@ -138,12 +162,17 @@ func TestUDP_findnodeMultiReply(t *testing.T) {
// check that they are all returned. we cannot just check for
// equality because they might not be returned in the order they
// were sent.
result
:=
<-
resultC
var
result
[]
*
Node
select
{
case
result
=
<-
resultc
:
case
err
:=
<-
errc
:
return
err
}
if
hasDuplicates
(
result
)
{
t
.
Error
(
"result slice contains duplicates"
)
return
fmt
.
Errorf
(
"result slice contains duplicates"
)
}
if
len
(
result
)
!=
len
(
nodes
)
{
t
.
Errorf
(
"wrong number of nodes returned: got %d, want %d"
,
len
(
result
),
len
(
nodes
))
return
fm
t
.
Errorf
(
"wrong number of nodes returned: got %d, want %d"
,
len
(
result
),
len
(
nodes
))
}
matched
:=
make
(
map
[
NodeID
]
bool
)
for
_
,
n
:=
range
result
{
...
...
@@ -154,6 +183,29 @@ func TestUDP_findnodeMultiReply(t *testing.T) {
}
}
if
len
(
matched
)
!=
len
(
nodes
)
{
t
.
Errorf
(
"wrong number of matching nodes: got %d, want %d"
,
len
(
matched
),
len
(
nodes
))
return
fm
t
.
Errorf
(
"wrong number of matching nodes: got %d, want %d"
,
len
(
matched
),
len
(
nodes
))
}
return
nil
})
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
// runUDP runs a test n times and returns an error if the test failed
// in all n runs. This is necessary because UDP is unreliable even for
// connections on the local machine, causing test failures.
func
runUDP
(
n
int
,
test
func
()
error
)
error
{
errcount
:=
0
errors
:=
""
for
i
:=
0
;
i
<
n
;
i
++
{
if
err
:=
test
();
err
!=
nil
{
errors
+=
fmt
.
Sprintf
(
"
\n
#%d: %v"
,
i
,
err
)
errcount
++
}
}
if
errcount
==
n
{
return
fmt
.
Errorf
(
"failed on all %d iterations:%s"
,
n
,
errors
)
}
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment