Commit 10ef87b6 authored by Jeromy's avatar Jeromy

starting a new testing framework

parent e6ab2e20
......@@ -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 = network
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
......
......@@ -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 {
......
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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment