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
10ef87b6
Commit
10ef87b6
authored
Aug 10, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
starting a new testing framework
parent
e6ab2e20
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
68 deletions
+98
-68
dht.go
dht.go
+3
-12
dht_test.go
dht_test.go
+18
-56
ext_test.go
ext_test.go
+77
-0
No files found.
dht.go
View file @
10ef87b6
...
...
@@ -66,18 +66,9 @@ type listenInfo struct {
}
// NewDHT creates a new DHT object with the given peer as the 'local' host
func
NewDHT
(
p
*
peer
.
Peer
)
(
*
IpfsDHT
,
error
)
{
if
p
==
nil
{
return
nil
,
errors
.
New
(
"nil peer passed to NewDHT()"
)
}
network
:=
swarm
.
NewSwarm
(
p
)
err
:=
network
.
Listen
()
if
err
!=
nil
{
return
nil
,
err
}
func
NewDHT
(
p
*
peer
.
Peer
,
net
swarm
.
Network
)
*
IpfsDHT
{
dht
:=
new
(
IpfsDHT
)
dht
.
network
=
net
work
dht
.
network
=
net
dht
.
datastore
=
ds
.
NewMapDatastore
()
dht
.
self
=
p
dht
.
listeners
=
make
(
map
[
uint64
]
*
listenInfo
)
...
...
@@ -86,7 +77,7 @@ func NewDHT(p *peer.Peer) (*IpfsDHT, error) {
dht
.
routes
=
make
([]
*
kb
.
RoutingTable
,
1
)
dht
.
routes
[
0
]
=
kb
.
NewRoutingTable
(
20
,
kb
.
ConvertPeerID
(
p
.
ID
))
dht
.
birth
=
time
.
Now
()
return
dht
,
nil
return
dht
}
// Start up background goroutines needed by the DHT
...
...
dht_test.go
View file @
10ef87b6
...
...
@@ -4,6 +4,7 @@ import (
"testing"
peer
"github.com/jbenet/go-ipfs/peer"
swarm
"github.com/jbenet/go-ipfs/swarm"
u
"github.com/jbenet/go-ipfs/util"
ma
"github.com/jbenet/go-multiaddr"
...
...
@@ -31,10 +32,12 @@ func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT)
var
dhts
[]
*
IpfsDHT
for
i
:=
0
;
i
<
4
;
i
++
{
d
,
err
:=
NewDHT
(
peers
[
i
])
net
:=
swarm
.
NewSwarm
(
peers
[
i
])
err
:=
net
.
Listen
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
d
:=
NewDHT
(
peers
[
i
],
net
)
dhts
=
append
(
dhts
,
d
)
d
.
Start
()
}
...
...
@@ -61,15 +64,19 @@ func TestPing(t *testing.T) {
peer_b
.
AddAddress
(
addr_b
)
peer_b
.
ID
=
peer
.
ID
([]
byte
(
"peer_b"
))
dht_a
,
err
:=
NewDHT
(
peer_a
)
neta
:=
swarm
.
NewSwarm
(
peer_a
)
err
=
neta
.
Listen
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dht_a
:=
NewDHT
(
peer_a
,
neta
)
dht_b
,
err
:=
NewDHT
(
peer_b
)
netb
:=
swarm
.
NewSwarm
(
peer_b
)
err
=
netb
.
Listen
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dht_b
:=
NewDHT
(
peer_b
,
netb
)
dht_a
.
Start
()
dht_b
.
Start
()
...
...
@@ -108,15 +115,19 @@ func TestValueGetSet(t *testing.T) {
peer_b
.
AddAddress
(
addr_b
)
peer_b
.
ID
=
peer
.
ID
([]
byte
(
"peer_b"
))
dht_a
,
err
:=
NewDHT
(
peer_a
)
neta
:=
swarm
.
NewSwarm
(
peer_a
)
err
=
neta
.
Listen
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dht_a
:=
NewDHT
(
peer_a
,
neta
)
dht_b
,
err
:=
NewDHT
(
peer_b
)
netb
:=
swarm
.
NewSwarm
(
peer_b
)
err
=
netb
.
Listen
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dht_b
:=
NewDHT
(
peer_b
,
netb
)
dht_a
.
Start
()
dht_b
.
Start
()
...
...
@@ -197,32 +208,7 @@ func TestProvides(t *testing.T) {
func
TestLayeredGet
(
t
*
testing
.
T
)
{
u
.
Debug
=
false
var
addrs
[]
*
ma
.
Multiaddr
for
i
:=
0
;
i
<
4
;
i
++
{
a
,
err
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
5000
+
i
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
addrs
=
append
(
addrs
,
a
)
}
var
peers
[]
*
peer
.
Peer
for
i
:=
0
;
i
<
4
;
i
++
{
p
:=
new
(
peer
.
Peer
)
p
.
AddAddress
(
addrs
[
i
])
p
.
ID
=
peer
.
ID
([]
byte
(
fmt
.
Sprintf
(
"peer_%d"
,
i
)))
peers
=
append
(
peers
,
p
)
}
var
dhts
[]
*
IpfsDHT
for
i
:=
0
;
i
<
4
;
i
++
{
d
,
err
:=
NewDHT
(
peers
[
i
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dhts
=
append
(
dhts
,
d
)
d
.
Start
()
}
addrs
,
_
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
err
:=
dhts
[
0
]
.
Connect
(
addrs
[
1
])
if
err
!=
nil
{
...
...
@@ -267,32 +253,8 @@ func TestLayeredGet(t *testing.T) {
func
TestFindPeer
(
t
*
testing
.
T
)
{
u
.
Debug
=
false
var
addrs
[]
*
ma
.
Multiaddr
for
i
:=
0
;
i
<
4
;
i
++
{
a
,
err
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
5000
+
i
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
addrs
=
append
(
addrs
,
a
)
}
var
peers
[]
*
peer
.
Peer
for
i
:=
0
;
i
<
4
;
i
++
{
p
:=
new
(
peer
.
Peer
)
p
.
AddAddress
(
addrs
[
i
])
p
.
ID
=
peer
.
ID
([]
byte
(
fmt
.
Sprintf
(
"peer_%d"
,
i
)))
peers
=
append
(
peers
,
p
)
}
var
dhts
[]
*
IpfsDHT
for
i
:=
0
;
i
<
4
;
i
++
{
d
,
err
:=
NewDHT
(
peers
[
i
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
dhts
=
append
(
dhts
,
d
)
d
.
Start
()
}
addrs
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
err
:=
dhts
[
0
]
.
Connect
(
addrs
[
1
])
if
err
!=
nil
{
...
...
ext_test.go
0 → 100644
View file @
10ef87b6
package
dht
import
(
"testing"
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"
"fmt"
"time"
)
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type
fauxNet
struct
{
Chan
*
swarm
.
Chan
swarm
.
Network
handlers
[]
mesHandleFunc
}
type
mesHandleFunc
func
(
*
swarm
.
Message
)
*
swarm
.
Message
func
newFauxNet
()
*
fauxNet
{
fn
:=
new
(
fauxNet
)
fn
.
Chan
=
swarm
.
NewChan
(
8
)
return
fn
}
func
(
f
*
fauxNet
)
Listen
()
error
{
go
func
()
{
for
{
select
{
case
in
:=
<-
f
.
Chan
.
Outgoing
:
for
_
,
h
:=
range
f
.
handlers
{
reply
:=
h
(
in
)
if
reply
!=
nil
{
f
.
Chan
.
Incoming
<-
reply
break
}
}
}
}
}()
return
nil
}
func
(
f
*
fauxNet
)
AddHandler
(
fn
func
(
*
swarm
.
Message
)
*
swarm
.
Message
)
{
f
.
handlers
=
append
(
f
.
handlers
,
fn
)
}
func
(
f
*
fauxNet
)
Send
(
mes
*
swarm
.
Message
)
{
}
func
TestGetFailure
(
t
*
testing
.
T
)
{
fn
:=
newFauxNet
()
fn
.
Listen
()
local
:=
new
(
peer
.
Peer
)
local
.
ID
=
peer
.
ID
([]
byte
(
"test_peer"
))
d
:=
NewDHT
(
local
,
fn
)
d
.
Start
()
b
,
err
:=
d
.
GetValue
(
u
.
Key
(
"test"
),
time
.
Second
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
b
)
}
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