From 160bac0cbb2b388018c4e794c120ba719ddd2887 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 22 Oct 2018 10:35:53 +0200 Subject: [PATCH] Fixed inconsistencies in usage of RequestConfig#connectionTimeout and Request#connectionRequestTimeout --- .../sync/TestConnectionManagement.java | 24 +++++----- .../async/InternalHttp2AsyncExecRuntime.java | 10 ++-- .../async/InternalHttpAsyncExecRuntime.java | 15 +++--- .../impl/async/MinimalHttpAsyncClient.java | 47 ++++++++++++------- .../impl/classic/InternalExecRuntime.java | 18 +++---- .../io/BasicHttpClientConnectionManager.java | 3 +- .../PoolingHttpClientConnectionManager.java | 7 ++- .../hc/client5/http/io/LeaseRequest.java | 8 ++-- .../impl/classic/TestInternalExecRuntime.java | 33 +++++-------- .../TestBasicHttpClientConnectionManager.java | 35 +++++++------- ...estPoolingHttpClientConnectionManager.java | 16 +++---- 11 files changed, 113 insertions(+), 103 deletions(-) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java index bf76397e4..89267c2a1 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java @@ -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()); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2AsyncExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2AsyncExecRuntime.java index fa0545df4..c286acebf 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2AsyncExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2AsyncExecRuntime.java @@ -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() { @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() { + final Timeout connectTimeout = requestConfig.getConnectionTimeout(); + return Operations.cancellable(connPool.getSession(target, connectTimeout, new FutureCallback() { @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() { + final Timeout connectTimeout = requestConfig.getConnectionTimeout(); + connPool.getSession(target, connectTimeout, new FutureCallback() { @Override public void completed(final IOSession ioSession) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java index d4a5aae39..5e1c94d27 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java @@ -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() { @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() { @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); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java index fce1774c6..1d46463bb 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java @@ -134,13 +134,16 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient private Future leaseEndpoint( final HttpHost host, + final Timeout connectionRequestTimeout, final Timeout connectTimeout, final HttpClientContext clientContext, final FutureCallback callback) { final HttpRoute route = new HttpRoute(RoutingSupport.normalize(host, schemePortResolver)); final ComplexFuture resultFuture = new ComplexFuture<>(callback); final Future leaseFuture = connmgr.lease( - route, null, connectTimeout, + route, + null, + connectionRequestTimeout, new FutureCallback() { @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 future = new BasicFuture<>(callback); - leaseEndpoint(host, requestConfig.getConnectionTimeout(), clientContext, new FutureCallback() { + leaseEndpoint( + host, + connectionRequestTimeout, + connectTimeout, + clientContext, + new FutureCallback() { - @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 leaseFuture = leaseEndpoint(target, connectTimeout, clientContext, + final Future leaseFuture = leaseEndpoint( + target, + connectionRequestTimeout, + connectTimeout, + clientContext, new FutureCallback() { @Override diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java index 3897d9bac..02ac4f98e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java @@ -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); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java index e704a755e..5f3c7dae5 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java @@ -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) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java index 5b3b64b9d..faf9a5f3f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java @@ -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 poolEntry; try { - poolEntry = leaseFuture.get(timeout, timeUnit); + poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit()); if (poolEntry == null || leaseFuture.isCancelled()) { throw new InterruptedException(); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/io/LeaseRequest.java b/httpclient5/src/main/java/org/apache/hc/client5/http/io/LeaseRequest.java index dbee508e2..7db5b2a68 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/io/LeaseRequest.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/io/LeaseRequest.java @@ -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; } \ No newline at end of file diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalExecRuntime.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalExecRuntime.java index 63a705909..c7f03c45d 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalExecRuntime.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalExecRuntime.java @@ -89,8 +89,7 @@ public class TestInternalExecRuntime { final HttpRoute route = new HttpRoute(new HttpHost("host", 80)); Mockito.when(mgr.lease(Mockito.eq(route), Mockito.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any()); @@ -110,8 +109,7 @@ public class TestInternalExecRuntime { final HttpClientContext context = HttpClientContext.create(); Mockito.when(mgr.lease(Mockito.eq(route), Mockito.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenThrow(new TimeoutException("timeout")); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenThrow(new ExecutionException(new IllegalStateException())); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.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.any(), Mockito.any())).thenReturn(leaseRequest); - Mockito.when(leaseRequest.get( - Mockito.anyLong(), Mockito.any())).thenReturn(connectionEndpoint); + Mockito.when(leaseRequest.get(Mockito.any())).thenReturn(connectionEndpoint); execRuntime.acquireEndpoint(route, null, context); Assert.assertTrue(execRuntime.isEndpointAcquired()); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java index 9d0257bb3..51ab77bad 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java @@ -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.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.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.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.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.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.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.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.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.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.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.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.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.any()); @@ -309,7 +310,7 @@ public class TestBasicHttpClientConnectionManager { Mockito.when(connFactory.createConnection(Mockito.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.any()); @@ -336,7 +337,7 @@ public class TestBasicHttpClientConnectionManager { Mockito.when(connFactory.createConnection(Mockito.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.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.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(); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestPoolingHttpClientConnectionManager.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestPoolingHttpClientConnectionManager.java index 3f56022ec..fd3bfeded 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestPoolingHttpClientConnectionManager.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestPoolingHttpClientConnectionManager.java @@ -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);