2
votes

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

2
Is it possible to show us the part of your code that you want to test ?Alp
added code snippet i am trying to testRichie
You can't get 'connection refused' from 10.255.255.1 unless you have a host whose IP address is 10.255.2555.1, in which case the solution is to use an IP address in that subnet that you don't have.user207421
@Richie please take a look at my answer.Alp
I mean you can't get it period, if you're in that subnet and there isn't such a host. You will get a connect timeout. Which client you're using has nothing to do with it, provided it's reporting the error correctly.user207421

2 Answers

1
votes

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 =]

0
votes

to allow me to test a connection timeout?

You can use Sniffy for simulating network latency - it seamlessly integrates with Java TCP stack and doesn't require any changes from application side. See documentation on emulating network issues.

Two scenarios are supported: adding latency and simulating connection timeout exceptions (throws after a delay) and other socket exceptions (throw right away)

Online demo available at demo.sniffy.io - click on widget in bottom-right corner, go to "Network Connections" tab and set delay to say 1000 milliseconds for en.wikipedia.org. Refresh the browser page and you'll see that it takes 1 seconds more to load the page. javaagent integration is also available for non-web applications.

enter image description here

Disclaimer: I'm the author of Sniffy