Richie Richie - 8 months ago 80
HTTP Question

Simulate java.net.ConnectException: Connection timout

I would like to artificially create a connection timeout in my java application to test my connection timeout handling. I've had a look at this thread Artificially create a connection timeout error.

But when I attempt what is suggested such as http://10.255.255.1 or http://www.google.com:81 the libs I'm using to make http calls (Apache http client 3.1 ) gives me a connection refused exception instead of a timeout.

[9/07/15 19:28:45:000 EST] 00000088 SystemErr R IOException Connection refused: connect
[9/07/15 19:28:46:188 EST] 00000088 SystemErr R java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:336)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:201)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
at java.net.Socket.connect(Socket.java:478)
at sun.reflect.GeneratedMethodAccessor187.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:140)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)


Can someone suggest what I can tweak or a different suggestion all together to allow me to test a connection timeout?




EDIT



Here is the code I'm trying to test....

HttpClient client = new HttpClient();
HttpConnectionManagerParams httpConnectionManagerParams = client.getHttpConnectionManager().getParams();
httpConnectionManagerParams.setConnectionTimeout(45000);
httpConnectionManagerParams.setSoTimeout(45000);
httpConnectionManagerParams.setTcpNoDelay(false);

// Create a method instance.
GetMethod method = new GetMethod(url);
HttpMethodParams httpMethodParams = method.getParams();

// no retries
httpMethodParams.setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
httpMethodParams.setSoTimeout(45000);

try {
// Execute the method.
int statusCode = client.executeMethod(method);

// Read the response body.
byte[] responseBody = method.getResponseBody();
jsonResponse = new String(
new String(responseBody).getBytes(JsonConstants.DEFAULT_CHARSET), JsonConstants.DEFAULT_CHARSET);

} catch (HttpException e) {
System.err.println("HttpException " + e.getMessage());
e.printStackTrace();

} catch (IOException e) {
System.err.println("IOException " + e.getMessage());
e.printStackTrace();

} finally {
// Release the connection.
method.releaseConnection();
}


thanks

Alp Alp
Answer Source

Why don't you use mock testing ? IMHO it is one of the best mocking tools out there: JMockit. You can mock up, for example HttpClient class and throw connectionTimeOutException (or whatever you really need to test your code).

It is still unclear to me, what happens when time out occurs, from the code that you sent. Does it throw an exception ? How does your code recognize it when it happens ?

Please take a look at it. If you need further assitance, give me a shout =]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download