mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-03 04:37:30 +08:00
test(bitswap) send block from one instance to another
This commit is contained in:
@ -119,9 +119,15 @@ func (bs *bitswap) ReceiveMessage(
|
|||||||
|
|
||||||
if incoming.Blocks() != nil {
|
if incoming.Blocks() != nil {
|
||||||
for _, block := range incoming.Blocks() {
|
for _, block := range incoming.Blocks() {
|
||||||
bs.blockstore.Put(block) // FIXME(brian): err ignored
|
err := bs.blockstore.Put(block) // FIXME(brian): err ignored
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
bs.notifications.Publish(block)
|
bs.notifications.Publish(block)
|
||||||
bs.HasBlock(ctx, block) // FIXME err ignored
|
err = bs.HasBlock(ctx, block) // FIXME err ignored
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +140,8 @@ func (bs *bitswap) ReceiveMessage(
|
|||||||
}
|
}
|
||||||
message := bsmsg.New()
|
message := bsmsg.New()
|
||||||
message.AppendBlock(*block)
|
message.AppendBlock(*block)
|
||||||
bs.send(ctx, p, message)
|
defer bs.strategy.MessageSent(p, message)
|
||||||
|
return p, message, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package bitswap
|
package bitswap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -20,11 +21,13 @@ func TestGetBlockTimeout(t *testing.T) {
|
|||||||
|
|
||||||
net := testnet.VirtualNetwork()
|
net := testnet.VirtualNetwork()
|
||||||
rs := testnet.VirtualRoutingServer()
|
rs := testnet.VirtualRoutingServer()
|
||||||
ipfs := session(net, rs, []byte("peer id"))
|
|
||||||
|
self := session(net, rs, []byte("peer id"))
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
|
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||||
block := testutil.NewBlockOrFail(t, "block")
|
block := testutil.NewBlockOrFail(t, "block")
|
||||||
|
_, err := self.exchange.Block(ctx, block.Key())
|
||||||
|
|
||||||
_, err := ipfs.exchange.Block(ctx, block.Key())
|
|
||||||
if err != context.DeadlineExceeded {
|
if err != context.DeadlineExceeded {
|
||||||
t.Fatal("Expected DeadlineExceeded error")
|
t.Fatal("Expected DeadlineExceeded error")
|
||||||
}
|
}
|
||||||
@ -59,28 +62,35 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
|
|||||||
|
|
||||||
hasBlock := session(net, rs, []byte("hasBlock"))
|
hasBlock := session(net, rs, []byte("hasBlock"))
|
||||||
|
|
||||||
rs.Announce(hasBlock.peer, block.Key())
|
if err := hasBlock.blockstore.Put(block); err != nil {
|
||||||
hasBlock.blockstore.Put(block)
|
t.Fatal(err)
|
||||||
hasBlock.exchange.HasBlock(context.Background(), block)
|
}
|
||||||
|
if err := hasBlock.exchange.HasBlock(context.Background(), block); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
wantsBlock := session(net, rs, []byte("wantsBlock"))
|
wantsBlock := session(net, rs, []byte("wantsBlock"))
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Second)
|
ctx, _ := context.WithTimeout(context.Background(), time.Second)
|
||||||
_, err := wantsBlock.exchange.Block(ctx, block.Key())
|
received, err := wantsBlock.exchange.Block(ctx, block.Key())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
t.Fatal("Expected to succeed")
|
t.Fatal("Expected to succeed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(block.Data, received.Data) {
|
||||||
|
t.Fatal("Data doesn't match")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ipfs struct {
|
type testnetBitSwap struct {
|
||||||
peer *peer.Peer
|
peer *peer.Peer
|
||||||
exchange exchange.Interface
|
exchange exchange.Interface
|
||||||
blockstore bstore.Blockstore
|
blockstore bstore.Blockstore
|
||||||
}
|
}
|
||||||
|
|
||||||
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
|
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetBitSwap {
|
||||||
p := &peer.Peer{}
|
p := &peer.Peer{ID: id}
|
||||||
|
|
||||||
adapter := net.Adapter(p)
|
adapter := net.Adapter(p)
|
||||||
htc := rs.Client(p)
|
htc := rs.Client(p)
|
||||||
@ -94,7 +104,7 @@ func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
|
|||||||
sender: adapter,
|
sender: adapter,
|
||||||
}
|
}
|
||||||
adapter.SetDelegate(bs)
|
adapter.SetDelegate(bs)
|
||||||
return ipfs{
|
return testnetBitSwap{
|
||||||
peer: p,
|
peer: p,
|
||||||
exchange: bs,
|
exchange: bs,
|
||||||
blockstore: blockstore,
|
blockstore: blockstore,
|
||||||
|
Reference in New Issue
Block a user