Skip to content

Commit

Permalink
[cronvoy] fix flaky malloc error
Browse files Browse the repository at this point in the history
This was due to running two engines simultaneously which not
yet fully support(Issue envoyproxy#332)

Signed-off-by: Chidera Olibie <colibie@google.com>
  • Loading branch information
colibie committed Oct 25, 2022
1 parent 7bf274c commit 558cf94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions test/java/org/chromium/net/CronetUrlRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@

/**
* Test functionality of CronetUrlRequest.
* This test makes use of 2 cronetEngines.
* This test makes use of 2 separate cronetEngines.
* One with request filters to throw custom errors and the other without.
* TODO(colibie): switch to one engine, with the filter ignoring urls with no custom errors.
* They do not run simultaneously as EM does not support this.
* TODO(colibie): Consider using a single engine with filter that does nothing
* if the url does not indicate to throw an error
*/
@RunWith(RobolectricTestRunner.class)
public class CronetUrlRequestTest {
Expand All @@ -90,20 +92,22 @@ public class CronetUrlRequestTest {
public void setUp() {
mTestFramework = mTestRule.startCronetTestFramework();
assertTrue(NativeTestServer.startNativeTestServer(getContext()));
// Add url interceptors after native application context is initialized.
if (!mTestRule.testingJavaImpl()) {
mMockUrlRequestJobFactory = new MockUrlRequestJobFactory(mTestFramework.mBuilder);
}
}

@After
public void tearDown() {
if (!mTestRule.testingJavaImpl()) {
mMockUrlRequestJobFactory.shutdown();
}
NativeTestServer.shutdownNativeTestServer();
}

private void startCronetEngineForMockUrlRequestJobFactory() {
// Envoy doesnt support multiple concurrent engines yet so shut down the original
mTestFramework.shutdownEngine();
mTestFramework = null;
// Create a new engine with the url filter added.
mMockUrlRequestJobFactory =
new MockUrlRequestJobFactory(new ExperimentalCronetEngine.Builder(getContext()));
}

private TestUrlRequestCallback startAndWaitForComplete(CronetEngine engine, String url)
throws Exception {
TestUrlRequestCallback callback = new TestUrlRequestCallback();
Expand Down Expand Up @@ -734,6 +738,7 @@ public void testMockNotFound() throws Exception {
@OnlyRunNativeCronet
@Ignore("https://github.com/envoyproxy/envoy-mobile/issues/1549")
public void testMockClientCertificateRequested() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
TestUrlRequestCallback callback =
startAndWaitForComplete(mMockUrlRequestJobFactory.getCronetEngine(),
MockUrlRequestJobFactory.getMockUrlForClientCertificateRequest());
Expand All @@ -743,6 +748,7 @@ public void testMockClientCertificateRequested() throws Exception {
assertEquals(0, callback.mRedirectCount);
assertNull(callback.mError);
assertFalse(callback.mOnErrorCalled);
mMockUrlRequestJobFactory.shutdown();
}

/**
Expand All @@ -754,6 +760,7 @@ public void testMockClientCertificateRequested() throws Exception {
@OnlyRunNativeCronet // Java impl doesn't support MockUrlRequestJobFactory
@Ignore("https://github.com/envoyproxy/envoy-mobile/issues/1549")
public void testMockSSLCertificateError() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
TestUrlRequestCallback callback =
startAndWaitForComplete(mMockUrlRequestJobFactory.getCronetEngine(),
MockUrlRequestJobFactory.getMockUrlForSSLCertificateError());
Expand All @@ -764,6 +771,7 @@ public void testMockSSLCertificateError() throws Exception {
assertContains("Exception in CronetUrlRequest: net::ERR_CERT_DATE_INVALID",
callback.mError.getMessage());
assertEquals(ResponseStep.ON_FAILED, callback.mResponseStep);
mMockUrlRequestJobFactory.shutdown();
}

/**
Expand Down Expand Up @@ -2031,6 +2039,7 @@ public void testDestroyUploadDataStreamAdapterOnSucceededCallback() throws Excep
@Feature({"Cronet"})
@OnlyRunNativeCronet // Java impl doesn't support MockUrlRequestJobFactory
public void testErrorCodes() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
checkSpecificErrorCode(EnvoyMobileError.DNS_RESOLUTION_FAILED, NetError.ERR_NAME_NOT_RESOLVED,
NetworkException.ERROR_HOSTNAME_NOT_RESOLVED, "NAME_NOT_RESOLVED",
false);
Expand All @@ -2046,6 +2055,7 @@ public void testErrorCodes() throws Exception {
NetworkException.ERROR_TIMED_OUT, "TIMED_OUT", true);
checkSpecificErrorCode(0x2000, NetError.ERR_OTHER, NetworkException.ERROR_OTHER, "OTHER",
false);
mMockUrlRequestJobFactory.shutdown();
}

/*
Expand All @@ -2056,6 +2066,7 @@ public void testErrorCodes() throws Exception {
@Feature({"Cronet"})
@OnlyRunNativeCronet // Java impl doesn't support MockUrlRequestJobFactory
public void testInternetDisconnectedError() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
AndroidNetworkMonitor androidNetworkMonitor = AndroidNetworkMonitor.getInstance();
Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
// save old networkInfo before overriding
Expand All @@ -2075,6 +2086,8 @@ public void testInternetDisconnectedError() throws Exception {
// bring back online since the AndroidNetworkMonitor class is a singleton
connectivityManager.setActiveNetworkInfo(networkInfo);
androidNetworkMonitor.onReceive(getContext(), intent);

mMockUrlRequestJobFactory.shutdown();
}

/*
Expand Down Expand Up @@ -2103,6 +2116,7 @@ public void testCookiesArentSavedOrSent() throws Exception {
@Feature({"Cronet"})
@OnlyRunNativeCronet
public void testQuicErrorCode() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
long envoyMobileError = 0x2000;
TestUrlRequestCallback callback = startAndWaitForComplete(
mMockUrlRequestJobFactory.getCronetEngine(),
Expand All @@ -2115,6 +2129,7 @@ public void testQuicErrorCode() throws Exception {
QuicException quicException = (QuicException)callback.mError;
// 1 is QUIC_INTERNAL_ERROR
assertEquals(1, quicException.getQuicDetailedErrorCode());
mMockUrlRequestJobFactory.shutdown();
}

@Test
Expand All @@ -2123,6 +2138,7 @@ public void testQuicErrorCode() throws Exception {
@OnlyRunNativeCronet
@Ignore("https://github.com/envoyproxy/envoy-mobile/issues/1594: error removed in envoymobile")
public void testQuicErrorCodeForNetworkChanged() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
TestUrlRequestCallback callback =
startAndWaitForComplete(mMockUrlRequestJobFactory.getCronetEngine(),
MockUrlRequestJobFactory.getMockUrlWithFailure(
Expand All @@ -2137,6 +2153,7 @@ public void testQuicErrorCodeForNetworkChanged() throws Exception {
// URLRequestFailedJob::PopulateNetErrorDetails for this test.
final int quicErrorCode = 83;
assertEquals(quicErrorCode, quicException.getQuicDetailedErrorCode());
mMockUrlRequestJobFactory.shutdown();
}

/**
Expand All @@ -2148,6 +2165,7 @@ public void testQuicErrorCodeForNetworkChanged() throws Exception {
@Feature({"Cronet"})
@OnlyRunNativeCronet
public void testLegacyOnFailedCallback() throws Exception {
startCronetEngineForMockUrlRequestJobFactory();
final long envoyMobileError = 0x2000;
final int netError = NetError.ERR_OTHER.getErrorCode();
final AtomicBoolean failedExpectation = new AtomicBoolean();
Expand Down Expand Up @@ -2201,6 +2219,7 @@ public void onCanceled(UrlRequest request, UrlResponseInfo info) {
done.block();
// Check that onFailed is called.
assertFalse(failedExpectation.get());
mMockUrlRequestJobFactory.shutdown();
}

private void checkSpecificErrorCode(@EnvoyMobileError long envoyMobileError, NetError netError,
Expand Down
2 changes: 1 addition & 1 deletion test/java/org/chromium/net/testing/CronetTestRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private void setUp() {

private void tearDown() {
if (mCronetTestFramework != null) {
mCronetTestFramework.mCronetEngine.shutdown();
mCronetTestFramework.shutdownEngine();
mCronetTestFramework = null;
}

Expand Down

0 comments on commit 558cf94

Please sign in to comment.