test(bitswap) test sending wantlist to peers
Brian Tiger Chow committed
Sep 21, 2014 at 17:04 UTC
b806270e5d635bbb55505d807cf7356fc560bfa4
1 file changed
+51
-3
exchange/bitswap/bitswap_test.go
+51
-3
@@ -146,10 +146,58 @@ func getOrFail(bitswap instance, b *blocks.Block, t *testing.T, wg *sync.WaitGro
146
}
147
148
func TestSendToWantingPeer(t *testing.T) {
149
- t.Log("I get a file from peer |w|. In this message, I receive |w|'s wants")
150
- t.Log("Peer |w| tells me it wants file |f|, but I don't have it")
151
- t.Log("Later, peer |o| sends |f| to me")
149
+ net := tn.VirtualNetwork()
150
+ rs := tn.VirtualRoutingServer()
151
+ sg := NewSessionGenerator(net, rs)
152
+ bg := NewBlockGenerator(t)
153
+
154
+ me := sg.Next()
155
+ w := sg.Next()
156
+ o := sg.Next()
157
+
158
+ alpha := bg.Next()
159
+
160
+ const timeout = 100 * time.Millisecond
161
+ const wait = 100 * time.Millisecond
162
+
163
+ t.Log("Peer |w| attempts to get a file |alpha|. NB: alpha not available")
164
+ ctx, _ := context.WithTimeout(context.Background(), timeout)
165
+ _, err := w.exchange.Block(ctx, alpha.Key())
166
+ if err == nil {
167
+ t.Error("Expected alpha to NOT be available")
168
+ }
169
+ time.Sleep(wait)
170
+
171
+ t.Log("Peer |w| announces availability of a file |beta|")
172
+ beta := bg.Next()
173
+ ctx, _ = context.WithTimeout(context.Background(), timeout)
174
+ w.exchange.HasBlock(ctx, beta)
175
+ time.Sleep(wait)
176
+
177
+ t.Log("I request and get |beta| from |w|. In the message, I receive |w|'s wants [alpha]")
178
+ t.Log("I don't have alpha, but I keep it on my wantlist.")
179
+ ctx, _ = context.WithTimeout(context.Background(), timeout)
180
+ me.exchange.Block(ctx, beta.Key())
181
+ time.Sleep(wait)
182
+
183
+ t.Log("Peer |o| announces the availability of |alpha|")
184
+ ctx, _ = context.WithTimeout(context.Background(), timeout)
185
+ o.exchange.HasBlock(ctx, alpha)
186
+ time.Sleep(wait)
187
+
188
+ t.Log("I request |alpha| for myself.")
189
+ ctx, _ = context.WithTimeout(context.Background(), timeout)
190
+ me.exchange.Block(ctx, alpha.Key())
191
+ time.Sleep(wait)
192
+
193
t.Log("After receiving |f| from |o|, I send it to the wanting peer |w|")
194
+ block, err := w.blockstore.Get(alpha.Key())
195
+ if err != nil {
196
+ t.Fatal("Should not have received an error")
197
+ }
198
+ if block.Key() != alpha.Key() {
199
+ t.Error("Expected to receive alpha from me")
200
+ }
201
}
202
203
func NewBlockGenerator(t *testing.T) BlockGenerator {