fix random bitswap hangs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Oct 11, 2015 at 21:22 UTC
c6f93380fcd6dddcb4014148315b5f1226e59452
2 files changed
+92
-3
exchange/bitswap/wantmanager.go
+11
-3
@@ -56,6 +56,8 @@ type msgQueue struct {
56
out bsmsg.BitSwapMessage
57
network bsnet.BitSwapNetwork
58
59
+ refcnt int
60
+
61
work chan struct{}
62
done chan struct{}
63
}
@@ -101,13 +103,13 @@ func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope) {
103
}
104
105
func (pm *WantManager) startPeerHandler(p peer.ID) *msgQueue {
104
- _, ok := pm.peers[p]
106
+ mq, ok := pm.peers[p]
107
if ok {
106
- // TODO: log an error?
108
+ mq.refcnt++
109
return nil
110
}
111
110
- mq := pm.newMsgQueue(p)
112
+ mq = pm.newMsgQueue(p)
113
114
// new peer, we will want to give them our full wantlist
115
fullwantlist := bsmsg.New(true)
@@ -129,6 +131,11 @@ func (pm *WantManager) stopPeerHandler(p peer.ID) {
131
return
132
}
133
134
+ pq.refcnt--
135
+ if pq.refcnt > 0 {
136
+ return
137
+ }
138
+
139
close(pq.done)
140
delete(pm.peers, p)
141
}
@@ -247,6 +254,7 @@ func (wm *WantManager) newMsgQueue(p peer.ID) *msgQueue {
254
mq.work = make(chan struct{}, 1)
255
mq.network = wm.network
256
mq.p = p
257
+ mq.refcnt = 1
258
259
return mq
260
}
test/sharness/t0130-multinode.sh
new
+81
@@ -0,0 +1,81 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2015 Jeromy Johnson
4
+# MIT Licensed; see the LICENSE file in this repository.
5
+#
6
+
7
+test_description="Test multiple ipfs nodes"
8
+
9
+. lib/test-lib.sh
10
+
11
+export IPTB_ROOT="`pwd`/.iptb"
12
+
13
+ipfsi() {
14
+ dir="$1"
15
+ shift
16
+ IPFS_PATH="$IPTB_ROOT/$dir" ipfs $@
17
+}
18
+
19
+check_has_connection() {
20
+ node=$1
21
+ ipfsi $node swarm peers | grep ipfs > /dev/null
22
+}
23
+
24
+startup_cluster() {
25
+ test_expect_success "start up nodes" '
26
+ iptb start
27
+ '
28
+
29
+ test_expect_success "connect nodes to eachother" '
30
+ iptb connect [1-4] 0
31
+ '
32
+
33
+ test_expect_success "nodes are connected" '
34
+ check_has_connection 0 &&
35
+ check_has_connection 1 &&
36
+ check_has_connection 2 &&
37
+ check_has_connection 3 &&
38
+ check_has_connection 4
39
+ '
40
+}
41
+
42
+check_file_fetch() {
43
+ node=$1
44
+ fhash=$2
45
+ fname=$3
46
+
47
+ test_expect_success "can fetch file" '
48
+ ipfsi $node cat $fhash > fetch_out
49
+ '
50
+
51
+ test_expect_success "file looks good" '
52
+ test_cmp $fname fetch_out
53
+ '
54
+}
55
+
56
+run_basic_test() {
57
+ startup_cluster
58
+
59
+ test_expect_success "add a file on node1" '
60
+ random 1000000 > filea &&
61
+ FILEA_HASH=$(ipfsi 1 add -q filea)
62
+ '
63
+
64
+ check_file_fetch 4 $FILEA_HASH filea
65
+ check_file_fetch 3 $FILEA_HASH filea
66
+ check_file_fetch 2 $FILEA_HASH filea
67
+ check_file_fetch 1 $FILEA_HASH filea
68
+ check_file_fetch 0 $FILEA_HASH filea
69
+
70
+ test_expect_success "shut down nodes" '
71
+ iptb stop
72
+ '
73
+}
74
+
75
+test_expect_success "set up tcp testbed" '
76
+ iptb init -n 5 -p 0 -f --bootstrap=none
77
+'
78
+
79
+run_basic_test
80
+
81
+test_done