Skip to content

Commit 2f02dd4

Browse files
hramosfacebook-github-bot
authored andcommitted
Revert PR #17312 due to buck failures in continuous integration tests
Summary: Circle is currently failing on the `android` step due to a dependency issue introduced by the aforementioned PR. I am currently waiting for an internal diff to be reviewed which will restore this PR alongside the necessary dependency. Closes #17902 Differential Revision: D6937173 Pulled By: hramos fbshipit-source-id: f732a397521cc5df36f503e618318ef6d69aeaa6
1 parent 47fe523 commit 2f02dd4

File tree

5 files changed

+67
-94
lines changed

5 files changed

+67
-94
lines changed

.circleci/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ jobs:
451451

452452
# Keep configuring Android dependencies while AVD boots up
453453

454+
# Install Android NDK
455+
- run: *create-ndk-directory
456+
- restore-cache: *restore-cache-ndk
457+
- run: *install-ndk
458+
- save-cache: *save-cache-ndk
459+
454460
# Fetch dependencies using BUCK
455461
- restore-cache: *restore-cache-buck
456462
- run: *install-buck
@@ -463,12 +469,6 @@ jobs:
463469
- run: buck fetch ReactAndroid/src/androidTest/...
464470
- run: ./gradlew :ReactAndroid:downloadBoost :ReactAndroid:downloadDoubleConversion :ReactAndroid:downloadFolly :ReactAndroid:downloadGlog :ReactAndroid:downloadJSCHeaders
465471

466-
# Install Android NDK
467-
- run: *create-ndk-directory
468-
- restore-cache: *restore-cache-ndk
469-
- run: *install-ndk
470-
- save-cache: *save-cache-ndk
471-
472472
# Build and compile
473473
- run: *build-android-app
474474
- run: *compile-native-libs

ReactAndroid/src/main/java/com/facebook/react/modules/network/BUCK

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ rn_android_library(
1111
],
1212
deps = [
1313
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
14-
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/internal:internal"),
1514
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
1615
react_native_dep("third-party/java/jsr-305:jsr-305"),
1716
react_native_dep("third-party/java/okhttp:okhttp3"),

ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
343343
}
344344
}
345345

346-
RequestBody requestBody;
347346
if (data == null) {
348-
requestBody = RequestBodyUtil.getEmptyBody(method);
347+
requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method));
349348
} else if (handler != null) {
350-
requestBody = handler.toRequestBody(data, contentType);
349+
RequestBody requestBody = handler.toRequestBody(data, contentType);
350+
requestBuilder.method(method, requestBody);
351351
} else if (data.hasKey(REQUEST_BODY_KEY_STRING)) {
352352
if (contentType == null) {
353353
ResponseUtil.onRequestError(
@@ -360,13 +360,14 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
360360
String body = data.getString(REQUEST_BODY_KEY_STRING);
361361
MediaType contentMediaType = MediaType.parse(contentType);
362362
if (RequestBodyUtil.isGzipEncoding(contentEncoding)) {
363-
requestBody = RequestBodyUtil.createGzip(contentMediaType, body);
363+
RequestBody requestBody = RequestBodyUtil.createGzip(contentMediaType, body);
364364
if (requestBody == null) {
365365
ResponseUtil.onRequestError(eventEmitter, requestId, "Failed to gzip request body", null);
366366
return;
367367
}
368+
requestBuilder.method(method, requestBody);
368369
} else {
369-
requestBody = RequestBody.create(contentMediaType, body);
370+
requestBuilder.method(method, RequestBody.create(contentMediaType, body));
370371
}
371372
} else if (data.hasKey(REQUEST_BODY_KEY_BASE64)) {
372373
if (contentType == null) {
@@ -379,7 +380,9 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
379380
}
380381
String base64String = data.getString(REQUEST_BODY_KEY_BASE64);
381382
MediaType contentMediaType = MediaType.parse(contentType);
382-
requestBody = RequestBody.create(contentMediaType, ByteString.decodeBase64(base64String));
383+
requestBuilder.method(
384+
method,
385+
RequestBody.create(contentMediaType, ByteString.decodeBase64(base64String)));
383386
} else if (data.hasKey(REQUEST_BODY_KEY_URI)) {
384387
if (contentType == null) {
385388
ResponseUtil.onRequestError(
@@ -400,7 +403,9 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
400403
null);
401404
return;
402405
}
403-
requestBody = RequestBodyUtil.create(MediaType.parse(contentType), fileInputStream);
406+
requestBuilder.method(
407+
method,
408+
RequestBodyUtil.create(MediaType.parse(contentType), fileInputStream));
404409
} else if (data.hasKey(REQUEST_BODY_KEY_FORMDATA)) {
405410
if (contentType == null) {
406411
contentType = "multipart/form-data";
@@ -411,16 +416,28 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) {
411416
if (multipartBuilder == null) {
412417
return;
413418
}
414-
requestBody = multipartBuilder.build();
419+
420+
requestBuilder.method(
421+
method,
422+
RequestBodyUtil.createProgressRequest(
423+
multipartBuilder.build(),
424+
new ProgressListener() {
425+
long last = System.nanoTime();
426+
427+
@Override
428+
public void onProgress(long bytesWritten, long contentLength, boolean done) {
429+
long now = System.nanoTime();
430+
if (done || shouldDispatch(now, last)) {
431+
ResponseUtil.onDataSend(eventEmitter, requestId, bytesWritten, contentLength);
432+
last = now;
433+
}
434+
}
435+
}));
415436
} else {
416437
// Nothing in data payload, at least nothing we could understand anyway.
417-
requestBody = RequestBodyUtil.getEmptyBody(method);
438+
requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method));
418439
}
419440

420-
requestBuilder.method(
421-
method,
422-
wrapRequestBodyWithProgressEmitter(requestBody, eventEmitter, requestId));
423-
424441
addRequest(requestId);
425442
client.newCall(requestBuilder.build()).enqueue(
426443
new Callback() {
@@ -498,29 +515,6 @@ public void onResponse(Call call, Response response) throws IOException {
498515
});
499516
}
500517

501-
private RequestBody wrapRequestBodyWithProgressEmitter(
502-
final RequestBody requestBody,
503-
final RCTDeviceEventEmitter eventEmitter,
504-
final int requestId) {
505-
if(requestBody == null) {
506-
return null;
507-
}
508-
return RequestBodyUtil.createProgressRequest(
509-
requestBody,
510-
new ProgressListener() {
511-
long last = System.nanoTime();
512-
513-
@Override
514-
public void onProgress(long bytesWritten, long contentLength, boolean done) {
515-
long now = System.nanoTime();
516-
if (done || shouldDispatch(now, last)) {
517-
ResponseUtil.onDataSend(eventEmitter, requestId, bytesWritten, contentLength);
518-
last = now;
519-
}
520-
}
521-
});
522-
}
523-
524518
private void readWithProgress(
525519
RCTDeviceEventEmitter eventEmitter,
526520
int requestId,

ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.java

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,60 @@
99

1010
package com.facebook.react.modules.network;
1111

12-
import com.facebook.common.internal.CountingOutputStream;
13-
1412
import java.io.IOException;
15-
1613
import okhttp3.MediaType;
1714
import okhttp3.RequestBody;
1815
import okio.BufferedSink;
19-
import okio.Okio;
16+
import okio.Buffer;
2017
import okio.Sink;
18+
import okio.ForwardingSink;
19+
import okio.Okio;
2120

2221
public class ProgressRequestBody extends RequestBody {
2322

2423
private final RequestBody mRequestBody;
2524
private final ProgressListener mProgressListener;
2625
private BufferedSink mBufferedSink;
27-
private long mContentLength = 0L;
2826

2927
public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) {
30-
mRequestBody = requestBody;
31-
mProgressListener = progressListener;
28+
mRequestBody = requestBody;
29+
mProgressListener = progressListener;
3230
}
3331

3432
@Override
3533
public MediaType contentType() {
36-
return mRequestBody.contentType();
34+
return mRequestBody.contentType();
3735
}
3836

3937
@Override
4038
public long contentLength() throws IOException {
41-
if (mContentLength == 0) {
42-
mContentLength = mRequestBody.contentLength();
43-
}
44-
return mContentLength;
39+
return mRequestBody.contentLength();
4540
}
4641

4742
@Override
4843
public void writeTo(BufferedSink sink) throws IOException {
49-
if (mBufferedSink == null) {
50-
mBufferedSink = Okio.buffer(outputStreamSink(sink));
51-
}
52-
53-
// contentLength changes for input streams, since we're using inputStream.available(),
54-
// so get the length before writing to the sink
55-
contentLength();
56-
57-
mRequestBody.writeTo(mBufferedSink);
58-
mBufferedSink.flush();
44+
if (mBufferedSink == null) {
45+
mBufferedSink = Okio.buffer(sink(sink));
46+
}
47+
mRequestBody.writeTo(mBufferedSink);
48+
mBufferedSink.flush();
5949
}
6050

61-
private Sink outputStreamSink(BufferedSink sink) {
62-
return Okio.sink(new CountingOutputStream(sink.outputStream()) {
63-
@Override
64-
public void write(byte[] data, int offset, int byteCount) throws IOException {
65-
super.write(data, offset, byteCount);
66-
sendProgressUpdate();
67-
}
51+
private Sink sink(Sink sink) {
52+
return new ForwardingSink(sink) {
53+
long bytesWritten = 0L;
54+
long contentLength = 0L;
6855

69-
@Override
70-
public void write(int data) throws IOException {
71-
super.write(data);
72-
sendProgressUpdate();
73-
}
74-
75-
private void sendProgressUpdate() throws IOException {
76-
long bytesWritten = getCount();
77-
long contentLength = contentLength();
78-
mProgressListener.onProgress(
79-
bytesWritten, contentLength, bytesWritten == contentLength);
80-
}
81-
});
56+
@Override
57+
public void write(Buffer source, long byteCount) throws IOException {
58+
super.write(source, byteCount);
59+
if (contentLength == 0) {
60+
contentLength = contentLength();
61+
}
62+
bytesWritten += byteCount;
63+
mProgressListener.onProgress(
64+
bytesWritten, contentLength, bytesWritten == contentLength);
65+
}
66+
};
8267
}
83-
}
68+
}

ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ public WritableMap answer(InvocationOnMock invocation) throws Throwable {
200200

201201
@Test
202202
public void testSuccessfulPostRequest() throws Exception {
203-
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
204-
ReactApplicationContext context = mock(ReactApplicationContext.class);
205-
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
206-
207203
OkHttpClient httpClient = mock(OkHttpClient.class);
208204
when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer<Object>() {
209205
@Override
@@ -215,13 +211,12 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
215211
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
216212
when(clientBuilder.build()).thenReturn(httpClient);
217213
when(httpClient.newBuilder()).thenReturn(clientBuilder);
218-
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
214+
NetworkingModule networkingModule =
215+
new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient);
219216

220217
JavaOnlyMap body = new JavaOnlyMap();
221218
body.putString("string", "This is request body");
222219

223-
mockEvents();
224-
225220
networkingModule.sendRequest(
226221
"POST",
227222
"http://somedomain/bar",
@@ -635,4 +630,4 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
635630
assertThat(requestIdArguments.getAllValues().contains(idx + 1)).isTrue();
636631
}
637632
}
638-
}
633+
}

0 commit comments

Comments
 (0)