wait for peers in wantmanager to all appear
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Feb 8, 2016 at 15:59 UTC
c73da8486a88c28312e9e4148e9a92c6b44dd34b
2 files changed
+29
-2
exchange/bitswap/bitswap_test.go
+13
@@ -158,6 +158,19 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
158
159
t.Log("Give the blocks to the first instance")
160
161
+ nump := len(instances) - 1
162
+ // assert we're properly connected
163
+ for _, inst := range instances {
164
+ peers := inst.Exchange.wm.ConnectedPeers()
165
+ for i := 0; i < 10 && len(peers) != nump; i++ {
166
+ time.Sleep(time.Millisecond * 50)
167
+ peers = inst.Exchange.wm.ConnectedPeers()
168
+ }
169
+ if len(peers) != nump {
170
+ t.Fatal("not enough peers connected to instance")
171
+ }
172
+ }
173
+
174
var blkeys []key.Key
175
first := instances[0]
176
for _, b := range blocks {
exchange/bitswap/wantmanager.go
+16
-2
@@ -16,8 +16,9 @@ import (
16
type WantManager struct {
17
// sync channels for Run loop
18
incoming chan []*bsmsg.Entry
19
- connect chan peer.ID // notification channel for new peers connecting
20
- disconnect chan peer.ID // notification channel for peers disconnecting
19
+ connect chan peer.ID // notification channel for new peers connecting
20
+ disconnect chan peer.ID // notification channel for peers disconnecting
21
+ peerReqs chan chan []peer.ID // channel to request connected peers on
22
23
// synchronized by Run loop, only touch inside there
24
peers map[peer.ID]*msgQueue
@@ -32,6 +33,7 @@ func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantMana
33
incoming: make(chan []*bsmsg.Entry, 10),
34
connect: make(chan peer.ID, 10),
35
disconnect: make(chan peer.ID, 10),
36
+ peerReqs: make(chan chan []peer.ID),
37
peers: make(map[peer.ID]*msgQueue),
38
wl: wantlist.NewThreadSafe(),
39
network: network,
@@ -88,6 +90,12 @@ func (pm *WantManager) addEntries(ks []key.Key, cancel bool) {
90
}
91
}
92
93
+func (pm *WantManager) ConnectedPeers() []peer.ID {
94
+ resp := make(chan []peer.ID)
95
+ pm.peerReqs <- resp
96
+ return <-resp
97
+}
98
+
99
func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope) {
100
// Blocks need to be sent synchronously to maintain proper backpressure
101
// throughout the network stack
@@ -242,6 +250,12 @@ func (pm *WantManager) Run() {
250
pm.startPeerHandler(p)
251
case p := <-pm.disconnect:
252
pm.stopPeerHandler(p)
253
+ case req := <-pm.peerReqs:
254
+ var peers []peer.ID
255
+ for p := range pm.peers {
256
+ peers = append(peers, p)
257
+ }
258
+ req <- peers
259
case <-pm.ctx.Done():
260
return
261
}