-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support asynchronous disconnects by breaking the socket only, which should cause the thread using that socket to trigger clean-up.
- Loading branch information
1 parent
b21b40e
commit 9c30213
Showing
8 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/DisconnectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.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 = 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 = 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters