Commit 4ba46347 authored by Brian Tiger Chow's avatar Brian Tiger Chow

test(bitswap) send block from one instance to another

parent d2e4bad4
......@@ -119,9 +119,15 @@ func (bs *bitswap) ReceiveMessage(
if incoming.Blocks() != nil {
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.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.AppendBlock(*block)
bs.send(ctx, p, message)
defer bs.strategy.MessageSent(p, message)
return p, message, nil
}
}
}
......
package bitswap
import (
"bytes"
"testing"
"time"
......@@ -20,11 +21,13 @@ func TestGetBlockTimeout(t *testing.T) {
net := testnet.VirtualNetwork()
rs := testnet.VirtualRoutingServer()
ipfs := session(net, rs, []byte("peer id"))
self := session(net, rs, []byte("peer id"))
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
block := testutil.NewBlockOrFail(t, "block")
_, err := self.exchange.Block(ctx, block.Key())
_, err := ipfs.exchange.Block(ctx, block.Key())
if err != context.DeadlineExceeded {
t.Fatal("Expected DeadlineExceeded error")
}
......@@ -59,28 +62,35 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
hasBlock := session(net, rs, []byte("hasBlock"))
rs.Announce(hasBlock.peer, block.Key())
hasBlock.blockstore.Put(block)
hasBlock.exchange.HasBlock(context.Background(), block)
if err := hasBlock.blockstore.Put(block); err != nil {
t.Fatal(err)
}
if err := hasBlock.exchange.HasBlock(context.Background(), block); err != nil {
t.Fatal(err)
}
wantsBlock := session(net, rs, []byte("wantsBlock"))
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 {
t.Log(err)
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
exchange exchange.Interface
blockstore bstore.Blockstore
}
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
p := &peer.Peer{}
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetBitSwap {
p := &peer.Peer{ID: id}
adapter := net.Adapter(p)
htc := rs.Client(p)
......@@ -94,7 +104,7 @@ func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
sender: adapter,
}
adapter.SetDelegate(bs)
return ipfs{
return testnetBitSwap{
peer: p,
exchange: bs,
blockstore: blockstore,
......
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