1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Merge pull request #421 from lingmingyb/test_spdyTimeout

Testcase for spdy connection socket timeout.
This commit is contained in:
Jesse Wilson
2014-01-09 09:00:00 -08:00
3 changed files with 35 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ public final class MockResponse implements Cloneable {
private int bytesPerSecond = Integer.MAX_VALUE;
private SocketPolicy socketPolicy = SocketPolicy.KEEP_OPEN;
private int bodyDelayTimeMs = 0;
/** Creates a new mock response with an empty body. */
public MockResponse() {
setBody(new byte[0]);
@@ -216,6 +217,18 @@ public final class MockResponse implements Cloneable {
this.bytesPerSecond = bytesPerSecond;
return this;
}
/**
* Set the delayed time of the response body to {@code delay}. This applies to the
* response body only; response headers are not affected.
*/
public MockResponse setBodyDelayTimeMs(int delay) {
bodyDelayTimeMs = delay;
return this;
}
public int getBodyDelayTimeMs() {
return bodyDelayTimeMs;
}
@Override public String toString() {
return status;

View File

@@ -729,6 +729,13 @@ public final class MockWebServer {
byte[] body = response.getBody();
stream.reply(spdyHeaders, body.length > 0);
if (body.length > 0) {
if (response.getBodyDelayTimeMs() != 0) {
try {
Thread.sleep(response.getBodyDelayTimeMs());
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
stream.getOutputStream().write(body);
stream.getOutputStream().close();
}

View File

@@ -271,6 +271,21 @@ public abstract class HttpOverSpdyTest {
assertContent("A", connection, Integer.MAX_VALUE);
}
@Test public void spdyConnectionTimeout() throws Exception {
MockResponse response = new MockResponse().setBody("A");
response.setBodyDelayTimeMs(1000);
server.enqueue(response);
server.play();
HttpURLConnection connection1 = client.open(server.getUrl("/"));
connection1.setReadTimeout(2000);
HttpURLConnection connection2 = client.open(server.getUrl("/"));
connection2.setReadTimeout(200);
connection1.connect();
connection2.connect();
assertContent("A", connection1, Integer.MAX_VALUE);
}
@Test public void responsesAreCached() throws IOException {
client.setResponseCache(cache);