Ejemplo n.º 1
0
  @Test
  public void testConnectionRequestTimeout() {
    testConnectionResetCount = 0;
    httpClientUtils = new HttpClientUtils();

    httpClientUtils.setMaxConnPerRoute(1);
    httpClientUtils.setMaxConnTotal(1);
    httpClientUtils.setConnectRequestTimeout(100);
    httpClientUtils.setConnectTimeout(10);

    httpClientUtils.initHttpClient();

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            if (e.getMessage().contains("Timeout waiting for connection from pool")) {
              Assert.assertTrue(false);
              // throw new RuntimeException("No available connect to user", e);
            }
            return e == null;
          }
        };
    runTest("http://localhost:65534/test/testConnectionRequestTimeout", 1, exceptionHandler, 2);
  }
Ejemplo n.º 2
0
  @Test
  public void testConnectionTimeout() {
    testConnectionTimeoutCount = 0;
    httpClientUtils = new HttpClientUtils();

    httpClientUtils.setMaxConnPerRoute(1);
    httpClientUtils.setMaxConnTotal(1);

    /*
          PING www.a.shifen.com (115.239.210.26) 56(84) bytes of data.
    64 bytes from 115.239.210.26: icmp_seq=1 ttl=50 time=6.97 ms
    PING www-wide.l.google.com (74.125.128.199) 56(84) bytes of data.
    64 bytes from hg-in-f199.1e100.net (74.125.128.199): icmp_seq=1 ttl=42 time=33.2 ms
     */
    httpClientUtils.setConnectTimeout(10);

    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return e == null;
          }
        };
    ExceptionHandler exceptionHandler2 =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return (ConnectException.class == e.getClass())
                && "Network is unreachable".equals(e.getMessage());
          }
        };

    runTest("http://www.baidu.com", 1, exceptionHandler, 1);
    runTest("https://www.lufax.com/", 1, exceptionHandler2, 1);

    /*
            测试结论:
    		1、...timed out. Connection will be retried using another IP address

    		9、runTest("https://www.google.com.hk", 1, exceptionHandler, 1); 测试日志阶段截段:
    [BUG]-[2014-03-05 19:40:06.211]-[testConnectionCount_12] - Connecting to www.google.com.hk/74.125.128.199:443
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114)

    [DEBUG]-[2014-03-05 19:40:06.213]-[testConnectionCount_12] - Connect to www.google.com.hk/74.125.128.199:443 timed out. Connection will be retried using another IP address
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:143)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connecting to www.google.com.hk/2404:6800:4005:c00:0:0:0:c7:443
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Shutdown connection
    [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]
    	org.apache.http.impl.conn.LoggingManagedHttpClientConnection.shutdown(LoggingManagedHttpClientConnection.java:87)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection discarded
    [org.apache.http.impl.execchain.MainClientExec]
    	org.apache.http.impl.execchain.ConnectionHolder.abortConnection(ConnectionHolder.java:126)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Close connection
    [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]
    	org.apache.http.impl.conn.LoggingManagedHttpClientConnection.close(LoggingManagedHttpClientConnection.java:79)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection released: [id: 101][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 0 of 1; total allocated: 0 of 1]
    [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]
    	org.apache.http.impl.conn.PoolingHttpClientConnectionManager.releaseConnection(PoolingHttpClientConnectionManager.java:282)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_13] - Connection leased: [id: 102][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 1 of 1; total allocated: 1 of 1]
    [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]
    	org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:246)
    		 */
  }