diff --git a/blockservice/mock.go b/blockservice/mock.go index 0305c92fb..2b646386c 100644 --- a/blockservice/mock.go +++ b/blockservice/mock.go @@ -5,12 +5,13 @@ import ( bitswap "github.com/jbenet/go-ipfs/exchange/bitswap" tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet" - "github.com/jbenet/go-ipfs/routing/mock" + mock "github.com/jbenet/go-ipfs/routing/mock" + delay "github.com/jbenet/go-ipfs/util/delay" ) // Mocks returns |n| connected mock Blockservices func Mocks(t *testing.T, n int) []*BlockService { - net := tn.VirtualNetwork() + net := tn.VirtualNetwork(delay.Fixed(0)) rs := mock.VirtualRoutingServer() sg := bitswap.NewSessionGenerator(net, rs) diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index d57132fba..21b259a7e 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -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() diff --git a/exchange/bitswap/testnet/network.go b/exchange/bitswap/testnet/network.go index 7f82bcdce..b8f61b413 100644 --- a/exchange/bitswap/testnet/network.go +++ b/exchange/bitswap/testnet/network.go @@ -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 } diff --git a/exchange/bitswap/testnet/network_test.go b/exchange/bitswap/testnet/network_test.go index 0bfb0cb1e..7a9f48e2d 100644 --- a/exchange/bitswap/testnet/network_test.go +++ b/exchange/bitswap/testnet/network_test.go @@ -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))