mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00
feat(bs/testnet) use delay in virtual network
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
@ -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)
|
||||
|
||||
|
@ -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))
|
||||
|
Reference in New Issue
Block a user