From: Matt Joiner Date: Sat, 27 Jan 2018 03:31:31 +0000 (+1100) Subject: Avoid race in test X-Git-Tag: v1.0.0~256 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=37272a391b0e8716f44f5fe07ebfb009e2e3678d;p=btrtrc.git Avoid race in test --- 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 {