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());
}
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]));
}
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());
}
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);
}
}
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());
}
Aggregations