Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
653841f2
Commit
653841f2
authored
Aug 11, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add fauxNet to stand in for Swarm in tests to reproduce various network conditions
parent
10ef87b6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
11 deletions
+57
-11
dht_test.go
dht_test.go
+2
-2
ext_test.go
ext_test.go
+55
-9
No files found.
dht_test.go
View file @
653841f2
...
...
@@ -208,7 +208,7 @@ func TestProvides(t *testing.T) {
func
TestLayeredGet
(
t
*
testing
.
T
)
{
u
.
Debug
=
false
addrs
,
_
,
dhts
:=
setupDHTS
(
4
,
t
)
addrs
,
_
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
err
:=
dhts
[
0
]
.
Connect
(
addrs
[
1
])
if
err
!=
nil
{
...
...
@@ -254,7 +254,7 @@ func TestLayeredGet(t *testing.T) {
func
TestFindPeer
(
t
*
testing
.
T
)
{
u
.
Debug
=
false
addrs
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
addrs
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
err
:=
dhts
[
0
]
.
Connect
(
addrs
[
1
])
if
err
!=
nil
{
...
...
ext_test.go
View file @
653841f2
...
...
@@ -3,12 +3,13 @@ package dht
import
(
"testing"
"code.google.com/p/goprotobuf/proto"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
swarm
"github.com/jbenet/go-ipfs/swarm"
//ma "github.com/jbenet/go-multiaddr"
u
"github.com/jbenet/go-ipfs/util"
ma
"github.com/jbenet/go-multiaddr"
"fmt"
"time"
)
...
...
@@ -36,7 +37,7 @@ func (f *fauxNet) Listen() error {
for
{
select
{
case
in
:=
<-
f
.
Chan
.
Outgoing
:
for
_
,
h
:=
range
f
.
handlers
{
for
_
,
h
:=
range
f
.
handlers
{
reply
:=
h
(
in
)
if
reply
!=
nil
{
f
.
Chan
.
Incoming
<-
reply
...
...
@@ -54,24 +55,69 @@ func (f *fauxNet) AddHandler(fn func(*swarm.Message) *swarm.Message) {
}
func
(
f
*
fauxNet
)
Send
(
mes
*
swarm
.
Message
)
{
f
.
Chan
.
Outgoing
<-
mes
}
func
(
f
*
fauxNet
)
GetChan
()
*
swarm
.
Chan
{
return
f
.
Chan
}
func
TestGetFailure
(
t
*
testing
.
T
)
{
func
(
f
*
fauxNet
)
Connect
(
addr
*
ma
.
Multiaddr
)
(
*
peer
.
Peer
,
error
)
{
return
nil
,
nil
}
func
TestGetFailures
(
t
*
testing
.
T
)
{
fn
:=
newFauxNet
()
fn
.
Listen
()
local
:=
new
(
peer
.
Peer
)
local
.
ID
=
peer
.
ID
(
[]
byte
(
"test_peer"
)
)
local
.
ID
=
peer
.
ID
(
"test_peer"
)
d
:=
NewDHT
(
local
,
fn
)
other
:=
&
peer
.
Peer
{
ID
:
peer
.
ID
(
"other_peer"
)}
d
.
Start
()
b
,
err
:=
d
.
GetValue
(
u
.
Key
(
"test"
),
time
.
Second
)
d
.
Update
(
other
)
// This one should time out
_
,
err
:=
d
.
GetValue
(
u
.
Key
(
"test"
),
time
.
Millisecond
*
5
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
nerr
,
ok
:=
err
.
(
*
u
.
IpfsError
)
if
!
ok
{
t
.
Fatal
(
"Got different error than we expected."
)
}
if
nerr
.
Inner
!=
u
.
ErrTimeout
{
t
.
Fatal
(
"Got different error than we expected."
)
}
}
else
{
t
.
Fatal
(
"Did not get expected error!"
)
}
fmt
.
Println
(
b
)
fn
.
AddHandler
(
func
(
mes
*
swarm
.
Message
)
*
swarm
.
Message
{
pmes
:=
new
(
PBDHTMessage
)
err
:=
proto
.
Unmarshal
(
mes
.
Data
,
pmes
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
resp
:=
DHTMessage
{
Type
:
pmes
.
GetType
(),
Id
:
pmes
.
GetId
(),
Response
:
true
,
Success
:
false
,
}
return
swarm
.
NewMessage
(
mes
.
Peer
,
resp
.
ToProtobuf
())
})
// This one should fail with NotFound
_
,
err
=
d
.
GetValue
(
u
.
Key
(
"test"
),
time
.
Millisecond
*
5
)
if
err
!=
nil
{
if
err
!=
u
.
ErrNotFound
{
t
.
Fatal
(
"Expected ErrNotFound, got: %s"
,
err
)
}
}
else
{
t
.
Fatal
(
"expected error, got none."
)
}
}
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