mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-09 18:28:08 +08:00
add fauxNet to stand in for Swarm in tests to reproduce various network conditions
This commit is contained in:
parent
0a41abdd1d
commit
4cb2e1e07b
@ -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"
|
||||
|
||||
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"
|
||||
"code.google.com/p/goprotobuf/proto"
|
||||
|
||||
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"
|
||||
|
||||
"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 TestGetFailure(t *testing.T) {
|
||||
func (f *fauxNet) GetChan() *swarm.Chan {
|
||||
return f.Chan
|
||||
}
|
||||
|
||||
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.")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user