模拟 java.net.ConnectException: 连接超时

Simulate java.net.ConnectException: Connection timout

我想在我的 java 应用程序中人为地创建一个连接超时来测试我的连接超时处理。我看过这个帖子 Artificially create a connection timeout error

但是当我尝试建议时 http://10.255.255.1 or http://www.google.com:81 我用来进行 http 调用的库(Apache http 客户端 3.1)给我一个连接被拒绝的异常而不是超时。

[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)

有人可以建议我可以调整什么或不同的建议一起让我测试连接超时吗?


编辑

这是我要测试的代码....

    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();
    }

谢谢

你为什么不使用模拟测试?恕我直言,它是最好的模拟工具之一:JMockit。您可以模拟,例如 HttpClient class 并抛出 connectionTimeOutException(或任何您真正需要测试代码的东西)。

根据您发送的代码,我仍然不清楚超时发生时会发生什么。它会抛出异常吗?当它发生时,您的代码如何识别它?

请看一下。如果您需要进一步的帮助,请告诉我 =]

to allow me to test a connection timeout?

您可以使用 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.

支持两种场景:添加延迟和模拟连接超时异常(延迟后抛出)和其他套接字异常(立即抛出)

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 上提供的在线演示也适用于非网络应用程序。

免责声明:我是 Sniffy

的作者