Retry Controller Settings in httpclient Interface Test

Keywords: Programming Java Linux SSL socket

In the process of using httpclient for interface testing, I did not take into account the failure of request auto-retry before, but sometimes I need to retry when some errors occur, such as timeout, frequent rejection of response, etc. After reading the official examples, I wrote a controller for auto-retry.Share the code for your reference.

Here's how to get a controller:

    /**
     * Get Retry Controller
     *
     * @return
     */
    private static HttpRequestRetryHandler getHttpRequestRetryHandler() {
        return new HttpRequestRetryHandler() {
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                logger.warn("An error occurred in the request!", exception);
                if (executionCount > HttpClientConstant.TRY_TIMES) return false;
                if (exception instanceof NoHttpResponseException) {
                    logger.warn("No response exception");
                    sleep(1);
                    return true;
                } else if (exception instanceof ConnectTimeoutException) {
                    logger.warn("Connection timed out, retry");
                    sleep(5);
                    return true;
                } else if (exception instanceof SSLHandshakeException) {
                    logger.warn("Local Certificate Exception");
                    return false;
                } else if (exception instanceof InterruptedIOException) {
                    logger.warn("IO Interrupt exception");
                    sleep(1);
                    return true;
                } else if (exception instanceof UnknownHostException) {
                    logger.warn("Server exception not found");
                    return false;
                } else if (exception instanceof SSLException) {
                    logger.warn("SSL abnormal");
                    return false;
                } else if (exception instanceof HttpHostConnectException) {
                    logger.warn("Host Connection Exception");
                    return false;
                } else if (exception instanceof SocketException) {
                    logger.warn("socket abnormal");
                    return false;
                } else {
                    logger.warn("Unrecorded request exception:{}", exception.getClass());
                }
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                HttpRequest request = clientContext.getRequest();
                // If the request is idempotent, try again
                if (!(request instanceof HttpEntityEnclosingRequest)) {
                    sleep(2);
                    return true;
                }
                return false;
            }
        };
    }

This time-out and number of retries are used as a basis for determining whether an interface request failed.Here are the controller setup methods:

	 /**
     * Get https protocol request object through connection pool
     * <p>
     * Increase the default request controller, request configuration, connect the controller, cancel the cookiestore, separate resolve the header that responds to set-cookie s and sends requests, adapting to situations where multiple users are online at the same time
     * </p>
     *
     * @return
     */
    private static CloseableHttpClient getCloseableHttpsClients() {
        // Create a custom httpsclient object
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).setRetryHandler(httpRequestRetryHandler).setDefaultRequestConfig(requestConfig).build();
//         CloseableHttpClient client = HttpClients.createDefault(); //Non-connection pool creation
        return client;
    }

Selected Technical Articles

  1. java line of code to print heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  3. Interface Test Code Coverage (jacoco) Schema Sharing
  4. Performance Test Framework
  5. How to have a pleasant performance test on the Linux command line interface
  6. Graphic HTTP Brain Map
  7. How to test probabilistic business interfaces
  8. httpclient handles multiuser simultaneous online
  9. Automatically turn swagger documents into test code
  10. Five lines of code to build a static blog
  11. How httpclient handles 302 redirects
  12. Preliminary Study on Test Framework of Linear Interface Based on java
  13. Tcloud Cloud Measurement Platform--A Master

Selected non-technical articles

  1. Why choose software testing as your career path?
  2. Ten steps to becoming a great Java developer
  3. Programming thinking written to everyone
  4. Barriers to automated testing
  5. What's wrong with automated testing
  6. Tested Immortal Code Brain Map
  7. 7 Steps to Become a Good Automated Test Engineer
  8. Attitude of Excellent Software Developers
  9. How to Execute Functional API Testing Correctly
  10. New Trends in Software Testing in the Next 10 Years-Up
  11. New Trends in Software Testing in the Next 10 Years-Up
  12. What problems did automated testing solve

Click to view the Public Number map

Posted by shopphp on Mon, 23 Sep 2019 19:50:16 -0700