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
- java line of code to print heart
- Chinese Language Version of Linux Performance Monitoring Software netdata
- Interface Test Code Coverage (jacoco) Schema Sharing
- Performance Test Framework
- How to have a pleasant performance test on the Linux command line interface
- Graphic HTTP Brain Map
- How to test probabilistic business interfaces
- httpclient handles multiuser simultaneous online
- Automatically turn swagger documents into test code
- Five lines of code to build a static blog
- How httpclient handles 302 redirects
- Preliminary Study on Test Framework of Linear Interface Based on java
- Tcloud Cloud Measurement Platform--A Master
Selected non-technical articles
- Why choose software testing as your career path?
- Ten steps to becoming a great Java developer
- Programming thinking written to everyone
- Barriers to automated testing
- What's wrong with automated testing
- Tested Immortal Code Brain Map
- 7 Steps to Become a Good Automated Test Engineer
- Attitude of Excellent Software Developers
- How to Execute Functional API Testing Correctly
- New Trends in Software Testing in the Next 10 Years-Up
- New Trends in Software Testing in the Next 10 Years-Up
- What problems did automated testing solve