Skip to content

Commit

Permalink
【UT】增加流量标签透传的UT、完善流量标签透传拦截器继承结构
Browse files Browse the repository at this point in the history
  • Loading branch information
TangLeDaily committed Aug 11, 2023
1 parent 7368744 commit 4497e5b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class RocketmqConsumerInterceptor extends AbstractServerInterceptor {

@Override
public ExecuteContext doBefore(ExecuteContext context) {
if (!isAvailable()) {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (!isRocketMqStackTrace(stackTraceElements)) {
return context;
}
if (context.getObject() instanceof Message) {
Expand All @@ -64,8 +65,7 @@ public ExecuteContext doBefore(ExecuteContext context) {
*
* @return boolean
*/
private boolean isAvailable() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
private boolean isRocketMqStackTrace(StackTraceElement[] stackTraceElements) {
int stackTraceIdxMax = stackTraceElements.length - 1;
for (int i = 0; i < stackTraceElements.length; i++) {
if (!ROCKETMQ_SELECT_CLASSNAME.equals(stackTraceElements[i].getClassName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ private String insertTags2Properties(String oldProperties, TrafficTag trafficTag

@Override
public ExecuteContext doAfter(ExecuteContext context) {
TrafficUtils.removeTrafficTag();
return context;
}

@Override
public ExecuteContext onThrow(ExecuteContext context) {
TrafficUtils.removeTrafficTag();
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.huaweicloud.sermant.tag.transmission;

import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils;
import com.huaweicloud.sermant.tag.transmission.config.CrossThreadConfig;

import org.junit.AfterClass;
Expand All @@ -38,6 +39,7 @@ public static void before() {
mockPluginConfigManager = Mockito.mockStatic(PluginConfigManager.class);
mockPluginConfigManager.when(() -> PluginConfigManager.getPluginConfig(CrossThreadConfig.class))
.thenReturn(new CrossThreadConfig());
TrafficUtils.removeTrafficTag();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.huaweicloud.sermant.tag.transmission.interceptors;

import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils;
import com.huaweicloud.sermant.tag.transmission.config.TagTransmissionConfig;

import org.junit.After;
Expand All @@ -33,12 +34,12 @@
* @author tangle
* @since 2023-07-27
*/
public class BaseTest {
public class BaseInterceptorTest {
public final TagTransmissionConfig tagTransmissionConfig = new TagTransmissionConfig();

public MockedStatic<PluginConfigManager> pluginConfigManagerMockedStatic;

public BaseTest() {
public BaseInterceptorTest() {
pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class);
pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(TagTransmissionConfig.class))
.thenReturn(tagTransmissionConfig);
Expand All @@ -51,6 +52,7 @@ public void before() {
tagKeys.add("id");
tagKeys.add("name");
tagTransmissionConfig.setTagKeys(tagKeys);
TrafficUtils.removeTrafficTag();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
* @author tangle
* @since 2023-07-27
*/
public class HttpClient4xInterceptorTest extends BaseTest {
public class HttpClient4XInterceptorInterceptorTest extends BaseInterceptorTest {
private final HttpClient4xInterceptor interceptor;

private final Object[] arguments;

public HttpClient4xInterceptorTest() {
public HttpClient4XInterceptorInterceptorTest() {
interceptor = new HttpClient4xInterceptor();
arguments = new Object[2];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -39,12 +38,12 @@
* @author tangle
* @since 2023-07-27
*/
public class HttpServletInterceptorTest extends BaseTest {
public class HttpServletInterceptorInterceptorTest extends BaseInterceptorTest {
private final HttpServletInterceptor interceptor;

private final Object[] arguments;

public HttpServletInterceptorTest() {
public HttpServletInterceptorInterceptorTest() {
interceptor = new HttpServletInterceptor();
arguments = new Object[2];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
* @author tangle
* @since 2023-07-27
*/
public class KafkaConsumerRecordInterceptorTest extends BaseTest {
public class KafkaConsumerRecordInterceptorInterceptorTest extends BaseInterceptorTest {
private final KafkaConsumerRecordInterceptor interceptor;

public KafkaConsumerRecordInterceptorTest() {
public KafkaConsumerRecordInterceptorInterceptorTest() {
interceptor = new KafkaConsumerRecordInterceptor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
* @author tangle
* @since 2023-07-27
*/
public class KafkaProducerInterceptorTest extends BaseTest {
public class KafkaProducerInterceptorInterceptorTest extends BaseInterceptorTest {
private final KafkaProducerInterceptor interceptor;

private final Object[] arguments;

public KafkaProducerInterceptorTest() {
public KafkaProducerInterceptorInterceptorTest() {
interceptor = new KafkaProducerInterceptor();
arguments = new Object[2];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -41,13 +43,37 @@
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(RocketmqConsumerInterceptor.class)
public class RocketmqConsumerInterceptorTest extends BaseTest {
public class RocketmqConsumerInterceptorInterceptorTest extends BaseInterceptorTest {
private final RocketmqConsumerInterceptor interceptor;

public RocketmqConsumerInterceptorTest() throws Exception {
public RocketmqConsumerInterceptorInterceptorTest() throws Exception {
RocketmqConsumerInterceptor interceptorBase = new RocketmqConsumerInterceptor();
interceptor = PowerMockito.spy(interceptorBase);
PowerMockito.when(interceptor, "isAvailable").thenReturn(true);
PowerMockito.doReturn(true).when(interceptor, "isRocketMqStackTrace", Mockito.any());
}

@Test
public void testIsAvailable() throws Exception {
StackTraceElement[] stackTraceElements = new StackTraceElement[2];
RocketmqConsumerInterceptor interceptorBase = new RocketmqConsumerInterceptor();

// 测试可通过
stackTraceElements[0] = new StackTraceElement("org.apache.rocketmq.common.message.Message", "xxxMethod",
"xxxFile", 1);
stackTraceElements[1] = new StackTraceElement("org.apache.xxx", "xxxMethod",
"xxxFile", 1);
boolean resultAccess = Whitebox.invokeMethod(interceptorBase, "isRocketMqStackTrace",
(Object) stackTraceElements);
Assert.assertTrue(resultAccess);

// 测试不可通过
stackTraceElements[0] = new StackTraceElement("org.apache.rocketmq.common.message.Message", "xxxMethod",
"xxxFile", 1);
stackTraceElements[1] = new StackTraceElement("org.apache.rocketmq", "xxxMethod",
"xxxFile", 1);
boolean resultRefuse = Whitebox.invokeMethod(interceptorBase, "isRocketMqStackTrace",
(Object) stackTraceElements);
Assert.assertFalse(resultRefuse);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
* @author tangle
* @since 2023-07-27
*/
public class RocketmqProducerInterceptorTest extends BaseTest {
public class RocketmqProducerInterceptorInterceptorTest extends BaseInterceptorTest {
private final RocketmqProducerInterceptor interceptor;

private final Object[] arguments;

public RocketmqProducerInterceptorTest() {
public RocketmqProducerInterceptorInterceptorTest() {
interceptor = new RocketmqProducerInterceptor();
arguments = new Object[12];
}
Expand Down

0 comments on commit 4497e5b

Please sign in to comment.