Commit 7d7fd57a authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(bs/testnet) use delay in virtual network

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent c211b061
......@@ -11,13 +11,14 @@ import (
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
mock "github.com/jbenet/go-ipfs/routing/mock"
delay "github.com/jbenet/go-ipfs/util/delay"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestClose(t *testing.T) {
// TODO
t.Skip("TODO Bitswap's Close implementation is a WIP")
vnet := tn.VirtualNetwork()
vnet := tn.VirtualNetwork(delay.Fixed(0))
rout := mock.VirtualRoutingServer()
sesgen := NewSessionGenerator(vnet, rout)
bgen := blocksutil.NewBlockGenerator()
......@@ -31,7 +32,7 @@ func TestClose(t *testing.T) {
func TestGetBlockTimeout(t *testing.T) {
net := tn.VirtualNetwork()
net := tn.VirtualNetwork(delay.Fixed(0))
rs := mock.VirtualRoutingServer()
g := NewSessionGenerator(net, rs)
......@@ -48,7 +49,7 @@ func TestGetBlockTimeout(t *testing.T) {
func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
net := tn.VirtualNetwork()
net := tn.VirtualNetwork(delay.Fixed(0))
rs := mock.VirtualRoutingServer()
g := NewSessionGenerator(net, rs)
......@@ -69,7 +70,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
net := tn.VirtualNetwork()
net := tn.VirtualNetwork(delay.Fixed(0))
rs := mock.VirtualRoutingServer()
block := blocks.NewBlock([]byte("block"))
g := NewSessionGenerator(net, rs)
......@@ -121,7 +122,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
if testing.Short() {
t.SkipNow()
}
net := tn.VirtualNetwork()
net := tn.VirtualNetwork(delay.Fixed(0))
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := blocksutil.NewBlockGenerator()
......@@ -181,7 +182,7 @@ func TestSendToWantingPeer(t *testing.T) {
t.SkipNow()
}
net := tn.VirtualNetwork()
net := tn.VirtualNetwork(delay.Fixed(0))
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := blocksutil.NewBlockGenerator()
......
......@@ -10,6 +10,7 @@ import (
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/util"
delay "github.com/jbenet/go-ipfs/util/delay"
)
type Network interface {
......@@ -33,14 +34,16 @@ type Network interface {
// network impl
func VirtualNetwork() Network {
func VirtualNetwork(d delay.D) Network {
return &network{
clients: make(map[util.Key]bsnet.Receiver),
delay: d,
}
}
type network struct {
clients map[util.Key]bsnet.Receiver
delay delay.D
}
func (n *network) Adapter(p peer.Peer) bsnet.BitSwapNetwork {
......@@ -84,13 +87,15 @@ func (n *network) deliver(
return errors.New("Invalid input")
}
n.delay.Wait()
nextPeer, nextMsg := r.ReceiveMessage(context.TODO(), from, message)
if (nextPeer == nil && nextMsg != nil) || (nextMsg == nil && nextPeer != nil) {
return errors.New("Malformed client request")
}
if nextPeer == nil && nextMsg == nil {
if nextPeer == nil && nextMsg == nil { // no response to send
return nil
}
......
......@@ -9,11 +9,12 @@ import (
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer"
delay "github.com/jbenet/go-ipfs/util/delay"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestSendRequestToCooperativePeer(t *testing.T) {
net := VirtualNetwork()
net := VirtualNetwork(delay.Fixed(0))
idOfRecipient := []byte("recipient")
......@@ -60,7 +61,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
}
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
net := VirtualNetwork()
net := VirtualNetwork(delay.Fixed(0))
idOfResponder := []byte("responder")
waiter := net.Adapter(testutil.NewPeerWithIDString("waiter"))
responder := net.Adapter(testutil.NewPeerWithID(idOfResponder))
......
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