|
24 | 24 | import java.util.concurrent.SynchronousQueue;
|
25 | 25 | import java.util.concurrent.ThreadPoolExecutor;
|
26 | 26 | import java.util.concurrent.TimeUnit;
|
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
27 | 28 | import okhttp3.mockwebserver.MockResponse;
|
28 | 29 | import okhttp3.mockwebserver.MockWebServer;
|
29 | 30 | import okhttp3.mockwebserver.RecordedRequest;
|
|
41 | 42 |
|
42 | 43 | import static okhttp3.TestUtil.defaultClient;
|
43 | 44 | import static org.junit.Assert.assertEquals;
|
| 45 | +import static org.junit.Assert.assertFalse; |
44 | 46 | import static org.junit.Assert.assertNotNull;
|
45 | 47 | import static org.junit.Assert.assertNull;
|
46 | 48 | import static org.junit.Assert.assertSame;
|
| 49 | +import static org.junit.Assert.assertTrue; |
47 | 50 | import static org.junit.Assert.fail;
|
48 | 51 |
|
49 | 52 | public final class InterceptorTest {
|
@@ -820,12 +823,46 @@ private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) thro
|
820 | 823 | Call call = client.newCall(request1);
|
821 | 824 |
|
822 | 825 | try {
|
823 |
| - Response response = call.execute(); // we want this call to throw a SocketTimeoutException |
| 826 | + call.execute(); // we want this call to throw a SocketTimeoutException |
824 | 827 | fail();
|
825 | 828 | } catch (SocketTimeoutException expected) {
|
826 | 829 | }
|
827 | 830 | }
|
828 | 831 |
|
| 832 | + @Test public void chainCanCancelCall() throws Exception { |
| 833 | + final AtomicReference<Call> callRef = new AtomicReference<>(); |
| 834 | + |
| 835 | + Interceptor interceptor = new Interceptor() { |
| 836 | + @Override public Response intercept(Chain chain) throws IOException { |
| 837 | + Call call = chain.call(); |
| 838 | + callRef.set(call); |
| 839 | + |
| 840 | + assertFalse(call.isCanceled()); |
| 841 | + call.cancel(); |
| 842 | + assertTrue(call.isCanceled()); |
| 843 | + |
| 844 | + return chain.proceed(chain.request()); |
| 845 | + } |
| 846 | + }; |
| 847 | + |
| 848 | + client = client.newBuilder() |
| 849 | + .addInterceptor(interceptor) |
| 850 | + .build(); |
| 851 | + |
| 852 | + Request request = new Request.Builder() |
| 853 | + .url(server.url("/")) |
| 854 | + .build(); |
| 855 | + Call call = client.newCall(request); |
| 856 | + |
| 857 | + try { |
| 858 | + call.execute(); |
| 859 | + fail(); |
| 860 | + } catch (IOException expected) { |
| 861 | + } |
| 862 | + |
| 863 | + assertSame(call, callRef.get()); |
| 864 | + } |
| 865 | + |
829 | 866 | private RequestBody uppercase(final RequestBody original) {
|
830 | 867 | return new RequestBody() {
|
831 | 868 | @Override public MediaType contentType() {
|
|
0 commit comments