use of okhttp3.ConnectionPool in project okhttp by square.
the class ConnectionReuseTest method connectionsNotReusedWithRedirectIfDiscardingResponseIsSlow.
@Test
public void connectionsNotReusedWithRedirectIfDiscardingResponseIsSlow() throws Exception {
client = client.newBuilder().connectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS)).build();
server.enqueue(new MockResponse().setResponseCode(301).addHeader("Location: /b").setBodyDelay(1, TimeUnit.SECONDS).setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertEquals("b", response.body().string());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class ConnectionReuseTest method connectionsAreEvicted.
@Test
public void connectionsAreEvicted() throws Exception {
server.enqueue(new MockResponse().setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
client = client.newBuilder().connectionPool(new ConnectionPool(5, 250, TimeUnit.MILLISECONDS)).build();
Request request = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request).execute();
assertEquals("a", response1.body().string());
// Give the thread pool a chance to evict.
Thread.sleep(500);
Response response2 = client.newCall(request).execute();
assertEquals("b", response2.body().string());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class URLConnectionTest method endOfStreamResponseIsNotPooled.
@Test
public void endOfStreamResponseIsNotPooled() throws Exception {
urlFactory.client().connectionPool().evictAll();
server.enqueue(new MockResponse().setBody("{}").clearHeaders().setSocketPolicy(DISCONNECT_AT_END));
HttpURLConnection connection = urlFactory.open(server.url("/").url());
assertContent("{}", connection);
assertEquals(0, urlFactory.client().connectionPool().idleConnectionCount());
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnections.
private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
// The pool will only reuse sockets if the SSL socket factories are the same.
SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
ConnectionPool connectionPool = new ConnectionPool();
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
connection = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection);
if (rebuildClient) {
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
}
connection = urlFactory.open(server.url("/").url());
assertContent("another response via HTTPS", connection);
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.ConnectionPool in project okhttp by square.
the class StreamAllocation method cancel.
public void cancel() {
HttpCodec codecToCancel;
RealConnection connectionToCancel;
synchronized (connectionPool) {
canceled = true;
codecToCancel = codec;
connectionToCancel = connection;
}
if (codecToCancel != null) {
codecToCancel.cancel();
} else if (connectionToCancel != null) {
connectionToCancel.cancel();
}
}
Aggregations