From a29289804c3ba6ab8d16fb06c0ee6b470b8d344f Mon Sep 17 00:00:00 2001 From: lilai Date: Thu, 17 Aug 2023 19:57:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90UT=E3=80=91=E6=B5=81=E9=87=8F=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E9=80=8F=E4=BC=A0=E7=89=B9=E6=80=A7=20httpclient3?= =?UTF-8?q?=E3=80=81Okhttp2=E3=80=81Jdk=20httpclient=20=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tag-transmission-plugin/pom.xml | 2 + .../AbstractClientInterceptor.java | 5 - .../AbstractServerInterceptor.java | 5 - .../client/jdk/JdkHttpClientInterceptor.java | 5 + .../client/okhttp/OkHttp2xInterceptor.java | 5 + .../http/server/HttpServletInterceptor.java | 5 + .../interceptors/BaseInterceptorTest.java | 1 + .../ExecutorInterceptorTest.java | 2 +- ...heduledExecutorServiceInterceptorTest.java | 2 +- .../HttpClient3xInterceptorTest.java | 110 ++++++++++++++++ .../HttpClient4xInterceptorTest.java} | 27 ++-- .../jdk/JdkHttpClientInterceptorTest.java | 110 ++++++++++++++++ .../okhttp/OkHttp2xInterceptorTest.java | 121 ++++++++++++++++++ .../server/HttpServletInterceptorTest.java} | 8 +- .../KafkaConsumerRecordInterceptorTest.java} | 12 +- .../kafka/KafkaProducerInterceptorTest.java} | 24 ++-- .../RocketmqConsumerInterceptorTest.java} | 12 +- .../RocketmqProducerInterceptorTest.java} | 24 ++-- 18 files changed, 424 insertions(+), 56 deletions(-) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{ => crossthread}/ExecutorInterceptorTest.java (97%) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{ => crossthread}/ScheduledExecutorServiceInterceptorTest.java (97%) create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptorTest.java rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{HttpClient4XInterceptorInterceptorTest.java => http/client/httpclient/HttpClient4xInterceptorTest.java} (71%) create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptorTest.java create mode 100644 sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptorTest.java rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{HttpServletInterceptorInterceptorTest.java => http/server/HttpServletInterceptorTest.java} (92%) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{KafkaConsumerRecordInterceptorInterceptorTest.java => mq/kafka/KafkaConsumerRecordInterceptorTest.java} (88%) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{KafkaProducerInterceptorInterceptorTest.java => mq/kafka/KafkaProducerInterceptorTest.java} (80%) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{RocketmqConsumerInterceptorInterceptorTest.java => mq/rocketmq/RocketmqConsumerInterceptorTest.java} (91%) rename sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/{RocketmqProducerInterceptorInterceptorTest.java => mq/rocketmq/RocketmqProducerInterceptorTest.java} (87%) diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml index 8df75aaf72..6557f467f6 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/pom.xml @@ -44,11 +44,13 @@ commons-httpclient commons-httpclient ${commons-httpclient.version} + provided com.squareup.okhttp okhttp ${okhttp.version} + provided javax.servlet diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractClientInterceptor.java index 387101a904..52f8d0f2d2 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractClientInterceptor.java @@ -32,11 +32,6 @@ * @since 2023-07-18 */ public abstract class AbstractClientInterceptor extends AbstractInterceptor { - /** - * 过滤一次处理过程中拦截器的多次调用 - */ - protected static final ThreadLocal LOCK_MARK = new ThreadLocal<>(); - protected final TagTransmissionConfig tagTransmissionConfig; /** diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractServerInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractServerInterceptor.java index 9d84c0ab0a..72ab21249b 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractServerInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/AbstractServerInterceptor.java @@ -32,11 +32,6 @@ * @since 2023-07-18 */ public abstract class AbstractServerInterceptor extends AbstractInterceptor { - /** - * 过滤一次处理过程中拦截器的多次调用 - */ - protected static final ThreadLocal LOCK_MARK = new ThreadLocal<>(); - protected final TagTransmissionConfig tagTransmissionConfig; /** diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java index 343a752d1a..87a840c4e9 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptor.java @@ -32,6 +32,11 @@ * @since 2023-08-08 */ public class JdkHttpClientInterceptor extends AbstractClientInterceptor { + /** + * 过滤一次处理过程中拦截器的多次调用 + */ + protected static final ThreadLocal LOCK_MARK = new ThreadLocal<>(); + @Override public ExecuteContext doBefore(ExecuteContext context) { if (LOCK_MARK.get() != null) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java index e049c79847..090a262768 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptor.java @@ -32,6 +32,11 @@ * @since 2023-08-08 */ public class OkHttp2xInterceptor extends AbstractClientInterceptor { + /** + * 过滤一次处理过程中拦截器的多次调用 + */ + protected static final ThreadLocal LOCK_MARK = new ThreadLocal<>(); + @Override public ExecuteContext doBefore(ExecuteContext context) { if (LOCK_MARK.get() != null) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java index 0b90dd3cce..c73f665f9e 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/main/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptor.java @@ -35,6 +35,11 @@ * @since 2023-07-18 */ public class HttpServletInterceptor extends AbstractServerInterceptor { + /** + * 过滤一次处理过程中拦截器的多次调用 + */ + protected static final ThreadLocal LOCK_MARK = new ThreadLocal<>(); + @Override public ExecuteContext doBefore(ExecuteContext context) { if (LOCK_MARK.get() != null) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java index dbb7b952e9..a64de42ffc 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/BaseInterceptorTest.java @@ -57,6 +57,7 @@ public void before() { @After public void after() { + TrafficUtils.removeTrafficTag(); pluginConfigManagerMockedStatic.close(); } } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ExecutorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ExecutorInterceptorTest.java similarity index 97% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ExecutorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ExecutorInterceptorTest.java index ee278e6a0f..78d42402e8 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ExecutorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ExecutorInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.crossthread; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ScheduledExecutorServiceInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ScheduledExecutorServiceInterceptorTest.java similarity index 97% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ScheduledExecutorServiceInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ScheduledExecutorServiceInterceptorTest.java index 0bb0c159e4..4d008a40ef 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/ScheduledExecutorServiceInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/crossthread/ScheduledExecutorServiceInterceptorTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.crossthread; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptorTest.java new file mode 100644 index 0000000000..e980e575b8 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient3xInterceptorTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.tag.transmission.interceptors.http.client.httpclient; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; + +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * HttpClient3xInterceptor 单元测试 + * + * @author lilai + * @since 2023-08-17 + */ +public class HttpClient3xInterceptorTest extends BaseInterceptorTest { + private final HttpClient3xInterceptor interceptor; + + private final Object[] arguments; + + public HttpClient3xInterceptorTest() { + this.interceptor = new HttpClient3xInterceptor(); + this.arguments = new Object[3]; + } + + @Test + public void testHttpClient3() { + ExecuteContext context; + ExecuteContext resContext; + Map> addHeaders = new HashMap<>(); + Map> tags = new HashMap<>(); + TrafficUtils.removeTrafficTag(); + + // 无Headers无Tags + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(0, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders().length); + TrafficUtils.removeTrafficTag(); + + // 有Headers无Tags + addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(1, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders().length); + Assert.assertEquals("defaultValue", + ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders()[0].getValue() + ); + TrafficUtils.removeTrafficTag(); + + // 有Headers有Tags + List ids = new ArrayList<>(); + ids.add("testId001"); + ids.add("testId002"); + tags.put("id", ids); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(1, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id").length); + Assert.assertEquals("testId001", + ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id")[0].getValue()); + TrafficUtils.removeTrafficTag(); + + // 测试TagTransmissionConfig开关关闭时 + tagTransmissionConfig.setEnabled(false); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(0, ((HttpMethod) resContext.getArguments()[1]).getRequestHeaders("id").length); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); + } + + private ExecuteContext buildContext(Map> addHeaders) { + HttpMethod httpMethod = new GetMethod("sermant.io"); + for (Map.Entry> entry : addHeaders.entrySet()) { + for (String val : entry.getValue()) { + httpMethod.setRequestHeader(entry.getKey(), val); + } + } + + arguments[1] = httpMethod; + return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4XInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptorTest.java similarity index 71% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4XInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptorTest.java index 831a4120b1..9a46fa94a3 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpClient4XInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/httpclient/HttpClient4xInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.http.client.httpclient; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.http.client.httpclient.HttpClient4xInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.http.HttpRequest; import org.apache.http.client.methods.HttpRequestBase; @@ -37,12 +37,12 @@ * @author tangle * @since 2023-07-27 */ -public class HttpClient4XInterceptorInterceptorTest extends BaseInterceptorTest { +public class HttpClient4xInterceptorTest extends BaseInterceptorTest { private final HttpClient4xInterceptor interceptor; private final Object[] arguments; - public HttpClient4XInterceptorInterceptorTest() { + public HttpClient4xInterceptorTest() { interceptor = new HttpClient4xInterceptor(); arguments = new Object[2]; } @@ -59,15 +59,17 @@ public void testClient() { context = buildContext(addHeaders); TrafficUtils.updateTrafficTag(tags); resContext = interceptor.before(context); - Assert.assertEquals(((HttpRequest) resContext.getArguments()[1]).getAllHeaders().length, 0); + Assert.assertEquals(0, ((HttpRequest) resContext.getArguments()[1]).getAllHeaders().length); + TrafficUtils.removeTrafficTag(); // 有Headers无Tags addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); context = buildContext(addHeaders); TrafficUtils.updateTrafficTag(tags); resContext = interceptor.before(context); - Assert.assertEquals(((HttpRequest) resContext.getArguments()[1]).getAllHeaders().length, 1); - Assert.assertEquals(((HttpRequest) resContext.getArguments()[1]).getAllHeaders()[0].getValue(), "defaultValue"); + Assert.assertEquals(1, ((HttpRequest) resContext.getArguments()[1]).getAllHeaders().length); + Assert.assertEquals("defaultValue", ((HttpRequest) resContext.getArguments()[1]).getAllHeaders()[0].getValue()); + TrafficUtils.removeTrafficTag(); // 有Headers有Tags List ids = new ArrayList<>(); @@ -77,13 +79,18 @@ public void testClient() { context = buildContext(addHeaders); TrafficUtils.updateTrafficTag(tags); resContext = interceptor.before(context); - Assert.assertEquals(((HttpRequest) resContext.getArguments()[1]).getHeaders("id").length, 2); - Assert.assertEquals(((HttpRequest) resContext.getArguments()[1]).getHeaders("id")[0].getValue(), "testId001"); + Assert.assertEquals(2, ((HttpRequest) resContext.getArguments()[1]).getHeaders("id").length); + Assert.assertEquals("testId001", ((HttpRequest) resContext.getArguments()[1]).getHeaders("id")[0].getValue()); + TrafficUtils.removeTrafficTag(); // 测试TagTransmissionConfig开关关闭时 tagTransmissionConfig.setEnabled(false); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); resContext = interceptor.before(context); - Assert.assertEquals(context, resContext); + Assert.assertEquals(0, ((HttpRequest) resContext.getArguments()[1]).getHeaders("id").length); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); } private ExecuteContext buildContext(Map> addHeaders) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptorTest.java new file mode 100644 index 0000000000..48df037689 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/jdk/JdkHttpClientInterceptorTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.tag.transmission.interceptors.http.client.jdk; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; + +import sun.net.www.MessageHeader; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * JdkHttpClientInterceptor 单元测试 + * + * @author lilai + * @since 2023-08-17 + */ +public class JdkHttpClientInterceptorTest extends BaseInterceptorTest { + private final JdkHttpClientInterceptor interceptor; + + private final Object[] arguments; + + public JdkHttpClientInterceptorTest() { + this.interceptor = new JdkHttpClientInterceptor(); + this.arguments = new Object[1]; + } + + @Test + public void testHttpClient3() { + ExecuteContext context; + ExecuteContext resContext; + Map> addHeaders = new HashMap<>(); + Map> tags = new HashMap<>(); + TrafficUtils.removeTrafficTag(); + + // 无Headers无Tags + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(0, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); + TrafficUtils.removeTrafficTag(); + + // 有Headers无Tags + addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(1, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); + Assert.assertEquals("defaultValue", + ((MessageHeader) resContext.getArguments()[0]).getHeaders().get("defaultKey").get(0)); + TrafficUtils.removeTrafficTag(); + + // 有Headers有Tags + List ids = new ArrayList<>(); + ids.add("testId001"); + ids.add("testId002"); + tags.put("id", ids); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(2, ((MessageHeader) resContext.getArguments()[0]).getHeaders().get("id").size()); + Assert.assertEquals("testId002", + ((MessageHeader) resContext.getArguments()[0]).getHeaders().get("id").get(0)); + TrafficUtils.removeTrafficTag(); + + // 测试TagTransmissionConfig开关关闭时 + tagTransmissionConfig.setEnabled(false); + addHeaders.clear(); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + Assert.assertEquals(0, ((MessageHeader) resContext.getArguments()[0]).getHeaders().size()); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); + } + + private ExecuteContext buildContext(Map> addHeaders) { + MessageHeader messageHeader = new MessageHeader(); + for (Map.Entry> entry : addHeaders.entrySet()) { + for (String val : entry.getValue()) { + messageHeader.add(entry.getKey(), val); + } + } + arguments[0] = messageHeader; + + return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptorTest.java new file mode 100644 index 0000000000..a44ca01885 --- /dev/null +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/client/okhttp/OkHttp2xInterceptorTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.tag.transmission.interceptors.http.client.okhttp; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.ReflectUtils; +import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; + +import com.squareup.okhttp.Headers; +import com.squareup.okhttp.Request.Builder; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * OkHttp2xInterceptor 单元测试 + * + * @author lilai + * @since 2023-08-17 + */ +public class OkHttp2xInterceptorTest extends BaseInterceptorTest { + private final OkHttp2xInterceptor interceptor; + + public OkHttp2xInterceptorTest() { + this.interceptor = new OkHttp2xInterceptor(); + } + + @Test + public void testHttpClient3() { + ExecuteContext context; + ExecuteContext resContext; + Builder requestBuilder; + Headers.Builder headerBuilder; + List nameAndValues; + Map> addHeaders = new HashMap<>(); + Map> tags = new HashMap<>(); + TrafficUtils.removeTrafficTag(); + + // 无Headers无Tags + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + requestBuilder = (Builder) resContext.getObject(); + headerBuilder = (Headers.Builder) ReflectUtils.getFieldValue(requestBuilder, "headers").get(); + nameAndValues = (List) ReflectUtils.getFieldValue(headerBuilder, "namesAndValues").get(); + Assert.assertEquals(0, nameAndValues.size()); + TrafficUtils.removeTrafficTag(); + + // 有Headers无Tags + addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + requestBuilder = (Builder) resContext.getObject(); + headerBuilder = (Headers.Builder) ReflectUtils.getFieldValue(requestBuilder, "headers").get(); + nameAndValues = (List) ReflectUtils.getFieldValue(headerBuilder, "namesAndValues").get(); + Assert.assertEquals(2, nameAndValues.size()); + Assert.assertEquals("defaultValue", (headerBuilder.get("defaultKey"))); + TrafficUtils.removeTrafficTag(); + + // 有Headers有Tags + List ids = new ArrayList<>(); + ids.add("testId001"); + ids.add("testId002"); + tags.put("id", ids); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + requestBuilder = (Builder) resContext.getObject(); + headerBuilder = (Headers.Builder) ReflectUtils.getFieldValue(requestBuilder, "headers").get(); + nameAndValues = (List) ReflectUtils.getFieldValue(headerBuilder, "namesAndValues").get(); + Assert.assertEquals(6, nameAndValues.size()); + Assert.assertEquals("testId002", headerBuilder.get("id")); + TrafficUtils.removeTrafficTag(); + + // 测试TagTransmissionConfig开关关闭时 + tagTransmissionConfig.setEnabled(false); + addHeaders.clear(); + context = buildContext(addHeaders); + TrafficUtils.updateTrafficTag(tags); + resContext = interceptor.before(context); + requestBuilder = (Builder) resContext.getObject(); + headerBuilder = (Headers.Builder) ReflectUtils.getFieldValue(requestBuilder, "headers").get(); + nameAndValues = (List) ReflectUtils.getFieldValue(headerBuilder, "namesAndValues").get(); + Assert.assertEquals(0, nameAndValues.size()); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); + } + + private ExecuteContext buildContext(Map> addHeaders) { + Builder builder = new Builder(); + for (Map.Entry> entry : addHeaders.entrySet()) { + for (String val : entry.getValue()) { + builder.addHeader(entry.getKey(), val); + } + } + + return ExecuteContext.forMemberMethod(builder, null, null, null, null); + } +} diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpServletInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptorTest.java similarity index 92% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpServletInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptorTest.java index 25173c4f0f..d80f8d049e 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/HttpServletInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/http/server/HttpServletInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.http.server; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.http.server.HttpServletInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Request; @@ -39,12 +39,12 @@ * @author tangle * @since 2023-07-27 */ -public class HttpServletInterceptorInterceptorTest extends BaseInterceptorTest { +public class HttpServletInterceptorTest extends BaseInterceptorTest { private final HttpServletInterceptor interceptor; private final Object[] arguments; - public HttpServletInterceptorInterceptorTest() { + public HttpServletInterceptorTest() { interceptor = new HttpServletInterceptor(); arguments = new Object[2]; } diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaConsumerRecordInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptorTest.java similarity index 88% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaConsumerRecordInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptorTest.java index 769bbca386..49314437b9 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaConsumerRecordInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaConsumerRecordInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.mq.kafka; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.mq.kafka.KafkaConsumerRecordInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.common.header.Headers; @@ -37,10 +37,10 @@ * @author tangle * @since 2023-07-27 */ -public class KafkaConsumerRecordInterceptorInterceptorTest extends BaseInterceptorTest { +public class KafkaConsumerRecordInterceptorTest extends BaseInterceptorTest { private final KafkaConsumerRecordInterceptor interceptor; - public KafkaConsumerRecordInterceptorInterceptorTest() { + public KafkaConsumerRecordInterceptorTest() { interceptor = new KafkaConsumerRecordInterceptor(); } @@ -71,7 +71,7 @@ public void testKafkaConsumer() { tags.put("id", ids); context = buildContext(addHeaders, tags); interceptor.before(context); - Assert.assertEquals(TrafficUtils.getTrafficTag().getTag().get("id").size(), 2); + Assert.assertEquals(2, TrafficUtils.getTrafficTag().getTag().get("id").size()); Assert.assertNull(TrafficUtils.getTrafficTag().getTag().get("name")); // 第二次消费,测试tags是否污染其他消费 @@ -82,7 +82,7 @@ public void testKafkaConsumer() { context = buildContext(addHeaders, tags); interceptor.before(context); Assert.assertNull(TrafficUtils.getTrafficTag().getTag().get("id")); - Assert.assertEquals(TrafficUtils.getTrafficTag().getTag().get("name").size(), 1); + Assert.assertEquals(1, TrafficUtils.getTrafficTag().getTag().get("name").size()); // 测试TagTransmissionConfig开关关闭时 TrafficUtils.removeTrafficTag(); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaProducerInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptorTest.java similarity index 80% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaProducerInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptorTest.java index eda0663219..f87aa07711 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/KafkaProducerInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/kafka/KafkaProducerInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.mq.kafka; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.mq.kafka.KafkaProducerInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.header.Header; @@ -40,12 +40,12 @@ * @author tangle * @since 2023-07-27 */ -public class KafkaProducerInterceptorInterceptorTest extends BaseInterceptorTest { +public class KafkaProducerInterceptorTest extends BaseInterceptorTest { private final KafkaProducerInterceptor interceptor; private final Object[] arguments; - public KafkaProducerInterceptorInterceptorTest() { + public KafkaProducerInterceptorTest() { interceptor = new KafkaProducerInterceptor(); arguments = new Object[2]; } @@ -62,7 +62,7 @@ public void testKafkaProducer() { addHeaders.put("defaultKey", Collections.singletonList("defaultValue")); context = buildContext(addHeaders); resContext = interceptor.before(context); - Assert.assertEquals(getValuesFromRecord(resContext, "defaultKey"), Collections.singletonList("defaultValue")); + Assert.assertEquals(Collections.singletonList("defaultValue"), getValuesFromRecord(resContext, "defaultKey")); // 有Headers有Tags List ids = new ArrayList<>(); @@ -72,7 +72,8 @@ public void testKafkaProducer() { TrafficUtils.updateTrafficTag(tags); context = buildContext(addHeaders); resContext = interceptor.before(context); - Assert.assertEquals(getValuesFromRecord(resContext, "id"), ids); + Assert.assertEquals(ids, getValuesFromRecord(resContext, "id")); + TrafficUtils.removeTrafficTag(); // 第二次生产,测试tags是否污染其他消费 tags.clear(); @@ -82,13 +83,18 @@ public void testKafkaProducer() { TrafficUtils.updateTrafficTag(tags); context = buildContext(addHeaders); resContext = interceptor.before(context); - Assert.assertEquals(getValuesFromRecord(resContext, "id"), new ArrayList<>()); - Assert.assertEquals(getValuesFromRecord(resContext, "name"), names); + Assert.assertEquals(0, getValuesFromRecord(resContext, "id").size()); + Assert.assertEquals(names, getValuesFromRecord(resContext, "name")); + TrafficUtils.removeTrafficTag(); // 测试TagTransmissionConfig开关关闭时 tagTransmissionConfig.setEnabled(false); + TrafficUtils.updateTrafficTag(tags); + context = buildContext(addHeaders); resContext = interceptor.before(context); - Assert.assertEquals(context, resContext); + Assert.assertEquals(0, getValuesFromRecord(resContext, "name").size()); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); } private List getValuesFromRecord(ExecuteContext resContext, String key) { diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqConsumerInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptorTest.java similarity index 91% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqConsumerInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptorTest.java index 79f72b96e5..ecc43c96d2 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqConsumerInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqConsumerInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.mq.rocketmq; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.mq.rocketmq.RocketmqConsumerInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.rocketmq.common.message.Message; import org.junit.Assert; @@ -44,10 +44,10 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest(RocketmqConsumerInterceptor.class) -public class RocketmqConsumerInterceptorInterceptorTest extends BaseInterceptorTest { +public class RocketmqConsumerInterceptorTest extends BaseInterceptorTest { private final RocketmqConsumerInterceptor interceptor; - public RocketmqConsumerInterceptorInterceptorTest() throws Exception { + public RocketmqConsumerInterceptorTest() throws Exception { RocketmqConsumerInterceptor interceptorBase = new RocketmqConsumerInterceptor(); interceptor = PowerMockito.spy(interceptorBase); PowerMockito.doReturn(true).when(interceptor, "isRocketMqStackTrace", Mockito.any()); @@ -101,7 +101,7 @@ public void testRocketmqConsumer() { tags.put("id", Collections.singletonList("testId001")); context = buildContext(addHeaders, tags); interceptor.before(context); - Assert.assertEquals(TrafficUtils.getTrafficTag().getTag().get("id").get(0), "testId001"); + Assert.assertEquals("testId001", TrafficUtils.getTrafficTag().getTag().get("id").get(0)); Assert.assertNull(TrafficUtils.getTrafficTag().getTag().get("name")); // 第二次消费,测试tags是否污染其他消费 @@ -112,7 +112,7 @@ public void testRocketmqConsumer() { context = buildContext(addHeaders, tags); interceptor.before(context); Assert.assertNull(TrafficUtils.getTrafficTag().getTag().get("id")); - Assert.assertEquals(TrafficUtils.getTrafficTag().getTag().get("name").get(0), "testName001"); + Assert.assertEquals("testName001", TrafficUtils.getTrafficTag().getTag().get("name").get(0)); // 测试TagTransmissionConfig开关关闭时 TrafficUtils.removeTrafficTag(); diff --git a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqProducerInterceptorInterceptorTest.java b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptorTest.java similarity index 87% rename from sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqProducerInterceptorInterceptorTest.java rename to sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptorTest.java index 165fa0b9e8..2f58bd536a 100644 --- a/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/RocketmqProducerInterceptorInterceptorTest.java +++ b/sermant-plugins/sermant-tag-transmission/tag-transmission-plugin/src/test/java/com/huaweicloud/sermant/tag/transmission/interceptors/mq/rocketmq/RocketmqProducerInterceptorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.huaweicloud.sermant.tag.transmission.interceptors; +package com.huaweicloud.sermant.tag.transmission.interceptors.mq.rocketmq; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.tag.TrafficUtils; -import com.huaweicloud.sermant.tag.transmission.interceptors.mq.rocketmq.RocketmqProducerInterceptor; +import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest; import org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader; import org.junit.Assert; @@ -35,12 +35,12 @@ * @author tangle * @since 2023-07-27 */ -public class RocketmqProducerInterceptorInterceptorTest extends BaseInterceptorTest { +public class RocketmqProducerInterceptorTest extends BaseInterceptorTest { private final RocketmqProducerInterceptor interceptor; private final Object[] arguments; - public RocketmqProducerInterceptorInterceptorTest() { + public RocketmqProducerInterceptorTest() { interceptor = new RocketmqProducerInterceptor(); arguments = new Object[12]; } @@ -53,6 +53,15 @@ public void test() { Map> tags = new HashMap<>(); TrafficUtils.removeTrafficTag(); + // 测试TagTransmissionConfig开关关闭时 + tagTransmissionConfig.setEnabled(false); + TrafficUtils.updateTrafficTag(tags); + context = buildContext(addHeaders); + resContext = interceptor.before(context); + Assert.assertNull(((SendMessageRequestHeader) resContext.getArguments()[3]).getProperties()); + tagTransmissionConfig.setEnabled(true); + TrafficUtils.removeTrafficTag(); + // 包含Headers但不包含tags addHeaders.put("defualtKey", "defaultValue"); context = buildContext(addHeaders); @@ -68,6 +77,7 @@ public void test() { Assert.assertEquals(getTruthProperties(addHeaders, tags), ((SendMessageRequestHeader) resContext.getArguments()[3]).getProperties()); interceptor.after(context); + TrafficUtils.removeTrafficTag(); // 第二次Message(不同tag),测试tag数据不会污染其他Message tags.clear(); @@ -78,11 +88,7 @@ public void test() { Assert.assertEquals(getTruthProperties(addHeaders, tags), ((SendMessageRequestHeader) resContext.getArguments()[3]).getProperties()); interceptor.after(context); - - // 测试TagTransmissionConfig开关关闭时 - tagTransmissionConfig.setEnabled(false); - resContext = interceptor.before(context); - Assert.assertEquals(context, resContext); + TrafficUtils.removeTrafficTag(); } private String getTruthProperties(Map headers, Map> tags) {