Fixed inconsistencies in usage of RequestConfig#connectionTimeout and Request#connectionRequestTimeout

This commit is contained in:
Oleg Kalnichevski 2018-10-22 10:35:53 +02:00
parent f4bb8fec5b
commit 160bac0cbb
11 changed files with 113 additions and 103 deletions

View File

@ -27,7 +27,6 @@
package org.apache.hc.client5.testing.sync;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.hc.client5.http.HttpRoute;
@ -54,6 +53,7 @@ import org.apache.hc.core5.http.protocol.RequestTargetHost;
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
import org.apache.hc.core5.pool.PoolReusePolicy;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.junit.Assert;
import org.junit.Test;
@ -80,7 +80,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
final HttpContext context = new BasicHttpContext();
final LeaseRequest leaseRequest1 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(Timeout.ZERO_MILLISECONDS);
this.connManager.connect(endpoint1, TimeValue.NEG_ONE_MILLISECONDS, context);
@ -97,7 +97,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
try {
// this should fail quickly, connection has not been released
final LeaseRequest leaseRequest2 = this.connManager.lease(route, null);
leaseRequest2.get(10, TimeUnit.MILLISECONDS);
leaseRequest2.get(Timeout.ofMillis(10));
Assert.fail("TimeoutException expected");
} catch (final TimeoutException ex) {
// expected
@ -106,7 +106,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
endpoint1.close();
this.connManager.release(endpoint1, null, TimeValue.NEG_ONE_MILLISECONDS);
final LeaseRequest leaseRequest2 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint2 = leaseRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint2 = leaseRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertFalse(endpoint2.isConnected());
this.connManager.connect(endpoint2, TimeValue.NEG_ONE_MILLISECONDS, context);
@ -120,7 +120,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
this.connManager.release(endpoint2, null, TimeValue.NEG_ONE_MILLISECONDS);
final LeaseRequest leaseRequest3 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint3 = leaseRequest3.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint3 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS);
Assert.assertTrue(endpoint3.isConnected());
// repeat the communication, no need to prepare the request again
@ -149,7 +149,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
final HttpContext context = new BasicHttpContext();
final LeaseRequest leaseRequest1 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(Timeout.ZERO_MILLISECONDS);
this.connManager.connect(endpoint1, TimeValue.NEG_ONE_MILLISECONDS, context);
final HttpProcessor httpProcessor = new DefaultHttpProcessor(
@ -165,7 +165,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
try {
// this should fail quickly, connection has not been released
final LeaseRequest leaseRequest2 = this.connManager.lease(route, null);
leaseRequest2.get(10, TimeUnit.MILLISECONDS);
leaseRequest2.get(Timeout.ofMillis(10));
Assert.fail("TimeoutException expected");
} catch (final TimeoutException ex) {
// expected
@ -175,7 +175,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
this.connManager.release(endpoint1, null, TimeValue.ofMillis(100));
final LeaseRequest leaseRequest2 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint2 = leaseRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint2 = leaseRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertFalse(endpoint2.isConnected());
this.connManager.connect(endpoint2, TimeValue.NEG_ONE_MILLISECONDS, context);
@ -187,7 +187,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
this.connManager.release(endpoint2, null, TimeValue.ofMillis(100));
final LeaseRequest leaseRequest3 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint3 = leaseRequest3.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint3 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS);
Assert.assertTrue(endpoint3.isConnected());
// repeat the communication, no need to prepare the request again
@ -199,7 +199,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
Thread.sleep(150);
final LeaseRequest leaseRequest4 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint4 = leaseRequest4.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint4 = leaseRequest4.get(Timeout.ZERO_MILLISECONDS);
Assert.assertFalse(endpoint4.isConnected());
// repeat the communication, no need to prepare the request again
@ -222,7 +222,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
final HttpContext context = new BasicHttpContext();
final LeaseRequest leaseRequest1 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(Timeout.ZERO_MILLISECONDS);
this.connManager.connect(endpoint1, TimeValue.NEG_ONE_MILLISECONDS, context);
Assert.assertEquals(1, this.connManager.getTotalStats().getLeased());
@ -271,7 +271,7 @@ public class TestConnectionManagement extends LocalServerTestBase {
final HttpContext context = new BasicHttpContext();
final LeaseRequest leaseRequest1 = this.connManager.lease(route, null);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = leaseRequest1.get(Timeout.ZERO_MILLISECONDS);
this.connManager.connect(endpoint1, TimeValue.NEG_ONE_MILLISECONDS, context);
Assert.assertEquals(1, this.connManager.getTotalStats().getLeased());

View File

@ -49,6 +49,7 @@ import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.reactor.Command;
import org.apache.hc.core5.reactor.IOSession;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
class InternalHttp2AsyncExecRuntime implements AsyncExecRuntime {
@ -84,9 +85,10 @@ class InternalHttp2AsyncExecRuntime implements AsyncExecRuntime {
if (sessionRef.get() == null) {
final HttpHost target = route.getTargetHost();
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
return Operations.cancellable(connPool.getSession(
target,
requestConfig.getConnectionTimeout(),
connectTimeout,
new FutureCallback<IOSession>() {
@Override
@ -167,7 +169,8 @@ class InternalHttp2AsyncExecRuntime implements AsyncExecRuntime {
}
final HttpHost target = endpoint.target;
final RequestConfig requestConfig = context.getRequestConfig();
return Operations.cancellable(connPool.getSession(target, requestConfig.getConnectionTimeout(), new FutureCallback<IOSession>() {
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
return Operations.cancellable(connPool.getSession(target, connectTimeout, new FutureCallback<IOSession>() {
@Override
public void completed(final IOSession ioSession) {
@ -210,7 +213,8 @@ class InternalHttp2AsyncExecRuntime implements AsyncExecRuntime {
} else {
final HttpHost target = endpoint.target;
final RequestConfig requestConfig = context.getRequestConfig();
connPool.getSession(target, requestConfig.getConnectionTimeout(), new FutureCallback<IOSession>() {
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
connPool.getSession(target, connectTimeout, new FutureCallback<IOSession>() {
@Override
public void completed(final IOSession ioSession) {

View File

@ -92,10 +92,11 @@ class InternalHttpAsyncExecRuntime implements AsyncExecRuntime {
if (endpointRef.get() == null) {
state = object;
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout();
return Operations.cancellable(manager.lease(
route,
object,
requestConfig.getConnectionRequestTimeout(),
connectionRequestTimeout,
new FutureCallback<AsyncConnectionEndpoint>() {
@Override
@ -191,20 +192,17 @@ class InternalHttpAsyncExecRuntime implements AsyncExecRuntime {
return Operations.nonCancellable();
}
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout timeout = requestConfig.getConnectionTimeout();
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
return Operations.cancellable(manager.connect(
endpoint,
connectionInitiator,
timeout,
connectTimeout,
versionPolicy,
context,
new FutureCallback<AsyncConnectionEndpoint>() {
@Override
public void completed(final AsyncConnectionEndpoint endpoint) {
if (TimeValue.isPositive(timeout)) {
endpoint.setSocketTimeout(timeout);
}
callback.completed(InternalHttpAsyncExecRuntime.this);
}
@ -225,6 +223,11 @@ class InternalHttpAsyncExecRuntime implements AsyncExecRuntime {
@Override
public void upgradeTls(final HttpClientContext context) {
final AsyncConnectionEndpoint endpoint = ensureValid();
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
if (TimeValue.isPositive(connectTimeout)) {
endpoint.setSocketTimeout(connectTimeout);
}
manager.upgrade(endpoint, versionPolicy, context);
}

View File

@ -134,13 +134,16 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
private Future<AsyncConnectionEndpoint> leaseEndpoint(
final HttpHost host,
final Timeout connectionRequestTimeout,
final Timeout connectTimeout,
final HttpClientContext clientContext,
final FutureCallback<AsyncConnectionEndpoint> callback) {
final HttpRoute route = new HttpRoute(RoutingSupport.normalize(host, schemePortResolver));
final ComplexFuture<AsyncConnectionEndpoint> resultFuture = new ComplexFuture<>(callback);
final Future<AsyncConnectionEndpoint> leaseFuture = connmgr.lease(
route, null, connectTimeout,
route,
null,
connectionRequestTimeout,
new FutureCallback<AsyncConnectionEndpoint>() {
@Override
@ -206,25 +209,32 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
ensureRunning();
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig requestConfig = clientContext.getRequestConfig();
final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout();
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
final BasicFuture<AsyncClientEndpoint> future = new BasicFuture<>(callback);
leaseEndpoint(host, requestConfig.getConnectionTimeout(), clientContext, new FutureCallback<AsyncConnectionEndpoint>() {
leaseEndpoint(
host,
connectionRequestTimeout,
connectTimeout,
clientContext,
new FutureCallback<AsyncConnectionEndpoint>() {
@Override
public void completed(final AsyncConnectionEndpoint result) {
future.completed(new InternalAsyncClientEndpoint(result));
}
@Override
public void completed(final AsyncConnectionEndpoint result) {
future.completed(new InternalAsyncClientEndpoint(result));
}
@Override
public void failed(final Exception ex) {
future.failed(ex);
}
@Override
public void failed(final Exception ex) {
future.failed(ex);
}
@Override
public void cancelled() {
future.cancel(true);
}
@Override
public void cancelled() {
future.cancel(true);
}
});
});
return future;
}
@ -253,10 +263,15 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
} else {
requestConfig = clientContext.getRequestConfig();
}
final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout();
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
final HttpHost target = new HttpHost(request.getAuthority(), request.getScheme());
final Future<AsyncConnectionEndpoint> leaseFuture = leaseEndpoint(target, connectTimeout, clientContext,
final Future<AsyncConnectionEndpoint> leaseFuture = leaseEndpoint(
target,
connectionRequestTimeout,
connectTimeout,
clientContext,
new FutureCallback<AsyncConnectionEndpoint>() {
@Override

View File

@ -94,8 +94,8 @@ class InternalExecRuntime implements ExecRuntime, Cancellable {
Args.notNull(route, "Route");
if (endpointRef.get() == null) {
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout requestTimeout = requestConfig.getConnectionRequestTimeout();
final LeaseRequest connRequest = manager.lease(route, requestTimeout, object);
final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout();
final LeaseRequest connRequest = manager.lease(route, connectionRequestTimeout, object);
state = object;
if (cancellableDependency != null) {
if (cancellableDependency.isCancelled()) {
@ -105,7 +105,7 @@ class InternalExecRuntime implements ExecRuntime, Cancellable {
cancellableDependency.setDependency(connRequest);
}
try {
final ConnectionEndpoint connectionEndpoint = connRequest.get(requestTimeout.getDuration(), requestTimeout.getTimeUnit());
final ConnectionEndpoint connectionEndpoint = connRequest.get(connectionRequestTimeout);
endpointRef.set(connectionEndpoint);
reusable = connectionEndpoint.isConnected();
if (cancellableDependency != null) {
@ -149,11 +149,8 @@ class InternalExecRuntime implements ExecRuntime, Cancellable {
}
}
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout timeout = requestConfig.getConnectionTimeout();
manager.connect(endpoint, timeout, context);
if (TimeValue.isPositive(timeout)) {
endpoint.setSocketTimeout(timeout);
}
final Timeout connectTimeout = requestConfig.getConnectionTimeout();
manager.connect(endpoint, connectTimeout, context);
}
@Override
@ -176,6 +173,11 @@ class InternalExecRuntime implements ExecRuntime, Cancellable {
@Override
public void upgradeTls(final HttpClientContext context) throws IOException {
final ConnectionEndpoint endpoint = ensureValid();
final RequestConfig requestConfig = context.getRequestConfig();
final Timeout timeout = requestConfig.getConnectionTimeout();
if (TimeValue.isPositive(timeout)) {
endpoint.setSocketTimeout(timeout);
}
manager.upgrade(endpoint, context);
}

View File

@ -189,8 +189,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
@Override
public ConnectionEndpoint get(
final long timeout,
final TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
final Timeout timeout) throws InterruptedException, ExecutionException, TimeoutException {
try {
return new InternalConnectionEndpoint(route, getConnection(route, state));
} catch (final IOException ex) {

View File

@ -30,7 +30,6 @@ import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -264,14 +263,14 @@ public class PoolingHttpClientConnectionManager
@Override
public synchronized ConnectionEndpoint get(
final long timeout,
final TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
final Timeout timeout) throws InterruptedException, ExecutionException, TimeoutException {
Args.notNull(timeout, "Operation timeout");
if (this.endpoint != null) {
return this.endpoint;
}
final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry;
try {
poolEntry = leaseFuture.get(timeout, timeUnit);
poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit());
if (poolEntry == null || leaseFuture.isCancelled()) {
throw new InterruptedException();
}

View File

@ -28,10 +28,10 @@
package org.apache.hc.client5.http.io;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.hc.core5.concurrent.Cancellable;
import org.apache.hc.core5.util.Timeout;
/**
* Represents a request for a {@link ConnectionEndpoint} whose life cycle
@ -51,9 +51,7 @@ public interface LeaseRequest extends Cancellable {
* before this began, an {@link InterruptedException} will
* be thrown.
*
* @param timeout the timeout, 0 or negative for no timeout
* @param timeUnit the unit for the {@code timeout},
* may be {@code null} only if there is no timeout
* @param timeout the operation timeout.
*
* @return a connection that can be used to communicate
* along the given route
@ -63,7 +61,7 @@ public interface LeaseRequest extends Cancellable {
* @throws InterruptedException
* if the calling thread is interrupted while waiting
*/
ConnectionEndpoint get(long timeout, TimeUnit timeUnit)
ConnectionEndpoint get(Timeout timeout)
throws InterruptedException, ExecutionException, TimeoutException;
}

View File

@ -89,8 +89,7 @@ public class TestInternalExecRuntime {
final HttpRoute route = new HttpRoute(new HttpHost("host", 80));
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
@ -99,7 +98,7 @@ public class TestInternalExecRuntime {
Assert.assertFalse(execRuntime.isEndpointConnected());
Assert.assertFalse(execRuntime.isConnectionReusable());
Mockito.verify(leaseRequest).get(345, TimeUnit.MILLISECONDS);
Mockito.verify(leaseRequest).get(Timeout.ofMillis(345));
Mockito.verify(cancellableDependency, Mockito.times(1)).setDependency(leaseRequest);
Mockito.verify(cancellableDependency, Mockito.times(1)).setDependency(execRuntime);
Mockito.verify(cancellableDependency, Mockito.times(2)).setDependency(Mockito.<Cancellable>any());
@ -110,8 +109,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
@ -126,8 +124,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenThrow(new TimeoutException("timeout"));
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenThrow(new TimeoutException("timeout"));
execRuntime.acquireEndpoint(route, null, context);
}
@ -137,8 +134,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenThrow(new ExecutionException(new IllegalStateException()));
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenThrow(new ExecutionException(new IllegalStateException()));
execRuntime.acquireEndpoint(route, null, context);
}
@ -147,8 +143,7 @@ public class TestInternalExecRuntime {
public void testAbortEndpoint() throws Exception {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(new HttpRoute(new HttpHost("host", 80)), null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());
@ -173,8 +168,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());
@ -200,8 +194,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());
@ -228,8 +221,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());
@ -262,8 +254,7 @@ public class TestInternalExecRuntime {
context.setRequestConfig(config);
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());
@ -274,7 +265,6 @@ public class TestInternalExecRuntime {
execRuntime.connectEndpoint(context);
Mockito.verify(mgr).connect(connectionEndpoint, Timeout.ofMillis(123), context);
Mockito.verify(connectionEndpoint).setSocketTimeout(Timeout.ofMillis(123));
}
@Test
@ -282,8 +272,7 @@ public class TestInternalExecRuntime {
final HttpClientContext context = HttpClientContext.create();
Mockito.when(mgr.lease(Mockito.eq(route), Mockito.<Timeout>any(), Mockito.any())).thenReturn(leaseRequest);
Mockito.when(leaseRequest.get(
Mockito.anyLong(), Mockito.<TimeUnit>any())).thenReturn(connectionEndpoint);
Mockito.when(leaseRequest.get(Mockito.<Timeout>any())).thenReturn(connectionEndpoint);
execRuntime.acquireEndpoint(route, null, context);
Assert.assertTrue(execRuntime.isEndpointAcquired());

View File

@ -48,6 +48,7 @@ import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -92,7 +93,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Assert.assertFalse(endpoint1.isConnected());
@ -102,7 +103,7 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertNull(mgr.getState());
final LeaseRequest connRequest2 = mgr.lease(route, null);
final ConnectionEndpoint conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint conn2 = connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(conn2);
Assert.assertFalse(conn2.isConnected());
@ -117,7 +118,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -130,7 +131,7 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertEquals(null, mgr.getState());
final LeaseRequest connRequest2 = mgr.lease(route, null);
final ConnectionEndpoint conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint conn2 = connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(conn2);
Assert.assertTrue(conn2.isConnected());
@ -145,7 +146,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, "some state");
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -158,7 +159,7 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertEquals("some other state", mgr.getState());
final LeaseRequest connRequest2 = mgr.lease(route, "some other state");
final ConnectionEndpoint conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint conn2 = connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(conn2);
Assert.assertTrue(conn2.isConnected());
@ -173,7 +174,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route1, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -188,7 +189,7 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target2 = new HttpHost("otherhost", 80);
final HttpRoute route2 = new HttpRoute(target2);
final LeaseRequest connRequest2 = mgr.lease(route2, null);
final ConnectionEndpoint conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint conn2 = connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(conn2);
Assert.assertFalse(conn2.isConnected());
@ -204,7 +205,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -219,7 +220,7 @@ public class TestBasicHttpClientConnectionManager {
Thread.sleep(50);
final LeaseRequest connRequest2 = mgr.lease(route, null);
final ConnectionEndpoint conn2 = connRequest2.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint conn2 = connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(conn2);
Assert.assertFalse(conn2.isConnected());
@ -246,7 +247,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -261,7 +262,7 @@ public class TestBasicHttpClientConnectionManager {
try {
final LeaseRequest connRequest2 = mgr.lease(route, null);
connRequest2.get(0, TimeUnit.MILLISECONDS);
connRequest2.get(Timeout.ZERO_MILLISECONDS);
Assert.fail("IllegalStateException expected");
} catch (final IllegalStateException ex) {
}
@ -282,7 +283,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -309,7 +310,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
Mockito.verify(connFactory, Mockito.times(1)).createConnection(Mockito.<Socket>any());
@ -336,7 +337,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
mgr.release(endpoint1, null, TimeValue.ofMillis(100));
@ -354,7 +355,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
final HttpClientContext context = HttpClientContext.create();
@ -395,7 +396,7 @@ public class TestBasicHttpClientConnectionManager {
Mockito.when(connFactory.createConnection(Mockito.<Socket>any())).thenReturn(conn);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ZERO_MILLISECONDS);
Assert.assertNotNull(endpoint1);
final HttpClientContext context = HttpClientContext.create();

View File

@ -116,7 +116,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
Assert.assertNotSame(conn, endpoint1);
@ -148,7 +148,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
Assert.assertNotSame(conn, endpoint1);
@ -175,7 +175,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
connRequest1.get(1, TimeUnit.SECONDS);
connRequest1.get(Timeout.ofSeconds(1));
}
@Test(expected=TimeoutException.class)
@ -193,7 +193,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
connRequest1.get(1, TimeUnit.SECONDS);
connRequest1.get(Timeout.ofSeconds(1));
}
@Test
@ -215,7 +215,7 @@ public class TestPoolingHttpClientConnectionManager {
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
Assert.assertTrue(endpoint1.isConnected());
@ -244,7 +244,7 @@ public class TestPoolingHttpClientConnectionManager {
Mockito.when(conn.isOpen()).thenReturn(Boolean.FALSE);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
Assert.assertFalse(endpoint1.isConnected());
@ -276,7 +276,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
final HttpClientContext context = HttpClientContext.create();
@ -329,7 +329,7 @@ public class TestPoolingHttpClientConnectionManager {
.thenReturn(future);
final LeaseRequest connRequest1 = mgr.lease(route, null);
final ConnectionEndpoint endpoint1 = connRequest1.get(1, TimeUnit.SECONDS);
final ConnectionEndpoint endpoint1 = connRequest1.get(Timeout.ofSeconds(1));
Assert.assertNotNull(endpoint1);
final ConnectionSocketFactory plainsf = Mockito.mock(ConnectionSocketFactory.class);