Browse Source

fix leaking statesync test (#6680)

pull/6682/head
Callum Waters 3 years ago
committed by GitHub
parent
commit
2c14d491f6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions
  1. +9
    -4
      internal/statesync/dispatcher.go
  2. +4
    -4
      internal/statesync/dispatcher_test.go

+ 9
- 4
internal/statesync/dispatcher.go View File

@ -57,7 +57,7 @@ func (d *dispatcher) LightBlock(ctx context.Context, height int64) (*types.Light
// fetch the next peer id in the list and request a light block from that // fetch the next peer id in the list and request a light block from that
// peer // peer
peer := d.availablePeers.Pop()
peer := d.availablePeers.Pop(ctx)
lb, err := d.lightBlock(ctx, height, peer) lb, err := d.lightBlock(ctx, height, peer)
return lb, peer, err return lb, peer, err
} }
@ -272,7 +272,7 @@ func (l *peerlist) Len() int {
return len(l.peers) return len(l.peers)
} }
func (l *peerlist) Pop() types.NodeID {
func (l *peerlist) Pop(ctx context.Context) types.NodeID {
l.mtx.Lock() l.mtx.Lock()
if len(l.peers) == 0 { if len(l.peers) == 0 {
// if we don't have any peers in the list we block until a peer is // if we don't have any peers in the list we block until a peer is
@ -281,8 +281,13 @@ func (l *peerlist) Pop() types.NodeID {
l.waiting = append(l.waiting, wait) l.waiting = append(l.waiting, wait)
// unlock whilst waiting so that the list can be appended to // unlock whilst waiting so that the list can be appended to
l.mtx.Unlock() l.mtx.Unlock()
peer := <-wait
return peer
select {
case peer := <-wait:
return peer
case <-ctx.Done():
return ""
}
} }
peer := l.peers[0] peer := l.peers[0]


+ 4
- 4
internal/statesync/dispatcher_test.go View File

@ -95,7 +95,7 @@ func TestPeerListBasic(t *testing.T) {
half := numPeers / 2 half := numPeers / 2
for i := 0; i < half; i++ { for i := 0; i < half; i++ {
assert.Equal(t, peerSet[i], peerList.Pop())
assert.Equal(t, peerSet[i], peerList.Pop(ctx))
} }
assert.Equal(t, half, peerList.Len()) assert.Equal(t, half, peerList.Len())
@ -104,7 +104,7 @@ func TestPeerListBasic(t *testing.T) {
peerList.Remove(peerSet[half]) peerList.Remove(peerSet[half])
half++ half++
assert.Equal(t, peerSet[half], peerList.Pop())
assert.Equal(t, peerSet[half], peerList.Pop(ctx))
} }
@ -117,7 +117,7 @@ func TestPeerListConcurrent(t *testing.T) {
// peer list hasn't been populated each these go routines should block // peer list hasn't been populated each these go routines should block
for i := 0; i < numPeers/2; i++ { for i := 0; i < numPeers/2; i++ {
go func() { go func() {
_ = peerList.Pop()
_ = peerList.Pop(ctx)
wg.Done() wg.Done()
}() }()
} }
@ -132,7 +132,7 @@ func TestPeerListConcurrent(t *testing.T) {
// we request the second half of the peer set // we request the second half of the peer set
for i := 0; i < numPeers/2; i++ { for i := 0; i < numPeers/2; i++ {
go func() { go func() {
_ = peerList.Pop()
_ = peerList.Pop(ctx)
wg.Done() wg.Done()
}() }()
} }


Loading…
Cancel
Save