Commit 7b20e130 by matttt Committed by Oliver Woodman

Allow a direct executor for Cronet's response handling thread.

We want to experiment with a direct executor to avoid thread hops between the
network thread and the response handling thread. This change is needed to do
so.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=161812382
parent 68e5e917
...@@ -124,6 +124,7 @@ public final class CronetDataSourceTest { ...@@ -124,6 +124,7 @@ public final class CronetDataSourceTest {
when(mockCronetEngine.newUrlRequestBuilder( when(mockCronetEngine.newUrlRequestBuilder(
anyString(), any(UrlRequest.Callback.class), any(Executor.class))) anyString(), any(UrlRequest.Callback.class), any(Executor.class)))
.thenReturn(mockUrlRequestBuilder); .thenReturn(mockUrlRequestBuilder);
when(mockUrlRequestBuilder.allowDirectExecutor()).thenReturn(mockUrlRequestBuilder);
when(mockUrlRequestBuilder.build()).thenReturn(mockUrlRequest); when(mockUrlRequestBuilder.build()).thenReturn(mockUrlRequest);
mockStatusResponse(); mockStatusResponse();
...@@ -683,6 +684,15 @@ public final class CronetDataSourceTest { ...@@ -683,6 +684,15 @@ public final class CronetDataSourceTest {
} }
} }
@Test
public void testAllowDirectExecutor() throws HttpDataSourceException {
testDataSpec = new DataSpec(Uri.parse(TEST_URL), 1000, 5000, null);
mockResponseStartSuccess();
dataSourceUnderTest.open(testDataSpec);
verify(mockUrlRequestBuilder).allowDirectExecutor();
}
// Helper methods. // Helper methods.
private void mockStatusResponse() { private void mockStatusResponse() {
......
...@@ -127,7 +127,11 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -127,7 +127,11 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
/** /**
* @param cronetEngine A CronetEngine. * @param cronetEngine A CronetEngine.
* @param executor The {@link java.util.concurrent.Executor} that will perform the requests. * @param executor The {@link java.util.concurrent.Executor} that will handle responses.
* This may be a direct executor (i.e. executes tasks on the calling thread) in order
* to avoid a thread hop from Cronet's internal network thread to the response handling
* thread. However, to avoid slowing down overall network performance, care must be taken
* to make sure response handling is a fast operation when using a direct executor.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then an {@link InvalidContentTypeException} is thrown from * predicate then an {@link InvalidContentTypeException} is thrown from
* {@link #open(DataSpec)}. * {@link #open(DataSpec)}.
...@@ -141,7 +145,11 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -141,7 +145,11 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
/** /**
* @param cronetEngine A CronetEngine. * @param cronetEngine A CronetEngine.
* @param executor The {@link java.util.concurrent.Executor} that will perform the requests. * @param executor The {@link java.util.concurrent.Executor} that will handle responses.
* This may be a direct executor (i.e. executes tasks on the calling thread) in order
* to avoid a thread hop from Cronet's internal network thread to the response handling
* thread. However, to avoid slowing down overall network performance, care must be taken
* to make sure response handling is a fast operation when using a direct executor.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then an {@link InvalidContentTypeException} is thrown from * predicate then an {@link InvalidContentTypeException} is thrown from
* {@link #open(DataSpec)}. * {@link #open(DataSpec)}.
...@@ -416,8 +424,8 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -416,8 +424,8 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
// Internal methods. // Internal methods.
private UrlRequest buildRequest(DataSpec dataSpec) throws OpenException { private UrlRequest buildRequest(DataSpec dataSpec) throws OpenException {
UrlRequest.Builder requestBuilder = cronetEngine.newUrlRequestBuilder(dataSpec.uri.toString(), UrlRequest.Builder requestBuilder = cronetEngine.newUrlRequestBuilder(
this, executor); dataSpec.uri.toString(), this, executor).allowDirectExecutor();
// Set the headers. // Set the headers.
boolean isContentTypeHeaderSet = false; boolean isContentTypeHeaderSet = false;
if (defaultRequestProperties != null) { if (defaultRequestProperties != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment