]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Small tweak to readahead, and fix the tests
authorMatt Joiner <anacrolix@gmail.com>
Tue, 16 Jun 2015 07:14:15 +0000 (17:14 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 16 Jun 2015 07:14:15 +0000 (17:14 +1000)
client.go
client_test.go

index 5f9420fd608ba82c34f375df0504cde81fe8231a..171abd71fb525f372612bf36121efd090562eaf5 100644 (file)
--- a/client.go
+++ b/client.go
@@ -276,9 +276,13 @@ again:
 // Calculates the number of pieces to set to Readahead priority, after the
 // Now, and Next pieces.
 func readaheadPieces(readahead, pieceLength int64) (ret int) {
+       // Expand the readahead to fit any partial pieces. Subtract 1 for the
+       // "next" piece that is assigned.
        ret = int((readahead+pieceLength-1)/pieceLength - 1)
+       // Lengthen the "readahead tail" to smooth blockiness that occurs when the
+       // piece length is much larger than the readahead.
        if ret < 2 {
-               ret = 2
+               ret++
        }
        return
 }
index 8c7cc344165ff85986300333e6fc49c772faae45..cfdbd7eb5ec433f0c72b6a3b6cb8c52b21f192b4 100644 (file)
@@ -15,6 +15,7 @@ import (
        _ "github.com/anacrolix/envpprof"
        "github.com/anacrolix/utp"
        "github.com/bradfitz/iter"
+       "github.com/stretchr/testify/assert"
        "gopkg.in/check.v1"
 
        "github.com/anacrolix/torrent/bencode"
@@ -295,15 +296,14 @@ func TestReadaheadPieces(t *testing.T) {
                readaheadPieces             int
        }{
                {5 * 1024 * 1024, 256 * 1024, 19},
-               {5 * 1024 * 1024, 5 * 1024 * 1024, 0},
-               {5*1024*1024 - 1, 5 * 1024 * 1024, 0},
-               {5 * 1024 * 1024, 5*1024*1024 - 1, 1},
-               {0, 5 * 1024 * 1024, -1},
+               {5 * 1024 * 1024, 5 * 1024 * 1024, 1},
+               {5*1024*1024 - 1, 5 * 1024 * 1024, 1},
+               {5 * 1024 * 1024, 5*1024*1024 - 1, 2},
+               {0, 5 * 1024 * 1024, 0},
                {5 * 1024 * 1024, 1048576, 4},
        } {
-               if readaheadPieces(case_.readaheadBytes, case_.pieceLength) != case_.readaheadPieces {
-                       t.Fatalf("case failed: %v", case_)
-               }
+               pieces := readaheadPieces(case_.readaheadBytes, case_.pieceLength)
+               assert.Equal(t, case_.readaheadPieces, pieces, "%v", case_)
        }
 }