Skip to content

Commit b124709

Browse files
committed
fix(android): avoid double-counting upload progress
1 parent 2660348 commit b124709

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal class ProgressRequestBody(
5959

6060
@Throws(IOException::class)
6161
override fun write(data: ByteArray, offset: Int, byteCount: Int) {
62-
super.write(data, offset, byteCount)
62+
out.write(data, offset, byteCount)
6363
count += byteCount.toLong()
6464
sendProgressUpdate()
6565
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Conflicting okhttp versions
9+
@file:Suppress("DEPRECATION_ERROR")
10+
11+
package com.facebook.react.modules.network
12+
13+
import okhttp3.MediaType
14+
import okhttp3.RequestBody
15+
import okio.Buffer
16+
import org.assertj.core.api.Assertions.assertThat
17+
import org.junit.Test
18+
19+
class ProgressRequestBodyTest {
20+
21+
@Test
22+
fun testBulkWritesDoNotDoubleCountProgress() {
23+
val content = ByteArray(8 * 1024) { it.toByte() }
24+
val progressUpdates = mutableListOf<ProgressUpdate>()
25+
val requestBody =
26+
ProgressRequestBody(
27+
RequestBody.create(checkNotNull(MediaType.parse("application/octet-stream")), content),
28+
ProgressListener { bytesWritten, contentLength, done ->
29+
progressUpdates.add(ProgressUpdate(bytesWritten, contentLength, done))
30+
},
31+
)
32+
val output = Buffer()
33+
34+
requestBody.writeTo(output)
35+
36+
assertThat(output.readByteArray()).isEqualTo(content)
37+
assertThat(progressUpdates).isNotEmpty()
38+
assertThat(progressUpdates.maxOf { it.bytesWritten }).isEqualTo(content.size.toLong())
39+
assertThat(progressUpdates.last())
40+
.isEqualTo(ProgressUpdate(content.size.toLong(), content.size.toLong(), true))
41+
}
42+
43+
private data class ProgressUpdate(
44+
val bytesWritten: Long,
45+
val contentLength: Long,
46+
val done: Boolean,
47+
)
48+
}

0 commit comments

Comments
 (0)