Consistent timeout settings in integration tests

This commit is contained in:
Oleg Kalnichevski 2017-09-02 16:49:42 +02:00
parent dcc09e92e0
commit b4e0611b05
6 changed files with 36 additions and 9 deletions

View File

@ -30,6 +30,7 @@ package org.apache.hc.client5.testing.async;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.apache.hc.core5.function.Decorator; import org.apache.hc.core5.function.Decorator;
@ -62,7 +63,13 @@ public abstract class IntegrationTestBase extends LocalAsyncServerTestBase {
@Override @Override
protected void before() throws Throwable { protected void before() throws Throwable {
clientBuilder = HttpAsyncClientBuilder.create().setConnectionManager(connManager); clientBuilder = HttpAsyncClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom()
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT)
.setConnectionRequestTimeout(TIMEOUT)
.build())
.setConnectionManager(connManager);
} }
@Override @Override

View File

@ -37,11 +37,15 @@ import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.testing.nio.Http2TestServer; import org.apache.hc.core5.testing.nio.Http2TestServer;
import org.apache.hc.core5.util.TimeValue; import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
public abstract class LocalAsyncServerTestBase { public abstract class LocalAsyncServerTestBase {
public static final Timeout TIMEOUT = Timeout.ofSeconds(30);
public static final Timeout LONG_TIMEOUT = Timeout.ofSeconds(60);
protected final URIScheme scheme; protected final URIScheme scheme;
public LocalAsyncServerTestBase(final URIScheme scheme) { public LocalAsyncServerTestBase(final URIScheme scheme) {
@ -61,7 +65,9 @@ public abstract class LocalAsyncServerTestBase {
@Override @Override
protected void before() throws Throwable { protected void before() throws Throwable {
server = new Http2TestServer( server = new Http2TestServer(
IOReactorConfig.DEFAULT, IOReactorConfig.custom()
.setSoTimeout(TIMEOUT)
.build(),
scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null); scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null);
server.register("/echo/*", new Supplier<AsyncServerExchangeHandler>() { server.register("/echo/*", new Supplier<AsyncServerExchangeHandler>() {

View File

@ -97,7 +97,7 @@ public class TestAsyncStatefulConnManagement extends IntegrationTestBase {
worker.start(); worker.start();
} }
for (final HttpWorker worker : workers) { for (final HttpWorker worker : workers) {
worker.join(10000); worker.join(LONG_TIMEOUT.toMillis());
} }
for (final HttpWorker worker : workers) { for (final HttpWorker worker : workers) {
final Exception ex = worker.getException(); final Exception ex = worker.getException();

View File

@ -64,6 +64,7 @@ import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.reactor.ListenerEndpoint; import org.apache.hc.core5.reactor.ListenerEndpoint;
import org.apache.hc.core5.testing.nio.Http2TestServer; import org.apache.hc.core5.testing.nio.Http2TestServer;
import org.apache.hc.core5.util.TimeValue; import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule; import org.junit.Rule;
@ -75,6 +76,8 @@ import org.junit.runners.Parameterized;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class TestHttpAsyncMinimal { public class TestHttpAsyncMinimal {
public static final Timeout TIMEOUT = Timeout.ofSeconds(30);
@Parameterized.Parameters(name = "{0} {1}") @Parameterized.Parameters(name = "{0} {1}")
public static Collection<Object[]> protocols() { public static Collection<Object[]> protocols() {
return Arrays.asList(new Object[][]{ return Arrays.asList(new Object[][]{
@ -102,7 +105,9 @@ public class TestHttpAsyncMinimal {
@Override @Override
protected void before() throws Throwable { protected void before() throws Throwable {
server = new Http2TestServer( server = new Http2TestServer(
IOReactorConfig.DEFAULT, IOReactorConfig.custom()
.setSoTimeout(TIMEOUT)
.build(),
scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null); scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null);
server.register("/echo/*", new Supplier<AsyncServerExchangeHandler>() { server.register("/echo/*", new Supplier<AsyncServerExchangeHandler>() {
@ -141,7 +146,7 @@ public class TestHttpAsyncMinimal {
.setTlsStrategy(new H2TlsStrategy(SSLTestContexts.createClientSSLContext())) .setTlsStrategy(new H2TlsStrategy(SSLTestContexts.createClientSSLContext()))
.build(); .build();
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom() final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
.setSoTimeout(5, TimeUnit.SECONDS) .setSoTimeout(TIMEOUT)
.build(); .build();
if (version.greaterEquals(HttpVersion.HTTP_2)) { if (version.greaterEquals(HttpVersion.HTTP_2)) {
httpclient = HttpAsyncClients.createMinimal( httpclient = HttpAsyncClients.createMinimal(

View File

@ -28,8 +28,8 @@
package org.apache.hc.client5.testing.sync; package org.apache.hc.client5.testing.sync;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient; import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
import org.apache.hc.client5.http.impl.sync.HttpClientBuilder; import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
@ -44,6 +44,7 @@ import org.apache.hc.core5.http.io.HttpServerRequestHandler;
import org.apache.hc.core5.http.protocol.HttpProcessor; import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.io.ShutdownType; import org.apache.hc.core5.io.ShutdownType;
import org.apache.hc.core5.testing.classic.ClassicTestServer; import org.apache.hc.core5.testing.classic.ClassicTestServer;
import org.apache.hc.core5.util.Timeout;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
@ -52,6 +53,9 @@ import org.junit.rules.ExternalResource;
*/ */
public abstract class LocalServerTestBase { public abstract class LocalServerTestBase {
public static final Timeout TIMEOUT = Timeout.ofSeconds(30);
public static final Timeout LONG_TIMEOUT = Timeout.ofSeconds(60);
public LocalServerTestBase(final URIScheme scheme) { public LocalServerTestBase(final URIScheme scheme) {
this.scheme = scheme; this.scheme = scheme;
} }
@ -72,7 +76,7 @@ public abstract class LocalServerTestBase {
server = new ClassicTestServer( server = new ClassicTestServer(
scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
SocketConfig.custom() SocketConfig.custom()
.setSoTimeout(5, TimeUnit.SECONDS) .setSoTimeout(TIMEOUT)
.build()); .build());
server.registerHandler("/echo/*", new EchoHandler()); server.registerHandler("/echo/*", new EchoHandler());
server.registerHandler("/random/*", new RandomHandler()); server.registerHandler("/random/*", new RandomHandler());
@ -102,9 +106,14 @@ public abstract class LocalServerTestBase {
protected void before() throws Throwable { protected void before() throws Throwable {
connManager = new PoolingHttpClientConnectionManager(); connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultSocketConfig(SocketConfig.custom() connManager.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(5, TimeUnit.SECONDS) .setSoTimeout(TIMEOUT)
.build()); .build());
clientBuilder = HttpClientBuilder.create() clientBuilder = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom()
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT)
.setConnectionRequestTimeout(TIMEOUT)
.build())
.setConnectionManager(connManager); .setConnectionManager(connManager);
} }

View File

@ -107,7 +107,7 @@ public class TestStatefulConnManagement extends LocalServerTestBase {
worker.start(); worker.start();
} }
for (final HttpWorker worker : workers) { for (final HttpWorker worker : workers) {
worker.join(10000); worker.join(LONG_TIMEOUT.toMillis());
} }
for (final HttpWorker worker : workers) { for (final HttpWorker worker : workers) {
final Exception ex = worker.getException(); final Exception ex = worker.getException();