@cryptotaxi247 / kubo / commits / ddf889d87

test(bitswap) send block from one instance to another

Brian Tiger Chow committed Sep 19, 2014 at 15:31 UTC ddf889d8762c83d9df0dedb757f3e115b94ae640
2 files changed +30 -13
exchange/bitswap/bitswap.go
+10 -3
@@ -119,9 +119,15 @@ func (bs *bitswap) ReceiveMessage(
119
120 if incoming.Blocks() != nil {
121 for _, block := range incoming.Blocks() {
122 - bs.blockstore.Put(block) // FIXME(brian): err ignored
122 + err := bs.blockstore.Put(block) // FIXME(brian): err ignored
123 + if err != nil {
124 + return nil, nil, err
125 + }
126 bs.notifications.Publish(block)
124 - bs.HasBlock(ctx, block) // FIXME err ignored
127 + err = bs.HasBlock(ctx, block) // FIXME err ignored
128 + if err != nil {
129 + return nil, nil, err
130 + }
131 }
132 }
133
@@ -134,7 +140,8 @@ func (bs *bitswap) ReceiveMessage(
140 }
141 message := bsmsg.New()
142 message.AppendBlock(*block)
137 - bs.send(ctx, p, message)
143 + defer bs.strategy.MessageSent(p, message)
144 + return p, message, nil
145 }
146 }
147 }
exchange/bitswap/bitswap_test.go
+20 -10
@@ -1,6 +1,7 @@
1 package bitswap
2
3 import (
4 + "bytes"
5 "testing"
6 "time"
7
@@ -20,11 +21,13 @@ func TestGetBlockTimeout(t *testing.T) {
21
22 net := testnet.VirtualNetwork()
23 rs := testnet.VirtualRoutingServer()
23 - ipfs := session(net, rs, []byte("peer id"))
24 +
25 + self := session(net, rs, []byte("peer id"))
26 +
27 ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
28 block := testutil.NewBlockOrFail(t, "block")
29 + _, err := self.exchange.Block(ctx, block.Key())
30
27 - _, err := ipfs.exchange.Block(ctx, block.Key())
31 if err != context.DeadlineExceeded {
32 t.Fatal("Expected DeadlineExceeded error")
33 }
@@ -59,28 +62,35 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
62
63 hasBlock := session(net, rs, []byte("hasBlock"))
64
62 - rs.Announce(hasBlock.peer, block.Key())
63 - hasBlock.blockstore.Put(block)
64 - hasBlock.exchange.HasBlock(context.Background(), block)
65 + if err := hasBlock.blockstore.Put(block); err != nil {
66 + t.Fatal(err)
67 + }
68 + if err := hasBlock.exchange.HasBlock(context.Background(), block); err != nil {
69 + t.Fatal(err)
70 + }
71
72 wantsBlock := session(net, rs, []byte("wantsBlock"))
73
74 ctx, _ := context.WithTimeout(context.Background(), time.Second)
69 - _, err := wantsBlock.exchange.Block(ctx, block.Key())
75 + received, err := wantsBlock.exchange.Block(ctx, block.Key())
76 if err != nil {
77 t.Log(err)
78 t.Fatal("Expected to succeed")
79 }
80 +
81 + if !bytes.Equal(block.Data, received.Data) {
82 + t.Fatal("Data doesn't match")
83 + }
84 }
85
76 -type ipfs struct {
86 +type testnetBitSwap struct {
87 peer *peer.Peer
88 exchange exchange.Interface
89 blockstore bstore.Blockstore
90 }
91
82 -func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
83 - p := &peer.Peer{}
92 +func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetBitSwap {
93 + p := &peer.Peer{ID: id}
94
95 adapter := net.Adapter(p)
96 htc := rs.Client(p)
@@ -94,7 +104,7 @@ func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
104 sender: adapter,
105 }
106 adapter.SetDelegate(bs)
97 - return ipfs{
107 + return testnetBitSwap{
108 peer: p,
109 exchange: bs,
110 blockstore: blockstore,