You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/04/01 17:11:51 UTC
svn commit: r1789825 - in /httpcomponents/httpcore/trunk:
httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/
httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/
httpcore5-testing/src/test/java/org/apache/hc/core5/te...
Author: olegk
Date: Sat Apr 1 17:11:51 2017
New Revision: 1789825
URL: http://svn.apache.org/viewvc?rev=1789825&view=rev
Log:
Classic test client to use connection pooling; classic test client and server improvements
Modified:
httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java
httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java
httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java
httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java Sat Apr 1 17:11:51 2017
@@ -28,63 +28,68 @@
package org.apache.hc.core5.testing.classic;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.net.Socket;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.net.ssl.SSLContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.config.H1Config;
+import org.apache.hc.core5.http.config.SocketConfig;
import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
import org.apache.hc.core5.http.impl.bootstrap.RequesterBootstrap;
-import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
import org.apache.hc.core5.http.io.HttpConnectionFactory;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.io.ShutdownType;
import org.apache.hc.core5.net.URIAuthority;
-import org.apache.hc.core5.util.Asserts;
-import org.apache.hc.core5.util.LangUtils;
public class ClassicTestClient {
- private final DefaultBHttpClientConnection connection;
- private volatile HttpProcessor httpProcessor;
- private volatile int timeout;
- private volatile HttpHost host;
+ private final SSLContext sslContext;
+ private final SocketConfig socketConfig;
- private volatile HttpRequester requester;
+ private final AtomicReference<HttpRequester> requesterRef;
- public ClassicTestClient() {
+ public ClassicTestClient(final SSLContext sslContext, final SocketConfig socketConfig) {
super();
- this.connection = new LoggingBHttpClientConnection(H1Config.DEFAULT);
- }
-
- public void setHttpProcessor(final HttpProcessor httpProcessor) {
- this.httpProcessor = httpProcessor;
+ this.sslContext = sslContext;
+ this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
+ this.requesterRef = new AtomicReference<>(null);
}
- public int getTimeout() {
- return this.timeout;
+ public ClassicTestClient(final SocketConfig socketConfig) {
+ this(null, socketConfig);
}
- public void setTimeout(final int timeout) {
- this.timeout = timeout;
+ public ClassicTestClient() {
+ this(null, null);
}
public void start() {
- Asserts.check(this.requester == null, "Client already running");
- this.requester = RequesterBootstrap.bootstrap()
- .setHttpProcessor(httpProcessor)
- .setConnectFactory(new LoggingConnFactory())
- .create();
+ start(null);
+ }
+ public void start(final HttpProcessor httpProcessor) {
+ if (requesterRef.get() == null) {
+ final HttpRequester requester = RequesterBootstrap.bootstrap()
+ .setSslSocketFactory(sslContext != null ? sslContext.getSocketFactory() : null)
+ .setHttpProcessor(httpProcessor)
+ .setConnectFactory(new LoggingConnFactory())
+ .create();
+ requesterRef.compareAndSet(null, requester);
+ } else {
+ throw new IllegalStateException("Requester has already been started");
+ }
}
- public void shutdown() {
- try {
- this.connection.close();
- } catch (final IOException ignore) {
+ public void shutdown(final ShutdownType shutdownType) {
+ final HttpRequester requester = requesterRef.getAndSet(null);
+ if (requester != null) {
+ requester.shutdown(shutdownType);
}
}
@@ -92,29 +97,15 @@ public class ClassicTestClient {
final HttpHost targetHost,
final ClassicHttpRequest request,
final HttpContext context) throws HttpException, IOException {
- Asserts.check(this.requester != null, "Client not running");
- if (LangUtils.equals(this.host, targetHost)) {
- this.connection.close();
- }
- this.host = targetHost;
- if (!this.connection.isOpen()) {
- final Socket socket = new Socket();
- socket.connect(new InetSocketAddress(this.host.getHostName(), this.host.getPort()), this.timeout);
- this.connection.bind(socket);
- this.connection.setSocketTimeout(this.timeout);
+ final HttpRequester requester = this.requesterRef.get();
+ if (requester == null) {
+ throw new IllegalStateException("Requester has not been started");
}
if (request.getAuthority() == null) {
request.setAuthority(new URIAuthority(targetHost));
}
request.setScheme(targetHost.getSchemeName());
- return this.requester.execute(this.connection, request, context);
- }
-
- public boolean keepAlive(
- final ClassicHttpRequest request,
- final ClassicHttpResponse response,
- final HttpContext context) throws IOException {
- return this.requester.keepAlive(this.connection, request, response, context);
+ return requester.execute(targetHost, request, socketConfig, context);
}
class LoggingConnFactory implements HttpConnectionFactory<LoggingBHttpClientConnection> {
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java Sat Apr 1 17:11:51 2017
@@ -31,7 +31,9 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.net.ssl.SSLContext;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.ExceptionListener;
@@ -43,88 +45,89 @@ import org.apache.hc.core5.http.io.HttpC
import org.apache.hc.core5.http.io.HttpExpectationVerifier;
import org.apache.hc.core5.http.io.HttpRequestHandler;
import org.apache.hc.core5.http.io.UriHttpRequestHandlerMapper;
+import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.io.ShutdownType;
-import org.apache.hc.core5.util.Asserts;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ClassicTestServer {
- private final UriHttpRequestHandlerMapper reqistry;
- private volatile HttpExpectationVerifier expectationVerifier;
- private volatile int timeout;
+ private final SSLContext sslContext;
+ private final SocketConfig socketConfig;
+ private final UriHttpRequestHandlerMapper registry;
- private volatile HttpServer server;
+ private final AtomicReference<HttpServer> serverRef;
- public ClassicTestServer() throws IOException {
+ public ClassicTestServer(final SSLContext sslContext, final SocketConfig socketConfig) {
super();
- this.reqistry = new UriHttpRequestHandlerMapper();
+ this.sslContext = sslContext;
+ this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
+ this.registry = new UriHttpRequestHandlerMapper();
+ this.serverRef = new AtomicReference<>(null);
}
- public int getTimeout() {
- return this.timeout;
+ public ClassicTestServer(final SocketConfig socketConfig) {
+ this(null, socketConfig);
}
- public void setTimeout(final int timeout) {
- this.timeout = timeout;
+ public ClassicTestServer() {
+ this(null, null);
}
public void registerHandler(
final String pattern,
final HttpRequestHandler handler) {
- this.reqistry.register(pattern, handler);
- }
-
- public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
- this.expectationVerifier = expectationVerifier;
+ this.registry.register(pattern, handler);
}
public int getPort() {
- final HttpServer local = this.server;
- if (local != null) {
- return this.server.getLocalPort();
+ final HttpServer server = this.serverRef.get();
+ if (server != null) {
+ return server.getLocalPort();
} else {
throw new IllegalStateException("Server not running");
}
}
public InetAddress getInetAddress() {
- final HttpServer local = this.server;
- if (local != null) {
- return local.getInetAddress();
+ final HttpServer server = this.serverRef.get();
+ if (server != null) {
+ return server.getInetAddress();
} else {
throw new IllegalStateException("Server not running");
}
}
- public void start() throws IOException {
- Asserts.check(this.server == null, "Server already running");
- this.server = ServerBootstrap.bootstrap()
- .setSocketConfig(SocketConfig.custom()
- .setSoTimeout(this.timeout, TimeUnit.MILLISECONDS)
- .build())
- .setConnectionFactory(new LoggingConnFactory())
- .setExceptionListener(new SimpleExceptionListener())
- .setExpectationVerifier(this.expectationVerifier)
- .setHandlerMapper(this.reqistry)
- .create();
- this.server.start();
- }
-
- public void shutdown() {
- shutdown(5, TimeUnit.SECONDS);
- }
-
- public void shutdown(final long gracePeriod, final TimeUnit timeUnit) {
- final HttpServer local = this.server;
- this.server = null;
- if (local != null) {
- local.initiateShutdown();
- try {
- local.awaitTermination(gracePeriod, timeUnit);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
+ public void start(final HttpProcessor httpProcessor, final HttpExpectationVerifier expectationVerifier) throws IOException {
+ if (serverRef.get() == null) {
+ final HttpServer server = ServerBootstrap.bootstrap()
+ .setSocketConfig(socketConfig)
+ .setSslContext(sslContext)
+ .setHttpProcessor(httpProcessor)
+ .setExpectationVerifier(expectationVerifier)
+ .setHandlerMapper(this.registry)
+ .setConnectionFactory(new LoggingConnFactory())
+ .setExceptionListener(new SimpleExceptionListener())
+ .create();
+ if (serverRef.compareAndSet(null, server)) {
+ server.start();
}
- local.shutdown(ShutdownType.IMMEDIATE);
+ } else {
+ throw new IllegalStateException("Server already running");
+ }
+ }
+
+ public void start(final HttpExpectationVerifier expectationVerifier) throws IOException {
+ start(null, expectationVerifier);
+ }
+
+ public void start() throws IOException {
+ start(null, null);
+ }
+
+ public void shutdown(final ShutdownType shutdownType) {
+ final HttpServer server = serverRef.getAndSet(null);
+ if (server != null) {
+ server.shutdown(shutdownType);
}
}
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java Sat Apr 1 17:11:51 2017
@@ -31,14 +31,16 @@ import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.concurrent.TimeUnit;
import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.config.SocketConfig;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
@@ -66,7 +68,17 @@ public class ClassicTestClientAdapter ex
throw new HttpException("Request method should be set.");
}
- final ClassicTestClient client = new ClassicTestClient();
+ final SocketConfig socketConfig;
+ if (request.containsKey(TIMEOUT)) {
+ final long timeout = (long) request.get(TIMEOUT);
+ socketConfig = SocketConfig.custom()
+ .setConnectTimeout((int) timeout, TimeUnit.MILLISECONDS)
+ .setSoTimeout((int) timeout, TimeUnit.MILLISECONDS)
+ .build();
+ } else {
+ socketConfig = null;
+ }
+ final ClassicTestClient client = new ClassicTestClient(socketConfig);
// Append the path to the defaultURI.
String tempDefaultURI = defaultURI;
@@ -125,39 +137,32 @@ public class ClassicTestClientAdapter ex
httpRequest.setEntity(entity);
}
- // timeout
- if (request.containsKey(TIMEOUT)) {
- final long timeout = (long) request.get(TIMEOUT);
- client.setTimeout((int) timeout);
- }
- client.start();
+ client.start(null);
// Now start the request.
final HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
final HttpCoreContext context = HttpCoreContext.create();
- final ClassicHttpResponse response = client.execute(host, httpRequest, context);
-
- // Prepare the response. It will contain status, body, headers, and contentType.
- final HttpEntity entity = response.getEntity();
- final String body = entity == null ? null : EntityUtils.toString(entity);
- final String contentType = entity == null ? null : entity.getContentType();
-
- client.keepAlive(httpRequest, response, context);
-
- // prepare the returned information
- final Map<String, Object> ret = new HashMap<String, Object>();
- ret.put(STATUS, response.getCode());
+ try (final ClassicHttpResponse response = client.execute(host, httpRequest, context)) {
+ // Prepare the response. It will contain status, body, headers, and contentType.
+ final HttpEntity entity = response.getEntity();
+ final String body = entity == null ? null : EntityUtils.toString(entity);
+ final String contentType = entity == null ? null : entity.getContentType();
+
+ // prepare the returned information
+ final Map<String, Object> ret = new HashMap<String, Object>();
+ ret.put(STATUS, response.getCode());
+
+ // convert the headers to a Map
+ final Map<String, Object> headerMap = new HashMap<String, Object>();
+ for (final Header header : response.getAllHeaders()) {
+ headerMap.put(header.getName(), header.getValue());
+ }
+ ret.put(HEADERS, headerMap);
+ ret.put(BODY, body);
+ ret.put(CONTENT_TYPE, contentType);
- // convert the headers to a Map
- final Map<String, Object> headerMap = new HashMap<String, Object>();
- for (final Header header : response.getAllHeaders()) {
- headerMap.put(header.getName(), header.getValue());
+ return ret ;
}
- ret.put(HEADERS, headerMap);
- ret.put(BODY, body);
- ret.put(CONTENT_TYPE, contentType);
-
- return ret ;
}
/**
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java Sat Apr 1 17:11:51 2017
@@ -52,6 +52,7 @@ import org.apache.hc.core5.http.HttpRequ
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
+import org.apache.hc.core5.http.config.SocketConfig;
import org.apache.hc.core5.http.io.HttpExpectationVerifier;
import org.apache.hc.core5.http.io.HttpRequestHandler;
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
@@ -67,6 +68,7 @@ import org.apache.hc.core5.http.protocol
import org.apache.hc.core5.http.protocol.RequestExpectContinue;
import org.apache.hc.core5.http.protocol.RequestTargetHost;
import org.apache.hc.core5.http.protocol.RequestUserAgent;
+import org.apache.hc.core5.io.ShutdownType;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@@ -81,15 +83,16 @@ public class TestClassicHttp {
@Override
protected void before() throws Throwable {
- server = new ClassicTestServer();
- server.setTimeout(5000);
+ server = new ClassicTestServer(SocketConfig.custom()
+ .setConnectTimeout(5, TimeUnit.SECONDS)
+ .setSoTimeout(5, TimeUnit.SECONDS).build());
}
@Override
protected void after() {
if (server != null) {
try {
- server.shutdown(3, TimeUnit.SECONDS);
+ server.shutdown(ShutdownType.IMMEDIATE);
server = null;
} catch (final Exception ignore) {
}
@@ -105,15 +108,16 @@ public class TestClassicHttp {
@Override
protected void before() throws Throwable {
- client = new ClassicTestClient();
- client.setTimeout(5000);
+ client = new ClassicTestClient(SocketConfig.custom()
+ .setConnectTimeout(5, TimeUnit.SECONDS)
+ .setSoTimeout(5, TimeUnit.SECONDS).build());
}
@Override
protected void after() {
if (client != null) {
try {
- client.shutdown();
+ client.shutdown(ShutdownType.IMMEDIATE);
client = null;
} catch (final Exception ignore) {
}
@@ -170,15 +174,15 @@ public class TestClassicHttp {
for (int r = 0; r < reqNo; r++) {
final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
- final ClassicHttpResponse response = this.client.execute(host, get, context);
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- final byte[] expected = testData.get(r);
-
- Assert.assertEquals(expected.length, received.length);
- for (int i = 0; i < expected.length; i++) {
- Assert.assertEquals(expected[i], received[i]);
+ try (final ClassicHttpResponse response = this.client.execute(host, get, context)) {
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] expected = testData.get(r);
+
+ Assert.assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i], received[i]);
+ }
}
- this.client.keepAlive(get, response, context);
}
}
@@ -235,15 +239,15 @@ public class TestClassicHttp {
final ByteArrayEntity outgoing = new ByteArrayEntity(data);
post.setEntity(outgoing);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- final byte[] expected = testData.get(r);
-
- Assert.assertEquals(expected.length, received.length);
- for (int i = 0; i < expected.length; i++) {
- Assert.assertEquals(expected[i], received[i]);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] expected = testData.get(r);
+
+ Assert.assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i], received[i]);
+ }
}
- this.client.keepAlive(post, response, context);
}
}
@@ -300,15 +304,15 @@ public class TestClassicHttp {
outgoing.setChunked(true);
post.setEntity(outgoing);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- final byte[] expected = testData.get(r);
-
- Assert.assertEquals(expected.length, received.length);
- for (int i = 0; i < expected.length; i++) {
- Assert.assertEquals(expected[i], received[i]);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] expected = testData.get(r);
+
+ Assert.assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i], received[i]);
+ }
}
- this.client.keepAlive(post, response, context);
}
}
@@ -368,19 +372,19 @@ public class TestClassicHttp {
final ByteArrayEntity outgoing = new ByteArrayEntity(data);
post.setEntity(outgoing);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- Assert.assertEquals(HttpVersion.HTTP_1_1, response.getVersion());
- final Header h1 = response.getFirstHeader("Version");
- Assert.assertNotNull(h1);
- Assert.assertEquals("1.0", h1.getValue());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- final byte[] expected = testData.get(r);
-
- Assert.assertEquals(expected.length, received.length);
- for (int i = 0; i < expected.length; i++) {
- Assert.assertEquals(expected[i], received[i]);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ Assert.assertEquals(HttpVersion.HTTP_1_1, response.getVersion());
+ final Header h1 = response.getFirstHeader("Version");
+ Assert.assertNotNull(h1);
+ Assert.assertEquals("1.0", h1.getValue());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] expected = testData.get(r);
+
+ Assert.assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i], received[i]);
+ }
}
- this.client.keepAlive(post, response, context);
}
}
@@ -438,15 +442,15 @@ public class TestClassicHttp {
outgoing.setChunked(true);
post.setEntity(outgoing);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- final byte[] expected = testData.get(r);
-
- Assert.assertEquals(expected.length, received.length);
- for (int i = 0; i < expected.length; i++) {
- Assert.assertEquals(expected[i], received[i]);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] expected = testData.get(r);
+
+ Assert.assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i], received[i]);
+ }
}
- this.client.keepAlive(post, response, context);
}
}
@@ -475,7 +479,7 @@ public class TestClassicHttp {
});
- this.server.setExpectationVerifier(new HttpExpectationVerifier() {
+ this.server.start(new HttpExpectationVerifier() {
@Override
public void verify(
@@ -500,8 +504,6 @@ public class TestClassicHttp {
}
});
-
- this.server.start();
this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
@@ -519,18 +521,17 @@ public class TestClassicHttp {
requestEntity.setChunked(false);
post.setEntity(requestEntity);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
-
- final HttpEntity responseEntity = response.getEntity();
- Assert.assertNotNull(responseEntity);
- EntityUtils.consume(responseEntity);
-
- if (r >= 2) {
- Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
- } else {
- Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ final HttpEntity responseEntity = response.getEntity();
+ Assert.assertNotNull(responseEntity);
+ EntityUtils.consume(responseEntity);
+
+ if (r >= 2) {
+ Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
+ } else {
+ Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+ }
}
- this.client.keepAlive(post, response, context);
}
}
@@ -660,26 +661,26 @@ public class TestClassicHttp {
outgoing.setChunked(n % 2 == 0);
post.setEntity(outgoing);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- final HttpEntity incoming = response.getEntity();
- Assert.assertNotNull(incoming);
- final InputStream instream = incoming.getContent();
- final ContentType contentType = EntityUtils.getContentTypeOrDefault(incoming);
- Charset charset = contentType.getCharset();
- if (charset == null) {
- charset = StandardCharsets.ISO_8859_1;
- }
- Assert.assertNotNull(instream);
- final BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
-
- String line;
- int count = 0;
- while ((line = reader.readLine()) != null) {
- Assert.assertEquals(pattern, line);
- count++;
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ final HttpEntity incoming = response.getEntity();
+ Assert.assertNotNull(incoming);
+ final InputStream instream = incoming.getContent();
+ final ContentType contentType = EntityUtils.getContentTypeOrDefault(incoming);
+ Charset charset = contentType.getCharset();
+ if (charset == null) {
+ charset = StandardCharsets.ISO_8859_1;
+ }
+ Assert.assertNotNull(instream);
+ final BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
+
+ String line;
+ int count = 0;
+ while ((line = reader.readLine()) != null) {
+ Assert.assertEquals(pattern, line);
+ count++;
+ }
+ Assert.assertEquals(n, count);
}
- Assert.assertEquals(n, count);
- this.client.keepAlive(post, response, context);
}
}
}
@@ -713,11 +714,11 @@ public class TestClassicHttp {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
post.setEntity(null);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- Assert.assertEquals(0, received.length);
- this.client.keepAlive(post, response, context);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ Assert.assertEquals(0, received.length);
+ }
}
@Test
@@ -741,12 +742,11 @@ public class TestClassicHttp {
});
this.server.start();
- this.client.setHttpProcessor(new DefaultHttpProcessor(
+ this.client.start(new DefaultHttpProcessor(
new RequestTargetHost(),
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()));
- this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost("localhost", this.server.getPort());
@@ -754,11 +754,11 @@ public class TestClassicHttp {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
post.setEntity(null);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
- Assert.assertEquals(0, received.length);
- this.client.keepAlive(post, response, context);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ Assert.assertEquals(0, received.length);
+ }
}
@Test
@@ -782,7 +782,7 @@ public class TestClassicHttp {
});
this.server.start();
- this.client.setHttpProcessor(new DefaultHttpProcessor(
+ this.client.start(new DefaultHttpProcessor(
new HttpRequestInterceptor() {
@Override
@@ -798,7 +798,6 @@ public class TestClassicHttp {
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()));
- this.client.start();
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost("localhost", this.server.getPort());
@@ -806,9 +805,9 @@ public class TestClassicHttp {
final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
post.setEntity(null);
- final ClassicHttpResponse response = this.client.execute(host, post, context);
- Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
- this.client.keepAlive(post, response, context);
+ try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
+ Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
+ }
}
@Test
@@ -837,10 +836,8 @@ public class TestClassicHttp {
for (int r = 0; r < reqNo; r++) {
final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
- final ClassicHttpResponse response = this.client.execute(host, get, context);
- Assert.assertNull(response.getEntity());
- if (!this.client.keepAlive(get, response, context)) {
- Assert.fail("Connection expected to be re-usable");
+ try (final ClassicHttpResponse response = this.client.execute(host, get, context)) {
+ Assert.assertNull(response.getEntity());
}
}
}
@@ -862,24 +859,23 @@ public class TestClassicHttp {
});
this.server.start();
- this.client.setHttpProcessor(new DefaultHttpProcessor(new RequestContent(), new RequestConnControl()));
- this.client.start();
+ this.client.start(new DefaultHttpProcessor(new RequestContent(), new RequestConnControl()));
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost("localhost", this.server.getPort());
final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest("GET", "/");
get1.setVersion(HttpVersion.HTTP_1_0);
- final ClassicHttpResponse response1 = this.client.execute(host, get1, context);
- Assert.assertEquals(200, response1.getCode());
- EntityUtils.consume(response1.getEntity());
- this.client.keepAlive(get1, response1, context);
+ try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
+ Assert.assertEquals(200, response1.getCode());
+ EntityUtils.consume(response1.getEntity());
+ }
final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest("GET", "/");
- final ClassicHttpResponse response2 = this.client.execute(host, get2, context);
- Assert.assertEquals(400, response2.getCode());
- EntityUtils.consume(response2.getEntity());
- this.client.keepAlive(get2, response2, context);
+ try (final ClassicHttpResponse response2 = this.client.execute(host, get2, context)) {
+ Assert.assertEquals(400, response2.getCode());
+ EntityUtils.consume(response2.getEntity());
+ }
}
}
Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java Sat Apr 1 17:11:51 2017
@@ -44,10 +44,12 @@ import org.apache.hc.core5.http.HttpEnti
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.config.SocketConfig;
import org.apache.hc.core5.http.io.HttpRequestHandler;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.io.ShutdownType;
import org.apache.hc.core5.testing.classic.ClassicTestServer;
import org.junit.After;
import org.junit.Assert;
@@ -63,14 +65,15 @@ public class TestClassicTestClientTestin
@Before
public void initServer() throws Exception {
- this.server = new ClassicTestServer();
- this.server.setTimeout(5000);
+ this.server = new ClassicTestServer(SocketConfig.custom()
+ .setConnectTimeout(5, TimeUnit.SECONDS)
+ .setSoTimeout(5, TimeUnit.SECONDS).build());
}
@After
public void shutDownServer() throws Exception {
if (this.server != null) {
- this.server.shutdown(0, TimeUnit.SECONDS);
+ this.server.shutdown(ShutdownType.IMMEDIATE);
}
}
@@ -86,7 +89,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -103,7 +106,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -120,7 +123,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -137,7 +140,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -154,7 +157,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -174,7 +177,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -195,7 +198,7 @@ public class TestClassicTestClientTestin
try {
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Assert.fail("WebServerTestingFrameworkException should have been thrown");
- } catch (final TestingFrameworkException e) {
+ } catch (TestingFrameworkException e) {
// expected
}
}
@@ -274,7 +277,7 @@ public class TestClassicTestClientTestin
throws HttpException, IOException {
try {
Assert.assertEquals("method not expected", "junk", request.getMethod());
- } catch (final Throwable t) {
+ } catch (Throwable t) {
thrown = t;
}
}
@@ -289,7 +292,7 @@ public class TestClassicTestClientTestin
final Map<String, Object> request = new HashMap<String, Object>();
request.put(PATH, CUSTOM_PATH);
- for (final String method : TestingFramework.ALL_METHODS) {
+ for (String method : TestingFramework.ALL_METHODS) {
request.put(METHOD, method);
adapter.execute(defaultURI, request, requestHandler, responseExpectations);
Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java?rev=1789825&r1=1789824&r2=1789825&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java Sat Apr 1 17:11:51 2017
@@ -36,7 +36,6 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.SSLSocketFactory;
@@ -66,6 +65,7 @@ import org.apache.hc.core5.pool.ConnPool
import org.apache.hc.core5.pool.ControlledConnPool;
import org.apache.hc.core5.pool.PoolEntry;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TimeValue;
/**
* @since 5.0
@@ -175,8 +175,9 @@ public class HttpRequester implements Gr
Args.notNull(context, "HTTP context");
final Future<PoolEntry<HttpHost, HttpClientConnection>> leaseFuture = connPool.lease(targetHost, null, null);
final PoolEntry<HttpHost, HttpClientConnection> poolEntry;
+ final TimeValue connectTimeout = socketConfig != null ? socketConfig.getConnectTimeout() : TimeValue.ZERO_MILLIS;
try {
- poolEntry = leaseFuture.get(socketConfig.getConnectTimeout().toMillis(), TimeUnit.MILLISECONDS);
+ poolEntry = leaseFuture.get(connectTimeout.getDuration(), connectTimeout.getTimeUnit());
} catch (final InterruptedException ex) {
throw new InterruptedIOException(ex.getMessage());
} catch (final ExecutionException ex) {
@@ -191,7 +192,7 @@ public class HttpRequester implements Gr
final Socket socket = createSocket(targetHost);
connection = connectFactory.createConnection(socket);
poolEntry.assignConnection(connection);
- socket.connect(toEndpoint(targetHost), socketConfig.getConnectTimeout().toMillisIntBound());
+ socket.connect(toEndpoint(targetHost), connectTimeout.toMillisIntBound());
}
final ClassicHttpResponse response = execute(connection, request, context);
final HttpEntity entity = response.getEntity();