From 37272a391b0e8716f44f5fe07ebfb009e2e3678d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 27 Jan 2018 14:31:31 +1100 Subject: [PATCH] Avoid race in test --- client_test.go | 8 ++++++-- t.go | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index 51f8ad4b..56390b3d 100644 --- a/client_test.go +++ b/client_test.go @@ -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: diff --git a/t.go b/t.go index 548247f7..ee78b183 100644 --- a/t.go +++ b/t.go @@ -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 { -- 2.44.0