test(bitswap) add SessionGenerator
Brian Tiger Chow committed
Sep 19, 2014 at 16:03 UTC
251f1d7f008e1f20370d80e5f8d636508bb19e90
1 file changed
+47
-18
exchange/bitswap/bitswap_test.go
+47
-18
@@ -12,17 +12,18 @@ import (
12
exchange "github.com/jbenet/go-ipfs/exchange"
13
notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
14
strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
15
- testnet "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
15
+ tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
16
peer "github.com/jbenet/go-ipfs/peer"
17
testutil "github.com/jbenet/go-ipfs/util/testutil"
18
)
19
20
func TestGetBlockTimeout(t *testing.T) {
21
22
- net := testnet.VirtualNetwork()
23
- rs := testnet.VirtualRoutingServer()
22
+ net := tn.VirtualNetwork()
23
+ rs := tn.VirtualRoutingServer()
24
+ g := NewSessionGenerator(net, rs)
25
25
- self := session(net, rs, []byte("peer id"))
26
+ self := g.Next()
27
28
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
29
block := testutil.NewBlockOrFail(t, "block")
@@ -35,13 +36,14 @@ func TestGetBlockTimeout(t *testing.T) {
36
37
func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
38
38
- net := testnet.VirtualNetwork()
39
- rs := testnet.VirtualRoutingServer()
39
+ net := tn.VirtualNetwork()
40
+ rs := tn.VirtualRoutingServer()
41
+ g := NewSessionGenerator(net, rs)
42
43
block := testutil.NewBlockOrFail(t, "block")
44
rs.Announce(&peer.Peer{}, block.Key()) // but not on network
45
44
- solo := session(net, rs, []byte("peer id"))
46
+ solo := g.Next()
47
48
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
49
_, err := solo.exchange.Block(ctx, block.Key())
@@ -55,11 +57,12 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
57
58
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
59
58
- net := testnet.VirtualNetwork()
59
- rs := testnet.VirtualRoutingServer()
60
+ net := tn.VirtualNetwork()
61
+ rs := tn.VirtualRoutingServer()
62
block := testutil.NewBlockOrFail(t, "block")
63
+ g := NewSessionGenerator(net, rs)
64
62
- hasBlock := session(net, rs, []byte("hasBlock"))
65
+ hasBlock := g.Next()
66
67
if err := hasBlock.blockstore.Put(block); err != nil {
68
t.Fatal(err)
@@ -68,7 +71,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
71
t.Fatal(err)
72
}
73
71
- wantsBlock := session(net, rs, []byte("wantsBlock"))
74
+ wantsBlock := g.Next()
75
76
ctx, _ := context.WithTimeout(context.Background(), time.Second)
77
received, err := wantsBlock.exchange.Block(ctx, block.Key())
@@ -82,13 +85,45 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
85
}
86
}
87
88
+func TestSendToWantingPeer(t *testing.T) {
89
+ t.Log("I get a file from peer |w|. In this message, I receive |w|'s wants")
90
+ t.Log("Peer |w| tells me it wants file |f|, but I don't have it")
91
+ t.Log("Later, peer |o| sends |f| to me")
92
+ t.Log("After receiving |f| from |o|, I send it to the wanting peer |w|")
93
+}
94
+
95
+func NewSessionGenerator(
96
+ net tn.Network, rs tn.RoutingServer) SessionGenerator {
97
+ return SessionGenerator{
98
+ net: net,
99
+ rs: rs,
100
+ seq: 0,
101
+ }
102
+}
103
+
104
+type SessionGenerator struct {
105
+ seq int
106
+ net tn.Network
107
+ rs tn.RoutingServer
108
+}
109
+
110
+func (g *SessionGenerator) Next() testnetBitSwap {
111
+ g.seq++
112
+ return session(g.net, g.rs, []byte(string(g.seq)))
113
+}
114
+
115
type testnetBitSwap struct {
116
peer *peer.Peer
117
exchange exchange.Interface
118
blockstore bstore.Blockstore
119
}
120
91
-func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetBitSwap {
121
+// session creates a test bitswap session.
122
+//
123
+// NB: It's easy make mistakes by providing the same peer ID to two different
124
+// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
125
+// just a much better idea.
126
+func session(net tn.Network, rs tn.RoutingServer, id peer.ID) testnetBitSwap {
127
p := &peer.Peer{ID: id}
128
129
adapter := net.Adapter(p)
@@ -109,9 +144,3 @@ func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetB
144
blockstore: blockstore,
145
}
146
}
112
-
113
-func TestSendToWantingPeer(t *testing.T) {
114
- t.Log("Peer |w| tells me it wants file, but I don't have it")
115
- t.Log("Then another peer |o| sends it to me")
116
- t.Log("After receiving the file from |o|, I send it to the wanting peer |w|")
117
-}