]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Avoid race in test
authorMatt Joiner <anacrolix@gmail.com>
Sat, 27 Jan 2018 03:31:31 +0000 (14:31 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 27 Jan 2018 03:31:46 +0000 (14:31 +1100)
client_test.go
t.go

index 51f8ad4b70bf951daca7ad1f583470637ea39844..56390b3d55af7986164451061d0e13ce954fc904 100644 (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:
diff --git a/t.go b/t.go
index 548247f762fa763e95d641be5941257caceaa4be..ee78b1839277fe1b4d614d8846d58971c6ab659d 100644 (file)
--- 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 {