Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81d4c89

Browse files
committedMay 13, 2015
update comments and reintroduce test
1 parent 0e20492 commit 81d4c89

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed
 

‎exchange/bitswap/bitswap_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
blocks "github.com/ipfs/go-ipfs/blocks"
1313
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
1414
tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
15+
p2ptestutil "github.com/ipfs/go-ipfs/p2p/test/util"
1516
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
1617
delay "github.com/ipfs/go-ipfs/thirdparty/delay"
1718
u "github.com/ipfs/go-ipfs/util"
@@ -34,6 +35,28 @@ func TestClose(t *testing.T) {
3435
bitswap.Exchange.GetBlock(context.Background(), block.Key())
3536
}
3637

38+
func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
39+
40+
rs := mockrouting.NewServer()
41+
net := tn.VirtualNetwork(rs, delay.Fixed(kNetworkDelay))
42+
g := NewTestSessionGenerator(net)
43+
defer g.Close()
44+
45+
block := blocks.NewBlock([]byte("block"))
46+
pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t)
47+
rs.Client(pinfo).Provide(context.Background(), block.Key()) // but not on network
48+
49+
solo := g.Next()
50+
defer solo.Exchange.Close()
51+
52+
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
53+
_, err := solo.Exchange.GetBlock(ctx, block.Key())
54+
55+
if err != context.DeadlineExceeded {
56+
t.Fatal("Expected DeadlineExceeded error")
57+
}
58+
}
59+
3760
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
3861

3962
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))

‎exchange/bitswap/peermanager.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type cancellation struct {
4646
type msgQueue struct {
4747
p peer.ID
4848

49-
lk sync.Mutex
50-
wlmsg bsmsg.BitSwapMessage
49+
outlk sync.Mutex
50+
out bsmsg.BitSwapMessage
5151

5252
work chan struct{}
5353
done chan struct{}
@@ -106,11 +106,11 @@ func (pm *PeerManager) runQueue(mq *msgQueue) {
106106
// TODO: cant connect, what now?
107107
}
108108

109-
// grab messages from queue
110-
mq.lk.Lock()
111-
wlm := mq.wlmsg
112-
mq.wlmsg = nil
113-
mq.lk.Unlock()
109+
// grab outgoin message
110+
mq.outlk.Lock()
111+
wlm := mq.out
112+
mq.out = nil
113+
mq.outlk.Unlock()
114114

115115
if wlm != nil && !wlm.Empty() {
116116
// send wantlist updates
@@ -178,26 +178,30 @@ func (pm *PeerManager) Run(ctx context.Context) {
178178
}
179179

180180
func (mq *msgQueue) addMessage(msg bsmsg.BitSwapMessage) {
181-
mq.lk.Lock()
181+
mq.outlk.Lock()
182182
defer func() {
183-
mq.lk.Unlock()
183+
mq.outlk.Unlock()
184184
select {
185185
case mq.work <- struct{}{}:
186186
default:
187187
}
188188
}()
189189

190-
if mq.wlmsg == nil || msg.Full() {
191-
mq.wlmsg = msg
190+
// if we have no message held, or the one we are given is full
191+
// overwrite the one we are holding
192+
if mq.out == nil || msg.Full() {
193+
mq.out = msg
192194
return
193195
}
194196

195197
// TODO: add a msg.Combine(...) method
198+
// otherwise, combine the one we are holding with the
199+
// one passed in
196200
for _, e := range msg.Wantlist() {
197201
if e.Cancel {
198-
mq.wlmsg.Cancel(e.Key)
202+
mq.out.Cancel(e.Key)
199203
} else {
200-
mq.wlmsg.AddEntry(e.Key, e.Priority)
204+
mq.out.AddEntry(e.Key, e.Priority)
201205
}
202206
}
203207
}

0 commit comments

Comments
 (0)
Please sign in to comment.