PageRenderTime 98ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/okhttp-tests/src/test/java/okhttp3/internal/http/DisconnectTest.java

https://gitlab.com/JoshLucid/okhttp
Java | 132 lines | 97 code | 17 blank | 18 comment | 3 complexity | 2445bc75af21cc55d1f67a40b3c4f695 MD5 | raw file
  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. */
  16. package okhttp3.internal.http;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.net.HttpURLConnection;
  21. import java.net.ServerSocket;
  22. import java.net.Socket;
  23. import java.util.concurrent.TimeUnit;
  24. import javax.net.ServerSocketFactory;
  25. import javax.net.SocketFactory;
  26. import okhttp3.DelegatingServerSocketFactory;
  27. import okhttp3.DelegatingSocketFactory;
  28. import okhttp3.OkHttpClient;
  29. import okhttp3.OkUrlFactory;
  30. import okhttp3.mockwebserver.MockResponse;
  31. import okhttp3.mockwebserver.MockWebServer;
  32. import okio.Buffer;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import static org.junit.Assert.fail;
  36. public final class DisconnectTest {
  37. // The size of the socket buffers in bytes.
  38. private static final int SOCKET_BUFFER_SIZE = 256 * 1024;
  39. private MockWebServer server;
  40. private OkHttpClient client;
  41. @Before public void setUp() throws Exception {
  42. // Sockets on some platforms can have large buffers that mean writes do not block when
  43. // required. These socket factories explicitly set the buffer sizes on sockets created.
  44. server = new MockWebServer();
  45. server.setServerSocketFactory(
  46. new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
  47. @Override protected ServerSocket configureServerSocket(
  48. ServerSocket serverSocket) throws IOException {
  49. serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
  50. return serverSocket;
  51. }
  52. });
  53. client = new OkHttpClient.Builder()
  54. .socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
  55. @Override protected Socket configureSocket(Socket socket) throws IOException {
  56. socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
  57. socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
  58. return socket;
  59. }
  60. })
  61. .build();
  62. }
  63. @Test public void interruptWritingRequestBody() throws Exception {
  64. int requestBodySize = 2 * 1024 * 1024; // 2 MiB
  65. server.enqueue(new MockResponse()
  66. .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
  67. server.start();
  68. HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
  69. disconnectLater(connection, 500);
  70. connection.setDoOutput(true);
  71. connection.setFixedLengthStreamingMode(requestBodySize);
  72. OutputStream requestBody = connection.getOutputStream();
  73. byte[] buffer = new byte[1024];
  74. try {
  75. for (int i = 0; i < requestBodySize; i += buffer.length) {
  76. requestBody.write(buffer);
  77. requestBody.flush();
  78. }
  79. fail("Expected connection to be closed");
  80. } catch (IOException expected) {
  81. }
  82. connection.disconnect();
  83. }
  84. @Test public void interruptReadingResponseBody() throws Exception {
  85. int responseBodySize = 2 * 1024 * 1024; // 2 MiB
  86. server.enqueue(new MockResponse()
  87. .setBody(new Buffer().write(new byte[responseBodySize]))
  88. .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
  89. server.start();
  90. HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
  91. disconnectLater(connection, 500);
  92. InputStream responseBody = connection.getInputStream();
  93. byte[] buffer = new byte[1024];
  94. try {
  95. while (responseBody.read(buffer) != -1) {
  96. }
  97. fail("Expected connection to be closed");
  98. } catch (IOException expected) {
  99. }
  100. connection.disconnect();
  101. }
  102. private void disconnectLater(final HttpURLConnection connection, final int delayMillis) {
  103. Thread interruptingCow = new Thread() {
  104. @Override public void run() {
  105. try {
  106. sleep(delayMillis);
  107. connection.disconnect();
  108. } catch (InterruptedException e) {
  109. throw new RuntimeException(e);
  110. }
  111. }
  112. };
  113. interruptingCow.start();
  114. }
  115. }