Add more comprehensive pending requests assertions

This commit is contained in:
Matt Joiner 2021-10-25 16:16:56 +11:00
parent 506996d038
commit 37373864e4
3 changed files with 37 additions and 5 deletions

19
pending-requests_test.go Normal file
View File

@ -0,0 +1,19 @@
package torrent
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
// Ensure that cmp.Diff will detect errors as required.
func TestPendingRequestsDiff(t *testing.T) {
var a, b pendingRequests
c := qt.New(t)
diff := func() string { return cmp.Diff(a.m, b.m) }
c.Check(diff(), qt.ContentEquals, "")
a.m = []int{1, 3}
b.m = []int{1, 2, 3}
c.Check(diff(), qt.Not(qt.Equals), "")
}

View File

@ -233,6 +233,7 @@ func (p *Peer) getDesiredRequestState() (desired requestState) {
}) })
}, },
) )
p.t.assertPendingRequests()
heap.Init(&requestHeap) heap.Init(&requestHeap)
for requestHeap.Len() != 0 && desired.Requests.GetCardinality() < uint64(p.nominalMaxRequests()) { for requestHeap.Len() != 0 && desired.Requests.GetCardinality() < uint64(p.nominalMaxRequests()) {
requestIndex := heap.Pop(&requestHeap).(RequestIndex) requestIndex := heap.Pop(&requestHeap).(RequestIndex)

View File

@ -30,6 +30,7 @@ import (
"github.com/anacrolix/multiless" "github.com/anacrolix/multiless"
"github.com/anacrolix/sync" "github.com/anacrolix/sync"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/pion/datachannel" "github.com/pion/datachannel"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -1385,9 +1386,7 @@ func (t *Torrent) deletePeerConn(c *PeerConn) (ret bool) {
} }
torrent.Add("deleted connections", 1) torrent.Add("deleted connections", 1)
c.deleteAllRequests() c.deleteAllRequests()
if t.numActivePeers() == 0 && t.haveInfo() { t.assertPendingRequests()
t.assertNoPendingRequests()
}
return return
} }
@ -1408,8 +1407,21 @@ func (t *Torrent) numActivePeers() (num int) {
return return
} }
func (t *Torrent) assertNoPendingRequests() { func (t *Torrent) assertPendingRequests() {
t.pendingRequests.AssertEmpty() var actual pendingRequests
if t.haveInfo() {
actual.m = make([]int, t.numRequests())
}
t.iterPeers(func(p *Peer) {
p.actualRequestState.Requests.Iterate(func(x uint32) bool {
actual.Inc(x)
return true
})
})
diff := cmp.Diff(actual.m, t.pendingRequests.m)
if diff != "" {
panic(diff)
}
} }
func (t *Torrent) dropConnection(c *PeerConn) { func (t *Torrent) dropConnection(c *PeerConn) {