fix:fix the error capture of rate limit exception. (#860)

pull/863/head
Haotian Zhang 2 years ago committed by GitHub
parent 35adebfca6
commit d3c481038b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,3 +6,4 @@
- [refactor:move loadbalancer to discovery module.](https://github.com/Tencent/spring-cloud-tencent/pull/846)
- [feat:update spring framework version of 2022 branch.](https://github.com/Tencent/spring-cloud-tencent/pull/851)
- [feature:add PolarisRateLimiterLimitedFallback spi.](https://github.com/Tencent/spring-cloud-tencent/pull/857)
- [fix:fix the error capture of rate limit exception.](https://github.com/Tencent/spring-cloud-tencent/pull/860)

@ -0,0 +1,3 @@
coverage:
status:
patch: off

@ -46,15 +46,12 @@ public class BusinessController {
private static final Logger LOG = LoggerFactory.getLogger(BusinessController.class);
private final AtomicInteger index = new AtomicInteger(0);
private final AtomicLong lastTimestamp = new AtomicLong(0);
@Autowired
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
private AtomicLong lastTimestamp = new AtomicLong(0);
/**
* Get information.
* @return information
@ -87,6 +84,7 @@ public class BusinessController {
}).start();
}
count.await();
index.set(0);
return builder.toString();
}

@ -46,6 +46,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.NonNull;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
@ -71,6 +72,9 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
String[] handlerBeanNames = applicationContext.getBeanNamesForType(ResponseErrorHandler.class);
if (handlerBeanNames.length == 1) {
if (this.delegateHandler == null) {
this.delegateHandler = new DefaultResponseErrorHandler();
}
return;
}
@ -211,7 +215,11 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter
return resultRequest;
}
public void setDelegateHandler(ResponseErrorHandler delegateHandler) {
protected ResponseErrorHandler getDelegateHandler() {
return this.delegateHandler;
}
protected void setDelegateHandler(ResponseErrorHandler delegateHandler) {
this.delegateHandler = delegateHandler;
}
}

@ -28,6 +28,7 @@ import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.polaris.api.core.ConsumerAPI;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
@ -40,10 +41,13 @@ import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.AbstractClientHttpResponse;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import static org.mockito.ArgumentMatchers.any;
@ -71,6 +75,9 @@ public class EnhancedRestTemplateReporterTest {
@InjectMocks
private EnhancedRestTemplateReporter enhancedRestTemplateReporter;
@InjectMocks
private EnhancedRestTemplateReporter enhancedRestTemplateReporter2;
@BeforeClass
public static void beforeClass() {
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
@ -99,6 +106,24 @@ public class EnhancedRestTemplateReporterTest {
enhancedRestTemplateReporter.setDelegateHandler(delegate);
}
@Test
public void testSetApplicationContext() {
ApplicationContext applicationContext = mock(ApplicationContext.class);
// test no ResponseErrorHandler
when(applicationContext.getBeanNamesForType(any(Class.class)))
.thenReturn(new String[] {"enhancedRestTemplateReporter"});
enhancedRestTemplateReporter2.setApplicationContext(applicationContext);
Assert.assertTrue(enhancedRestTemplateReporter2.getDelegateHandler() instanceof DefaultResponseErrorHandler);
// test one other ResponseErrorHandler
when(applicationContext.getBeanNamesForType(any(Class.class)))
.thenReturn(new String[] {"enhancedRestTemplateReporter", "mockedResponseErrorHandler"});
when(applicationContext.getBean(anyString())).thenReturn(mock(MockedResponseErrorHandler.class));
enhancedRestTemplateReporter2.setApplicationContext(applicationContext);
Assert.assertTrue(enhancedRestTemplateReporter2.getDelegateHandler() instanceof MockedResponseErrorHandler);
}
@Test
public void testHasError() throws IOException {
when(delegate.hasError(any())).thenReturn(true);
@ -155,21 +180,21 @@ public class EnhancedRestTemplateReporterTest {
verify(delegate).handleError(uri, HttpMethod.GET, response);
}
class MockedClientHttpResponse extends AbstractClientHttpResponse {
static class MockedClientHttpResponse extends AbstractClientHttpResponse {
private HttpHeaders headers;
private final HttpHeaders headers;
MockedClientHttpResponse() {
this.headers = new HttpHeaders();
}
@Override
public int getRawStatusCode() throws IOException {
public int getRawStatusCode() {
return 0;
}
@Override
public String getStatusText() throws IOException {
public String getStatusText() {
return null;
}
@ -193,4 +218,12 @@ public class EnhancedRestTemplateReporterTest {
return HttpStatus.OK;
}
}
private static class MockedResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(@NonNull ClientHttpResponse response) {
}
}
}

Loading…
Cancel
Save