Skip to content

Instantly share code, notes, and snippets.

@horiga
Last active March 8, 2017 13:37
Show Gist options
  • Save horiga/fbecb87166a613c32897f1014e8a7e4f to your computer and use it in GitHub Desktop.
Save horiga/fbecb87166a613c32897f1014e8a7e4f to your computer and use it in GitHub Desktop.
How to HTTP timeout testing
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.Response;
import com.sun.net.httpserver.HttpServer;
public class HttpTimeoutTest {
private static Logger log = LoggerFactory.getLogger(HttpTimeoutTest.class);
@Test
public void connectTimeout() {
int port;
ServerSocket serverSocket;
try {
int backlog = 1;
serverSocket = new ServerSocket(0, backlog);
port = serverSocket.getLocalPort();
// connect
new Socket().connect(serverSocket.getLocalSocketAddress());
} catch (Exception e) {
throw new RuntimeException(e);
}
int connectionTimeoutInMs = 1000;
int requestTimeoutInMs = 3000;
int idleConnectionInPoolTimeoutInMs = 20 * 1000;
int idleConnectionTimeoutInMs = 20 * 1000;
AsyncHttpClient ahc = new AsyncHttpClient(
new AsyncHttpClientConfig.Builder()
.setAllowPoolingConnection(true)
.setCompressionEnabled(true)
.setConnectionTimeoutInMs(connectionTimeoutInMs)
.setRequestTimeoutInMs(requestTimeoutInMs)
.setFollowRedirects(false)
.setIdleConnectionInPoolTimeoutInMs(idleConnectionInPoolTimeoutInMs)
.setIdleConnectionTimeoutInMs(idleConnectionTimeoutInMs)
.build());
int executionTimeoutInMs = 3000;
try {
Response response = ahc.prepareGet("http://localhost:" + port)
.execute()
.get(executionTimeoutInMs, TimeUnit.MILLISECONDS);
log.debug("{}", response);
} catch (Exception e) {
log.error("Error, connect to server!!", e);
}
try {
serverSocket.close();
} catch (Exception ignore) {}
}
@Test
public void requestTimeout() throws Exception {
int port = 20011;
HttpServer httpServer = HttpServer.create(new InetSocketAddress("localhost", port), port);
httpServer.setExecutor(Executors.newFixedThreadPool(1));
httpServer.createContext("/echo", exchange -> {
try {
Thread.sleep(10000);
final String response = "Hello World";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (Exception ignore) {}
});
httpServer.start();
try {
int connectionTimeoutInMs = 1000;
int requestTimeoutInMs = 3000;
int idleConnectionInPoolTimeoutInMs = 20 * 1000;
int idleConnectionTimeoutInMs = 20 * 1000;
AsyncHttpClient ahc = new AsyncHttpClient(
new AsyncHttpClientConfig.Builder()
.setAllowPoolingConnection(true)
.setCompressionEnabled(true)
.setConnectionTimeoutInMs(connectionTimeoutInMs)
.setRequestTimeoutInMs(requestTimeoutInMs)
.setFollowRedirects(false)
.setIdleConnectionInPoolTimeoutInMs(idleConnectionInPoolTimeoutInMs)
.setIdleConnectionTimeoutInMs(idleConnectionTimeoutInMs)
.build());
int executionTimeoutInMs = 5000;
Response response = ahc.prepareGet("http://localhost:" + port + "/echo")
.execute()
.get(executionTimeoutInMs, TimeUnit.MILLISECONDS);
log.debug("{}", response);
} catch (Exception e) {
log.error("Error, read timeout!! ", e);
}
httpServer.stop(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment