Java 类org.apache.http.conn.ConnectionRequest 实例源码

项目:remote-files-sync    文件:BasicHttpClientConnectionManager.java   
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);
        }

    };
}
项目:remote-files-sync    文件:PoolingHttpClientConnectionManager.java   
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);
        }

    };

}
项目:purecloud-iot    文件:BasicHttpClientConnectionManager.java   
@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);
        }

    };
}
项目:purecloud-iot    文件:PoolingHttpClientConnectionManager.java   
@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);
        }

    };

}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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());
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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);
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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);
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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));
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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));
}
项目:Visit    文件:BasicHttpClientConnectionManager.java   
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);
        }

    };
}
项目:Visit    文件:PoolingHttpClientConnectionManager.java   
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);
        }

    };

}
项目:ZTLib    文件:BasicHttpClientConnectionManager.java   
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);
        }

    };
}
项目:ZTLib    文件:PoolingHttpClientConnectionManager.java   
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);
        }

    };

}
项目:aws-sdk-java-v2    文件:ClientConnectionRequestFactory.java   
/**
 * 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));
}
项目:aws-sdk-java-v2    文件:ClientConnectionManagerFactory.java   
@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();
    }
}
项目:ibm-cos-sdk-java    文件:ClientConnectionRequestFactory.java   
/**
 * 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));
}
项目:ibm-cos-sdk-java    文件:ClientConnectionManagerFactory.java   
@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();
    }
}
项目:yarpc-java    文件:HttpConnectionReuseTest.java   
@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());
}
项目:purecloud-iot    文件:TestConnectionAutoRelease.java   
@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);
}
项目:purecloud-iot    文件:TestConnectionAutoRelease.java   
@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);
}
项目:purecloud-iot    文件:TestConnectionAutoRelease.java   
@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);
}
项目:purecloud-iot    文件:TestConnectionManagement.java   
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);
}
项目:purecloud-iot    文件:TestAbortHandling.java   
/**
 * 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);
}
项目:purecloud-iot    文件:TestAbortHandling.java   
@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);
    }
}
项目:purecloud-iot    文件:TestAbortHandling.java   
@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);
        }

    };
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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());
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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());
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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());
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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());
}
项目:purecloud-iot    文件:TestBasicHttpClientConnectionManager.java   
@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();
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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);
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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);
}
项目:purecloud-iot    文件:TestPoolingHttpClientConnectionManager.java   
@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);
}
项目:incubator-gobblin    文件:AbstractHttpWriter.java   
@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);
}
项目:aws-sdk-java-v2    文件:ClientConnectionRequestFactory.java   
Handler(ConnectionRequest orig) {
    this.orig = orig;
}
项目:aws-sdk-java-v2    文件:ClientConnectionManagerFactoryTest.java   
@Override
public ConnectionRequest requestConnection(HttpRoute route,
                                           Object state) {
    return null;
}
项目:ibm-cos-sdk-java    文件:ClientConnectionRequestFactory.java   
Handler(ConnectionRequest orig) {
    this.orig = orig;
}
项目:ibm-cos-sdk-java    文件:IdleConnectionReaperTest.java   
@Override
public ConnectionRequest requestConnection(HttpRoute route, Object state) {
    return null;
}
项目:ibm-cos-sdk-java    文件:ClientConnectionManagerFactoryTest.java   
@Override
public ConnectionRequest requestConnection(HttpRoute route,
                                           Object state) {
    return null;
}
项目:ibm-cos-sdk-java    文件:ClientConnectionRequestFactoryTest.java   
@Test
public void wrapOnce() {
    ConnectionRequest wrapped = ClientConnectionRequestFactory
            .wrap(noop);
    assertTrue(wrapped instanceof Wrapped);
}