Search in sources :

Example 16 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class UrlConnectionCacheTest method conditionalCacheHitIsNotDoublePooled.

@Test
public void conditionalCacheHitIsNotDoublePooled() throws Exception {
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
    server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
    assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
    assertEquals(1, urlFactory.client().connectionPool().idleConnectionCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 17 with ConnectionPool

use of okhttp3.ConnectionPool in project retrofit by square.

the class Crawler method main.

public static void main(String... args) throws Exception {
    Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
    dispatcher.setMaxRequests(20);
    dispatcher.setMaxRequestsPerHost(1);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(HttpUrl.parse("https://example.com/")).addConverterFactory(PageAdapter.FACTORY).client(okHttpClient).build();
    PageService pageService = retrofit.create(PageService.class);
    Crawler crawler = new Crawler(pageService);
    crawler.crawlPage(HttpUrl.parse(args[0]));
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher)

Example 18 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class ConnectionPoolTest method oldestConnectionsEvictedIfIdleLimitExceeded.

@Test
public void oldestConnectionsEvictedIfIdleLimitExceeded() throws Exception {
    ConnectionPool pool = new ConnectionPool(2, 100L, TimeUnit.NANOSECONDS);
    // Prevent the cleanup runnable from being started.
    pool.cleanupRunning = true;
    RealConnection c1 = newConnection(pool, routeA1, 50L);
    RealConnection c2 = newConnection(pool, routeB1, 75L);
    // With 2 connections, there's no need to evict until the connections time out.
    assertEquals(50L, pool.cleanup(100L));
    assertEquals(2, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    assertFalse(c2.socket().isClosed());
    // Add a third connection
    RealConnection c3 = newConnection(pool, routeC1, 75L);
    // The third connection bounces the first.
    assertEquals(0L, pool.cleanup(100L));
    assertEquals(2, pool.connectionCount());
    assertTrue(c1.socket().isClosed());
    assertFalse(c2.socket().isClosed());
    assertFalse(c3.socket().isClosed());
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

Example 19 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class ConnectionPoolTest method allocateAndLeakAllocation.

/** Use a helper method so there's no hidden reference remaining on the stack. */
private void allocateAndLeakAllocation(ConnectionPool pool, RealConnection connection) {
    synchronized (pool) {
        StreamAllocation leak = new StreamAllocation(pool, connection.route().address(), null);
        leak.acquire(connection);
    }
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation)

Example 20 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class ConnectionPoolTest method cleanupPrioritizesEarliestEviction.

@Test
public void cleanupPrioritizesEarliestEviction() throws Exception {
    ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
    // Prevent the cleanup runnable from being started.
    pool.cleanupRunning = true;
    RealConnection c1 = newConnection(pool, routeA1, 75L);
    RealConnection c2 = newConnection(pool, routeB1, 50L);
    // Running at time 75, the pool returns that nothing can be evicted until time 150.
    assertEquals(75L, pool.cleanup(75L));
    assertEquals(2, pool.connectionCount());
    // Running at time 149, the pool returns that nothing can be evicted until time 150.
    assertEquals(1L, pool.cleanup(149L));
    assertEquals(2, pool.connectionCount());
    // Running at time 150, the pool evicts c2.
    assertEquals(0L, pool.cleanup(150L));
    assertEquals(1, pool.connectionCount());
    assertFalse(c1.socket().isClosed());
    assertTrue(c2.socket().isClosed());
    // Running at time 150, the pool returns that nothing can be evicted until time 175.
    assertEquals(25L, pool.cleanup(150L));
    assertEquals(1, pool.connectionCount());
    // Running at time 175, the pool evicts c1.
    assertEquals(0L, pool.cleanup(175L));
    assertEquals(0, pool.connectionCount());
    assertTrue(c1.socket().isClosed());
    assertTrue(c2.socket().isClosed());
}
Also used : RealConnection(okhttp3.internal.connection.RealConnection) Test(org.junit.Test)

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