Skip to content

Commit 014326c

Browse files
Thomas Nardonefacebook-github-bot
authored andcommitted
Remove GuardedAsyncTask wrapper for call.cancel() (facebook#48251)
Summary: The original comment mentioning square/okhttp#869 predates the [upgrade to okhttp3](facebook@6bbaff2), which resolved the issue via square/okhttp#1592. Cancel calls should now be async and don't need to be guarded. Removing this also fixes a discrepancy in NetworkingModule unit test verification. Changelog: [Internal] Reviewed By: javache Differential Revision: D67157018
1 parent e577e48 commit 014326c

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.facebook.common.logging.FLog;
1515
import com.facebook.fbreact.specs.NativeNetworkingAndroidSpec;
1616
import com.facebook.react.bridge.Arguments;
17-
import com.facebook.react.bridge.GuardedAsyncTask;
1817
import com.facebook.react.bridge.ReactApplicationContext;
1918
import com.facebook.react.bridge.ReactMethod;
2019
import com.facebook.react.bridge.ReadableArray;
@@ -673,14 +672,7 @@ public void abortRequest(double requestIdAsDouble) {
673672
}
674673

675674
private void cancelRequest(final int requestId) {
676-
// We have to use AsyncTask since this might trigger a NetworkOnMainThreadException, this is an
677-
// open issue on OkHttp: https://github.com/square/okhttp/issues/869
678-
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
679-
@Override
680-
protected void doInBackgroundGuarded(Void... params) {
681-
OkHttpCallUtil.cancelTag(mClient, Integer.valueOf(requestId));
682-
}
683-
}.execute();
675+
OkHttpCallUtil.cancelTag(mClient, Integer.valueOf(requestId));
684676
}
685677

686678
@ReactMethod

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import okio.Buffer
3030
import org.assertj.core.api.Assertions.assertThat
3131
import org.junit.After
3232
import org.junit.Before
33-
import org.junit.Ignore
3433
import org.junit.Test
3534
import org.junit.runner.RunWith
3635
import org.mockito.MockedStatic
@@ -73,7 +72,7 @@ class NetworkingModuleTest {
7372
context = mock()
7473
whenever(context.hasActiveReactInstance()).thenReturn(true)
7574

76-
networkingModule = NetworkingModule(context, "", httpClient)
75+
networkingModule = NetworkingModule(context, "", httpClient, null)
7776

7877
arguments = mockStatic(Arguments::class.java)
7978
arguments.`when`<WritableArray> { Arguments.createArray() }.thenAnswer { JavaOnlyArray() }
@@ -477,12 +476,11 @@ class NetworkingModuleTest {
477476
}
478477

479478
@Test
480-
@Ignore("TODO: Fix me (T171890419)")
481479
fun testCancelAllCallsInvalidate() {
482480
val requests = 3
483-
val calls = arrayOfNulls<Call>(requests)
481+
val calls = mutableListOf<Call>()
484482
for (idx in 0 until requests) {
485-
calls[idx] = mock<Call>()
483+
calls.add(mock<Call>())
486484
}
487485

488486
whenever(httpClient.newCall(any())).thenAnswer { invocation ->
@@ -507,11 +505,9 @@ class NetworkingModuleTest {
507505
verify(httpClient, times(3)).newCall(any())
508506

509507
networkingModule.invalidate()
510-
val clientArguments = argumentCaptor<OkHttpClient>()
511508
val requestIdArguments = argumentCaptor<Int>()
512509
okHttpCallUtil.verify(
513-
{ OkHttpCallUtil.cancelTag(clientArguments.capture(), requestIdArguments.capture()) },
514-
times(3))
510+
{ OkHttpCallUtil.cancelTag(any(), requestIdArguments.capture()) }, times(requests))
515511

516512
assertThat(requestIdArguments.allValues.size).isEqualTo(requests)
517513
for (idx in 0 until requests) {
@@ -520,7 +516,6 @@ class NetworkingModuleTest {
520516
}
521517

522518
@Test
523-
@Ignore("TODO: Fix me (T171890419)")
524519
fun testCancelSomeCallsInvalidate() {
525520
val requests = 3
526521
val calls = arrayOfNulls<Call>(requests)

0 commit comments

Comments
 (0)