Skip to content

Commit

Permalink
Closes issue mozilla-mobile#8202: Increase downloadJobState.currentBy…
Browse files Browse the repository at this point in the history
…tesCopied

in copyInChunks.
  • Loading branch information
Amejia481 committed Aug 24, 2020
1 parent 3d47143 commit 051ce48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ abstract class AbstractFetchDownloadService : Service() {
}
}

private fun copyInChunks(downloadJobState: DownloadJobState, inStream: InputStream, outStream: OutputStream) {
@VisibleForTesting
internal fun copyInChunks(downloadJobState: DownloadJobState, inStream: InputStream, outStream: OutputStream) {
val data = ByteArray(CHUNK_SIZE)
logger.debug("starting copyInChunks ${downloadJobState.state.url}" +
" currentBytesCopied ${downloadJobState.currentBytesCopied}")
Expand All @@ -632,9 +633,10 @@ abstract class AbstractFetchDownloadService : Service() {

// If bytesRead is -1, there's no data left to read from the stream
if (bytesRead == -1) { break }
downloadJobState.currentBytesCopied += bytesRead

val newState = downloadJobState.state.copy(
currentBytesCopied = downloadJobState.currentBytesCopied + bytesRead
currentBytesCopied = downloadJobState.currentBytesCopied
)
updateDownloadState(newState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1375,4 +1375,19 @@ class AbstractFetchDownloadServiceTest {

assertTrue(downloadJobState.canUpdateNotification())
}

@Test
fun `copyInChunks must alter download currentBytesCopied`() = runBlocking {
val downloadJobState = DownloadJobState(state = mock(), status = DOWNLOADING)
val inputStream = mock<InputStream>()

assertEquals(0, downloadJobState.currentBytesCopied)

doReturn(15, -1).`when`(inputStream).read(any())
doNothing().`when`(service).updateDownloadState(any())

service.copyInChunks(downloadJobState, inputStream, mock())

assertEquals(15, downloadJobState.currentBytesCopied)
}
}

0 comments on commit 051ce48

Please sign in to comment.