Skip to content

Commit

Permalink
New API, Chain.call() returns the in-flight call. (#3557)
Browse files Browse the repository at this point in the history
Interceptors could use this to detect if the call has been canceled, or
cancel the call, or do other things.
  • Loading branch information
swankjesse committed Aug 30, 2017
1 parent 16f43b6 commit 36bd68a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
39 changes: 38 additions & 1 deletion okhttp-tests/src/test/java/okhttp3/InterceptorTest.java
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
Expand All @@ -41,9 +42,11 @@

import static okhttp3.TestUtil.defaultClient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public final class InterceptorTest {
Expand Down Expand Up @@ -820,12 +823,46 @@ private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) thro
Call call = client.newCall(request1);

try {
Response response = call.execute(); // we want this call to throw a SocketTimeoutException
call.execute(); // we want this call to throw a SocketTimeoutException
fail();
} catch (SocketTimeoutException expected) {
}
}

@Test public void chainCanCancelCall() throws Exception {
final AtomicReference<Call> callRef = new AtomicReference<>();

Interceptor interceptor = new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
Call call = chain.call();
callRef.set(call);

assertFalse(call.isCanceled());
call.cancel();
assertTrue(call.isCanceled());

return chain.proceed(chain.request());
}
};

client = client.newBuilder()
.addInterceptor(interceptor)
.build();

Request request = new Request.Builder()
.url(server.url("/"))
.build();
Call call = client.newCall(request);

try {
call.execute();
fail();
} catch (IOException expected) {
}

assertSame(call, callRef.get());
}

private RequestBody uppercase(final RequestBody original) {
return new RequestBody() {
@Override public MediaType contentType() {
Expand Down
2 changes: 2 additions & 0 deletions okhttp/src/main/java/okhttp3/Interceptor.java
Expand Up @@ -38,6 +38,8 @@ interface Chain {
*/
@Nullable Connection connection();

Call call();

int connectTimeoutMillis();

Chain withConnectTimeout(int timeout, TimeUnit unit);
Expand Down
Expand Up @@ -105,7 +105,7 @@ public HttpCodec httpStream() {
return httpCodec;
}

public Call call() {
@Override public Call call() {
return call;
}

Expand Down

0 comments on commit 36bd68a

Please sign in to comment.