Skip to content

Commit

Permalink
master - fixed off-by-one in buffer state tail index.
Browse files Browse the repository at this point in the history
  • Loading branch information
masterwok committed Jul 31, 2018
1 parent 454b5fd commit 1e6351c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TorrentSessionBufferState constructor(
field = value
}

var bufferTailIndex = startIndex + bufferSize
var bufferTailIndex = startIndex + bufferSize - 1
@Synchronized
get() = field
private set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TorrentSessionBufferStateTest {
bufferHeadIndex: Int
, endIndex: Int
, bufferSize: Int
): Int = Math.min(bufferHeadIndex + bufferSize, endIndex)
): Int = Math.min(bufferHeadIndex + bufferSize - 1, endIndex)

private fun IntRange.random(): Int {
return Random().nextInt((endInclusive + 1) - start) + start
Expand Down Expand Up @@ -185,8 +185,14 @@ class TorrentSessionBufferStateTest {

underTest.setPieceDownloaded(0)

val expectedTail = getExpectedTailIndex(
underTest.bufferHeadIndex
, underTest.endIndex
, bufferSize
)

assertEquals(underTest.bufferHeadIndex, 30)
assertEquals(underTest.bufferTailIndex, Math.min(underTest.bufferHeadIndex + bufferSize, endIndex))
assertEquals(underTest.bufferTailIndex, Math.min(expectedTail, endIndex))
}

@Test
Expand Down

0 comments on commit 1e6351c

Please sign in to comment.