/*
 * Copyright (C) 2014 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squareup.okhttp.internal.http;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

import static org.junit.Assert.fail;

public final class DisconnectTest {
  private final MockWebServer server = new MockWebServer();
  private final OkHttpClient client = new OkHttpClient();

  @Test public void interruptWritingRequestBody() throws Exception {
    int requestBodySize = 2 * 1024 * 1024; // 2 MiB

    server.enqueue(new MockResponse()
        .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
    server.play();

    HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
    disconnectLater(connection, 500);

    connection.setDoOutput(true);
    connection.setFixedLengthStreamingMode(requestBodySize);
    OutputStream requestBody = connection.getOutputStream();
    byte[] buffer = new byte[1024];
    try {
      for (int i = 0; i < requestBodySize; i += buffer.length) {
        requestBody.write(buffer);
        requestBody.flush();
      }
      fail("Expected connection to be closed");
    } catch (IOException expected) {
    }

    connection.disconnect();
  }

  @Test public void interruptReadingResponseBody() throws Exception {
    int responseBodySize = 2 * 1024 * 1024; // 2 MiB

    server.enqueue(new MockResponse()
        .setBody(new byte[responseBodySize])
        .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
    server.play();

    HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
    disconnectLater(connection, 500);

    InputStream responseBody = connection.getInputStream();
    byte[] buffer = new byte[1024];
    try {
      while (responseBody.read(buffer) != -1) {
      }
      fail("Expected connection to be closed");
    } catch (IOException expected) {
    }

    connection.disconnect();
  }

  private void disconnectLater(final HttpURLConnection connection, final int delayMillis) {
    Thread interruptingCow = new Thread() {
      @Override public void run() {
        try {
          sleep(delayMillis);
          connection.disconnect();
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
    };
    interruptingCow.start();
  }
}

@@ -638,16 +638,126 @@
 .http;%0A%0A
+import com.squareup.okhttp.DelegatingServerSocketFactory;%0Aimport com.squareup.okhttp.DelegatingSocketFactory;%0A
 import c
@@ -1077,249 +1077,1379 @@
 ava.
-util.concurrent.TimeUnit;%0Aimport org.junit.Test;%0A%0Aimport static org.junit.Assert.fail;%0A%0Apublic final class DisconnectTest %7B%0A  private final MockWebServer server = new MockWebServer();%0A  private final OkHttpClient client = new OkHttpClient();
+net.ServerSocket;%0Aimport java.net.Socket;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport javax.net.ServerSocketFactory;%0Aimport javax.net.SocketFactory;%0A%0Aimport static org.junit.Assert.fail;%0A%0Apublic final class DisconnectTest %7B%0A%0A  // The size of the socket buffers in bytes.%0A  private static final int SOCKET_BUFFER_SIZE = 256 * 1024;%0A%0A  private MockWebServer server;%0A  private OkHttpClient client;%0A%0A  @Before public void setUp() throws Exception %7B%0A    server = new MockWebServer();%0A    client = new OkHttpClient();%0A%0A    // Sockets on some platforms can have large buffers that mean writes do not block when%0A    // required. These socket factories explicitly set the buffer sizes on sockets created.%0A    server.setServerSocketFactory(%0A        new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) %7B%0A          @Override%0A          protected void configureServerSocket(ServerSocket serverSocket) throws IOException %7B%0A            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);%0A          %7D%0A        %7D);%0A    client.setSocketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) %7B%0A      @Override%0A      protected void configureSocket(Socket socket) throws IOException %7B%0A        socket.setSendBufferSize(SOCKET_BUFFER_SIZE);%0A        socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);%0A      %7D%0A    %7D);%0A  %7D
 %0A%0A  

Apply fix for devices with large socket buffers