Search in sources :

Example 21 with ConnectionPool

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 22 with ConnectionPool

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 23 with ConnectionPool

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) Test(org.junit.Test)

Example 24 with ConnectionPool

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 25 with ConnectionPool

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();
    }
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec)

Aggregations

ConnectionPool (okhttp3.ConnectionPool)12 Test (org.junit.Test)12 OkHttpClient (okhttp3.OkHttpClient)10 MockResponse (okhttp3.mockwebserver.MockResponse)8 IOException (java.io.IOException)5 RealConnection (okhttp3.internal.connection.RealConnection)5 Field (java.lang.reflect.Field)4 Dispatcher (okhttp3.Dispatcher)3 Socket (java.net.Socket)2 Level (java.util.logging.Level)2 SimpleFormatter (java.util.logging.SimpleFormatter)2 HttpCodec (okhttp3.internal.http.HttpCodec)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 BuckConfig (com.facebook.buck.cli.BuckConfig)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 DirCacheExperimentEvent (com.facebook.buck.event.DirCacheExperimentEvent)1 BytesReceivedEvent (com.facebook.buck.event.NetworkEvent.BytesReceivedEvent)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 CommandThreadFactory (com.facebook.buck.log.CommandThreadFactory)1 Logger (com.facebook.buck.log.Logger)1