Skip to content

Commit

Permalink
【UT】流量标签透传特性
Browse files Browse the repository at this point in the history
httpclient3、Okhttp2、Jdk httpclient 拦截器UT
  • Loading branch information
lilai23 committed Aug 24, 2023
1 parent 09f771e commit a292898
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
* @since 2023-07-18
*/
public abstract class AbstractClientInterceptor<Carrier> extends AbstractInterceptor {
/**
* 过滤一次处理过程中拦截器的多次调用
*/
protected static final ThreadLocal<Boolean> LOCK_MARK = new ThreadLocal<>();

protected final TagTransmissionConfig tagTransmissionConfig;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
* @since 2023-07-18
*/
public abstract class AbstractServerInterceptor<Carrier> extends AbstractInterceptor {
/**
* 过滤一次处理过程中拦截器的多次调用
*/
protected static final ThreadLocal<Boolean> LOCK_MARK = new ThreadLocal<>();

protected final TagTransmissionConfig tagTransmissionConfig;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
* @since 2023-08-08
*/
public class JdkHttpClientInterceptor extends AbstractClientInterceptor<MessageHeader> {
/**
* 过滤一次处理过程中拦截器的多次调用
*/
protected static final ThreadLocal<Boolean> LOCK_MARK = new ThreadLocal<>();

@Override
public ExecuteContext doBefore(ExecuteContext context) {
if (LOCK_MARK.get() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
* @since 2023-08-08
*/
public class OkHttp2xInterceptor extends AbstractClientInterceptor<Builder> {
/**
* 过滤一次处理过程中拦截器的多次调用
*/
protected static final ThreadLocal<Boolean> LOCK_MARK = new ThreadLocal<>();

@Override
public ExecuteContext doBefore(ExecuteContext context) {
if (LOCK_MARK.get() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
* @since 2023-07-18
*/
public class HttpServletInterceptor extends AbstractServerInterceptor<HttpServletRequest> {
/**
* 过滤一次处理过程中拦截器的多次调用
*/
protected static final ThreadLocal<Boolean> LOCK_MARK = new ThreadLocal<>();

@Override
public ExecuteContext doBefore(ExecuteContext context) {
if (LOCK_MARK.get() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void before() {

@After
public void after() {
TrafficUtils.removeTrafficTag();
pluginConfigManagerMockedStatic.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> addHeaders = new HashMap<>();
Map<String, List<String>> 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<String> 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<String, List<String>> addHeaders) {
HttpMethod httpMethod = new GetMethod("sermant.io");
for (Map.Entry<String, List<String>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
}
Expand All @@ -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<String> ids = new ArrayList<>();
Expand All @@ -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<String, List<String>> addHeaders) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> addHeaders = new HashMap<>();
Map<String, List<String>> 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<String> 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<String, List<String>> addHeaders) {
MessageHeader messageHeader = new MessageHeader();
for (Map.Entry<String, List<String>> 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);
}
}
Loading

0 comments on commit a292898

Please sign in to comment.