fix:fix the error capture of rate limit exception.

pull/1080/head
Haotian Zhang 2 years ago
parent 52689515f5
commit edeeaded92

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

@ -91,6 +91,7 @@ public class BusinessController {
}).start(); }).start();
} }
count.await(); count.await();
index.set(0);
return builder.toString(); return builder.toString();
} }

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

@ -20,39 +20,36 @@ package com.tencent.cloud.rpc.enhancement.resttemplate;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.rpc.ServiceCallResult; import org.checkerframework.checker.nullness.qual.NonNull;
import org.assertj.core.api.Assertions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic; import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.client.AbstractClientHttpResponse; 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 org.springframework.web.client.ResponseErrorHandler;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -78,8 +75,9 @@ public class EnhancedRestTemplateReporterTest {
private ResponseErrorHandler delegate; private ResponseErrorHandler delegate;
@InjectMocks @InjectMocks
private EnhancedRestTemplateReporter enhancedRestTemplateReporter; private EnhancedRestTemplateReporter enhancedRestTemplateReporter;
@Captor
private ArgumentCaptor captor; @InjectMocks
private EnhancedRestTemplateReporter enhancedRestTemplateReporter2;
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
@ -109,6 +107,24 @@ public class EnhancedRestTemplateReporterTest {
enhancedRestTemplateReporter.setDelegateHandler(delegate); 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 @Test
public void testHasError() throws IOException { public void testHasError() throws IOException {
when(delegate.hasError(any())).thenReturn(true); when(delegate.hasError(any())).thenReturn(true);
@ -129,13 +145,9 @@ public class EnhancedRestTemplateReporterTest {
enhancedRestTemplateReporter.hasError(response); enhancedRestTemplateReporter.hasError(response);
URI uri = mock(URI.class); URI uri = mock(URI.class);
String labels = URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8);
response.getHeaders().set(RouterConstant.ROUTER_LABEL_HEADER, labels);
enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response); enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response);
verify(consumerAPI, times(2)).updateServiceCallResult((ServiceCallResult) captor.capture()); verify(consumerAPI, times(2)).updateServiceCallResult(any());
ServiceCallResult value = (ServiceCallResult) captor.getValue();
Assertions.assertThat(value.getLabels()).isEqualTo("k1:v1|k2:v2");
verify(delegate).handleError(uri, HttpMethod.GET, response); verify(delegate).handleError(uri, HttpMethod.GET, response);
} }
@ -169,21 +181,21 @@ public class EnhancedRestTemplateReporterTest {
verify(delegate).handleError(uri, HttpMethod.GET, response); verify(delegate).handleError(uri, HttpMethod.GET, response);
} }
class MockedClientHttpResponse extends AbstractClientHttpResponse { static class MockedClientHttpResponse extends AbstractClientHttpResponse {
private HttpHeaders headers; private final HttpHeaders headers;
MockedClientHttpResponse() { MockedClientHttpResponse() {
this.headers = new HttpHeaders(); this.headers = new HttpHeaders();
} }
@Override @Override
public int getRawStatusCode() throws IOException { public int getRawStatusCode() {
return 0; return 0;
} }
@Override @Override
public String getStatusText() throws IOException { public String getStatusText() {
return null; return null;
} }
@ -207,4 +219,12 @@ public class EnhancedRestTemplateReporterTest {
return HttpStatus.OK; return HttpStatus.OK;
} }
} }
private static class MockedResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(@NonNull ClientHttpResponse response) {
}
}
} }

Loading…
Cancel
Save