Commit 653841f2 authored by Jeromy's avatar Jeromy

add fauxNet to stand in for Swarm in tests to reproduce various network conditions

parent 10ef87b6
......@@ -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 {
......
......@@ -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.")
}
}
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