public final ConnectionRequest requestConnection( final HttpRoute route, final Object state) { Args.notNull(route, "Route"); return new ConnectionRequest() { public boolean cancel() { // Nothing to abort, since requests are immediate. return false; } public HttpClientConnection get(final long timeout, final TimeUnit tunit) { return BasicHttpClientConnectionManager.this.getConnection( route, state); } }; }
public ConnectionRequest requestConnection( final HttpRoute route, final Object state) { Args.notNull(route, "HTTP route"); if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Connection request: " + format(route, state) + formatStats(route)); } final Future<CPoolEntry> future = this.pool.lease(route, state, null); return new ConnectionRequest() { public boolean cancel() { return future.cancel(true); } public HttpClientConnection get( final long timeout, final TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException { return leaseConnection(future, timeout, tunit); } }; }
@Override public final ConnectionRequest requestConnection( final HttpRoute route, final Object state) { Args.notNull(route, "Route"); return new ConnectionRequest() { @Override public boolean cancel() { // Nothing to abort, since requests are immediate. return false; } @Override public HttpClientConnection get(final long timeout, final TimeUnit tunit) { return BasicHttpClientConnectionManager.this.getConnection( route, state); } }; }
@Override public ConnectionRequest requestConnection( final HttpRoute route, final Object state) { Args.notNull(route, "HTTP route"); if (this.log.isDebugEnabled()) { this.log.debug("Connection request: " + format(route, state) + formatStats(route)); } final Future<CPoolEntry> future = this.pool.lease(route, state, null); return new ConnectionRequest() { @Override public boolean cancel() { return future.cancel(true); } @Override public HttpClientConnection get( final long timeout, final TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException { return leaseConnection(future, timeout, tunit); } }; }
@Test public void testLeaseReleaseNonReusable() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create( Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Assert.assertFalse(conn1.isOpen()); mgr.releaseConnection(conn1, null, 100, TimeUnit.MILLISECONDS); Assert.assertNull(mgr.getRoute()); Assert.assertNull(mgr.getState()); final ConnectionRequest connRequest2 = mgr.requestConnection(route, null); final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn2); Assert.assertFalse(conn2.isOpen()); Mockito.verify(connFactory, Mockito.times(2)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); }
@Test(expected=IllegalStateException.class) public void testAlreadyLeased() throws Exception { final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create( Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); mgr.releaseConnection(conn1, null, 100, TimeUnit.MILLISECONDS); mgr.getConnection(route, null); mgr.getConnection(route, null); }
@Test(expected=InterruptedException.class) public void testLeaseFutureCancelled() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); entry.markRouteComplete(); Mockito.when(future.isCancelled()).thenReturn(Boolean.TRUE); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); connRequest1.get(1, TimeUnit.SECONDS); }
@Test public void testReleaseReusable() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); final CPoolEntry entry = Mockito.spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS)); entry.markRouteComplete(); Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); Assert.assertTrue(conn1.isOpen()); mgr.releaseConnection(conn1, "some state", 0, TimeUnit.MILLISECONDS); Mockito.verify(pool).release(entry, true); Mockito.verify(entry).setState("some state"); Mockito.verify(entry).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS)); }
@Test public void testReleaseNonReusable() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); final CPoolEntry entry = Mockito.spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS)); entry.markRouteComplete(); Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); Mockito.when(conn.isOpen()).thenReturn(Boolean.FALSE); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); Assert.assertFalse(conn1.isOpen()); mgr.releaseConnection(conn1, "some state", 0, TimeUnit.MILLISECONDS); Mockito.verify(pool).release(entry, false); Mockito.verify(entry, Mockito.never()).setState(Mockito.anyObject()); Mockito.verify(entry, Mockito.never()).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS)); }
/** * Returns a wrapped instance of {@link ConnectionRequest} * to capture the necessary performance metrics. * * @param orig the target instance to be wrapped */ static ConnectionRequest wrap(ConnectionRequest orig) { if (orig instanceof Wrapped) { throw new IllegalArgumentException(); } return (ConnectionRequest) Proxy.newProxyInstance( // https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423 ClientConnectionRequestFactory.class.getClassLoader(), INTERFACES, new Handler(orig)); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { Object ret = method.invoke(orig, args); return ret instanceof ConnectionRequest ? ClientConnectionRequestFactory.wrap((ConnectionRequest) ret) : ret ; } catch (InvocationTargetException e) { log.debug("", e); throw e.getCause(); } }
/** * Returns a wrapped instance of {@link ConnectionRequest} * to capture the necessary performance metrics. * @param orig the target instance to be wrapped */ static ConnectionRequest wrap(ConnectionRequest orig) { if (orig instanceof Wrapped) throw new IllegalArgumentException(); return (ConnectionRequest) Proxy.newProxyInstance( // https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423 ClientConnectionRequestFactory.class.getClassLoader(), interfaces, new Handler(orig)); }
@Before public void requestConnection() throws Exception { HttpHost host = new HttpHost(InetAddress.getLocalHost(), inbound.getPort()); HttpRoute route = new HttpRoute(host, InetAddress.getLocalHost(), false); connManager = new BasicHttpClientConnectionManager(); ConnectionRequest connRequest = connManager.requestConnection(route, null); conn = connRequest.get(10, TimeUnit.SECONDS); // Make two requests on the same connection connManager.connect(conn, route, 10000, new BasicHttpContext()); }
@Test public void testReleaseOnEntityConsumeContent() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); EntityUtils.consume(e); // Expect one connection in the pool stats = this.connManager.getTotalStats(); Assert.assertEquals(1, stats.getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
@Test public void testReleaseOnEntityWriteTo() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); final ByteArrayOutputStream outsteam = new ByteArrayOutputStream(); e.writeTo(outsteam); // Expect one connection in the pool stats = this.connManager.getTotalStats(); Assert.assertEquals(1, stats.getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
@Test public void testReleaseOnAbort() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool final PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); httpget.abort(); // Expect zero connections in the pool Assert.assertEquals(0, this.connManager.getTotalStats().getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
private static HttpClientConnection getConnection( final HttpClientConnectionManager mgr, final HttpRoute route, final long timeout, final TimeUnit unit) throws ConnectionPoolTimeoutException, ExecutionException, InterruptedException { final ConnectionRequest connRequest = mgr.requestConnection(route, null); return connRequest.get(timeout, unit); }
/** * Tests that if a socket fails to connect, the allocated connection is * properly released back to the connection manager. */ @Test public void testSocketConnectFailureReleasesConnection() throws Exception { final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class); final ConnectionRequest connrequest = Mockito.mock(ConnectionRequest.class); Mockito.when(connrequest.get( Mockito.anyInt(), Mockito.any(TimeUnit.class))).thenReturn(conn); final HttpClientConnectionManager connmgr = Mockito.mock(HttpClientConnectionManager.class); Mockito.doThrow(new ConnectException()).when(connmgr).connect( Mockito.any(HttpClientConnection.class), Mockito.any(HttpRoute.class), Mockito.anyInt(), Mockito.any(HttpContext.class)); Mockito.when(connmgr.requestConnection( Mockito.any(HttpRoute.class), Mockito.any())).thenReturn(connrequest); final HttpClient client = HttpClients.custom().setConnectionManager(connmgr).build(); final HttpContext context = new BasicHttpContext(); final HttpGet httpget = new HttpGet("http://www.example.com/a"); try { client.execute(httpget, context); Assert.fail("expected IOException"); } catch(final IOException expected) {} Mockito.verify(connmgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS); }
@Override public ConnectionRequest requestConnection(final HttpRoute route, final Object state) { // If this is the redirect route, stub the return value // so-as to pretend the host is waiting on a slot... if(route.getTargetHost().getHostName().equals("localhost")) { final Thread currentThread = Thread.currentThread(); return new ConnectionRequest() { @Override public boolean cancel() { currentThread.interrupt(); return true; } @Override public HttpClientConnection get( final long timeout, final TimeUnit tunit) throws InterruptedException, ConnectionPoolTimeoutException { connLatch.countDown(); // notify waiter that we're getting a connection // zero usually means sleep forever, but CountDownLatch doesn't interpret it that way. if(!awaitLatch.await(timeout > 0 ? timeout : Integer.MAX_VALUE, tunit)) { throw new ConnectionPoolTimeoutException(); } return Mockito.mock(HttpClientConnection.class); } }; } else { return super.requestConnection(route, state); } }
@Override public ConnectionRequest requestConnection( final HttpRoute route, final Object state) { final Thread currentThread = Thread.currentThread(); return new ConnectionRequest() { @Override public boolean cancel() { currentThread.interrupt(); return true; } @Override public HttpClientConnection get( final long timeout, final TimeUnit tunit) throws InterruptedException, ConnectionPoolTimeoutException { connLatch.countDown(); // notify waiter that we're getting a connection // zero usually means sleep forever, but CountDownLatch doesn't interpret it that way. if(!awaitLatch.await(timeout > 0 ? timeout : Integer.MAX_VALUE, tunit)) { throw new ConnectionPoolTimeoutException(); } return Mockito.mock(HttpClientConnection.class); } }; }
@Test public void testLeaseReleaseReusable() throws Exception { final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create(Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); mgr.releaseConnection(conn1, null, 10000, TimeUnit.MILLISECONDS); Assert.assertEquals(route, mgr.getRoute()); Assert.assertEquals(null, mgr.getState()); final ConnectionRequest connRequest2 = mgr.requestConnection(route, null); final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn2); Assert.assertTrue(conn2.isOpen()); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); }
@Test public void testLeaseReleaseReusableWithState() throws Exception { final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create( Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, "some state"); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); mgr.releaseConnection(conn1, "some other state", 10000, TimeUnit.MILLISECONDS); Assert.assertEquals(route, mgr.getRoute()); Assert.assertEquals("some other state", mgr.getState()); final ConnectionRequest connRequest2 = mgr.requestConnection(route, "some other state"); final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn2); Assert.assertTrue(conn2.isOpen()); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); }
@Test public void testLeaseDifferentRoute() throws Exception { final HttpHost target1 = new HttpHost("somehost", 80); final HttpRoute route1 = new HttpRoute(target1); Mockito.when(connFactory.create( Mockito.<HttpRoute>any(), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route1, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route1), Mockito.<ConnectionConfig>any()); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE); mgr.releaseConnection(conn1, null, 0, TimeUnit.MILLISECONDS); Assert.assertEquals(route1, mgr.getRoute()); Assert.assertEquals(null, mgr.getState()); final HttpHost target2 = new HttpHost("otherhost", 80); final HttpRoute route2 = new HttpRoute(target2); final ConnectionRequest connRequest2 = mgr.requestConnection(route2, null); final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn2); Assert.assertFalse(conn2.isOpen()); Mockito.verify(conn).close(); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route1), Mockito.<ConnectionConfig>any()); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route2), Mockito.<ConnectionConfig>any()); }
@Test public void testLeaseExpired() throws Exception { final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create( Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE); mgr.releaseConnection(conn1, null, 10, TimeUnit.MILLISECONDS); Assert.assertEquals(route, mgr.getRoute()); Assert.assertEquals(null, mgr.getState()); Thread.sleep(50); final ConnectionRequest connRequest2 = mgr.requestConnection(route, null); final HttpClientConnection conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn2); Assert.assertFalse(conn2.isOpen()); Mockito.verify(conn).close(); Mockito.verify(connFactory, Mockito.times(2)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); }
@Test public void testShutdown() throws Exception { final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(connFactory.create( Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); Assert.assertNotNull(conn1); Mockito.verify(connFactory, Mockito.times(1)).create( Mockito.eq(route), Mockito.<ConnectionConfig>any()); Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); mgr.releaseConnection(conn1, null, 0, TimeUnit.MILLISECONDS); mgr.shutdown(); Mockito.verify(conn, Mockito.times(1)).shutdown(); try { final ConnectionRequest connRequest2 = mgr.requestConnection(route, null); connRequest2.get(0, TimeUnit.MILLISECONDS); Assert.fail("IllegalStateException expected"); } catch (final IllegalStateException ex) { } // Should have no effect mgr.closeExpiredConnections(); mgr.closeIdleConnections(0L, TimeUnit.MILLISECONDS); mgr.shutdown(); Mockito.verify(conn, Mockito.times(1)).shutdown(); }
@Test public void testLeaseRelease() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); entry.markRouteComplete(); Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); Mockito.when(schemePortResolver.resolve(target)).thenReturn(80); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(conn.isOpen()).thenReturn(true); Mockito.when(future.isCancelled()).thenReturn(false); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); Assert.assertNotSame(conn, conn1); mgr.releaseConnection(conn1, null, 0, TimeUnit.MILLISECONDS); Mockito.verify(pool).release(entry, true); }
@Test public void testReleaseRouteIncomplete() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); Mockito.when(schemePortResolver.resolve(target)).thenReturn(80); Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(conn.isOpen()).thenReturn(true); Mockito.when(future.isCancelled()).thenReturn(false); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); Assert.assertNotSame(conn, conn1); mgr.releaseConnection(conn1, null, 0, TimeUnit.MILLISECONDS); Mockito.verify(pool).release(entry, false); }
@Test(expected=ConnectionPoolTimeoutException.class) public void testLeaseFutureTimeout() throws Exception { final HttpHost target = new HttpHost("localhost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(future.isCancelled()).thenReturn(Boolean.TRUE); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenThrow(new TimeoutException()); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); connRequest1.get(1, TimeUnit.SECONDS); }
@Override public ConnectionRequest requestConnection(HttpRoute route, Object state) { try { onConnect(new URI(route.getTargetHost().toURI())); } catch (IOException | URISyntaxException e) { throw new RuntimeException("onConnect() callback failure: " + e, e); } return super.requestConnection(route, state); }
Handler(ConnectionRequest orig) { this.orig = orig; }
@Override public ConnectionRequest requestConnection(HttpRoute route, Object state) { return null; }
@Test public void wrapOnce() { ConnectionRequest wrapped = ClientConnectionRequestFactory .wrap(noop); assertTrue(wrapped instanceof Wrapped); }