Avoid race in test

This commit is contained in:
Matt Joiner 2018-01-27 14:31:31 +11:00
parent 1f3eace72f
commit 37272a391b
2 changed files with 14 additions and 2 deletions

View File

@ -873,10 +873,14 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
assert.True(t, new)
psc := leecherGreeting.SubscribePieceStateChanges()
defer psc.Close()
leecherGreeting.DownloadAll()
leecherGreeting.cl.mu.Lock()
leecherGreeting.downloadPiecesLocked(0, leecherGreeting.numPieces())
if ps.Cancel {
leecherGreeting.CancelPieces(0, leecherGreeting.NumPieces())
leecherGreeting.cancelPiecesLocked(0, leecherGreeting.NumPieces())
}
leecherGreeting.cl.mu.Unlock()
addClientPeer(leecherGreeting, seeder)
completes := make(map[int]bool, 3)
values:

8
t.go
View File

@ -152,6 +152,10 @@ func (t *Torrent) deleteReader(r *reader) {
func (t *Torrent) DownloadPieces(begin, end int) {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
t.downloadPiecesLocked(begin, end)
}
func (t *Torrent) downloadPiecesLocked(begin, end int) {
for i := begin; i < end; i++ {
if t.pieces[i].priority.Raise(PiecePriorityNormal) {
t.updatePiecePriority(i)
@ -162,6 +166,10 @@ func (t *Torrent) DownloadPieces(begin, end int) {
func (t *Torrent) CancelPieces(begin, end int) {
t.cl.mu.Lock()
defer t.cl.mu.Unlock()
t.cancelPiecesLocked(begin, end)
}
func (t *Torrent) cancelPiecesLocked(begin, end int) {
for i := begin; i < end; i++ {
p := &t.pieces[i]
if p.priority == PiecePriorityNone {