DisconnectTest.java revision 342d21a7eb7d28ae132c43a4ab8353cbedff9e01
1/* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.squareup.okhttp.internal.http; 17 18import com.squareup.okhttp.OkHttpClient; 19import com.squareup.okhttp.mockwebserver.MockResponse; 20import com.squareup.okhttp.mockwebserver.MockWebServer; 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.net.HttpURLConnection; 25import java.util.concurrent.TimeUnit; 26import org.junit.Test; 27 28import static org.junit.Assert.fail; 29 30public final class DisconnectTest { 31 private final MockWebServer server = new MockWebServer(); 32 private final OkHttpClient client = new OkHttpClient(); 33 34 @Test public void interruptWritingRequestBody() throws Exception { 35 int requestBodySize = 10 * 1024 * 1024; // 10 MiB 36 37 server.enqueue(new MockResponse() 38 .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps 39 server.play(); 40 41 HttpURLConnection connection = client.open(server.getUrl("/")); 42 disconnectLater(connection, 500); 43 44 connection.setDoOutput(true); 45 connection.setFixedLengthStreamingMode(requestBodySize); 46 OutputStream requestBody = connection.getOutputStream(); 47 byte[] buffer = new byte[1024]; 48 try { 49 for (int i = 0; i < requestBodySize; i += buffer.length) { 50 requestBody.write(buffer); 51 requestBody.flush(); 52 } 53 fail("Expected connection to be closed"); 54 } catch (IOException expected) { 55 } 56 57 connection.disconnect(); 58 } 59 60 @Test public void interruptReadingResponseBody() throws Exception { 61 int responseBodySize = 10 * 1024 * 1024; // 10 MiB 62 63 server.enqueue(new MockResponse() 64 .setBody(new byte[responseBodySize]) 65 .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps 66 server.play(); 67 68 HttpURLConnection connection = client.open(server.getUrl("/")); 69 disconnectLater(connection, 500); 70 71 InputStream responseBody = connection.getInputStream(); 72 byte[] buffer = new byte[1024]; 73 try { 74 while (responseBody.read(buffer) != -1) { 75 } 76 fail("Expected connection to be closed"); 77 } catch (IOException expected) { 78 } 79 80 connection.disconnect(); 81 } 82 83 private void disconnectLater(final HttpURLConnection connection, final int delayMillis) { 84 Thread interruptingCow = new Thread() { 85 @Override public void run() { 86 try { 87 sleep(delayMillis); 88 connection.disconnect(); 89 } catch (InterruptedException e) { 90 throw new RuntimeException(e); 91 } 92 } 93 }; 94 interruptingCow.start(); 95 } 96} 97