Skip to content

Commit

Permalink
Merge pull request sermant-io#1296 from daizhenyu/develop-rpc-ut
Browse files Browse the repository at this point in the history
流量标签透传插件 dubbo、sofarpc、servicecombrpc UT
  • Loading branch information
Sherlockhan authored Sep 11, 2023
2 parents 64d7eeb + 6358bcc commit dbf95d7
Show file tree
Hide file tree
Showing 10 changed files with 951 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.rpc;

import com.huaweicloud.sermant.core.utils.tag.TrafficTag;
import com.huaweicloud.sermant.tag.transmission.interceptors.BaseInterceptorTest;

import org.junit.Before;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* rpc模块 单元测试抽象类
*
* @author daizhenyu
* @since 2023-09-06
**/
public abstract class AbstractRpcInterceptorTest extends BaseInterceptorTest {
// 构建流量标签的key-value关系,用于构建参数
public Map<String, List<String>> fullTrafficTag;

public AbstractRpcInterceptorTest() {
}

@Before
public void beforeTest() {
fullTrafficTag = new HashMap<>();
fullTrafficTag.put("id", Collections.singletonList("001"));
fullTrafficTag.put("name", Collections.singletonList("test001"));

// 初始化流量标签
Map<String, List<String>> tag = new HashMap<>();
tag.put("id", Collections.singletonList("001"));
tag.put("name", Collections.singletonList("test001"));
fullTrafficTag = new HashMap<>(tag);
TrafficTag trafficTag = new TrafficTag(tag);
doBefore(trafficTag);
}

public abstract void doBefore(TrafficTag trafficTag);

public Map<String, List<String>> buildExpectTrafficTag(String... keys) {
Map<String, List<String>> expectTag = new HashMap<>();
for (String key : keys) {
expectTag.put(key, fullTrafficTag.get(key));
}
return expectTag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.rpc.dubbo;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.utils.tag.TrafficTag;
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils;
import com.huaweicloud.sermant.tag.transmission.interceptors.rpc.AbstractRpcInterceptorTest;

import com.alibaba.dubbo.rpc.RpcInvocation;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
* AlibabaDubboConsumerInterceptor类的单元测试
*
* @author daizhenyu
* @since 2023-08-09
**/
public class AlibabaDubboConsumerInterceptorTest extends AbstractRpcInterceptorTest {
private final AlibabaDubboConsumerInterceptor interceptor = new AlibabaDubboConsumerInterceptor();

public AlibabaDubboConsumerInterceptorTest() {
}

@Override
public void doBefore(TrafficTag trafficTag) {
TrafficUtils.setTrafficTag(trafficTag);
}

@Test
public void testAlibabaDubboConsumer() {
// 定义参数
ExecuteContext context;
ExecuteContext returnContext;
Map<String, String> expectAttachments;

// RpcInvocation为null
context = buildContext(null);
returnContext = interceptor.before(context);
Assert.assertNull(returnContext.getArguments()[1]);

// 流量标签透传开关关闭
tagTransmissionConfig.setEnabled(false);
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertNull(((RpcInvocation) returnContext.getArguments()[1]).getAttachments());
tagTransmissionConfig.setEnabled(true);

// TrafficTag包含完整的流量标签
expectAttachments = buildExpectAttachments("id", "name");
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertEquals(expectAttachments, ((RpcInvocation) returnContext.getArguments()[1]).getAttachments());

// TrafficTag包含部分的流量标签
TrafficUtils.getTrafficTag().getTag().remove("id");
expectAttachments = buildExpectAttachments("name");
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertEquals(expectAttachments, ((RpcInvocation) returnContext.getArguments()[1]).getAttachments());

// TrafficTag没有流量标签
TrafficUtils.removeTrafficTag();
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertNull(((RpcInvocation) returnContext.getArguments()[1]).getAttachments());
}

private Map<String, String> buildExpectAttachments(String... keys) {
Map<String, String> expectAttachments = new HashMap<>();
for (String key : keys) {
expectAttachments.put(key, fullTrafficTag.get(key).get(0));
}
return expectAttachments;
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation) {
Object[] arguments = new Object[]{null, rpcInvocation};
return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.rpc.dubbo;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.utils.tag.TrafficTag;
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils;
import com.huaweicloud.sermant.tag.transmission.interceptors.rpc.AbstractRpcInterceptorTest;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcInvocation;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* AlibabaDubboProviderInterceptor类的单元测试
*
* @author daizhenyu
* @since 2023-08-29
**/
public class AlibabaDubboProviderInterceptorTest extends AbstractRpcInterceptorTest {
private final AlibabaDubboProviderInterceptor interceptor = new AlibabaDubboProviderInterceptor();

public AlibabaDubboProviderInterceptorTest() {
}

@Override
public void doBefore(TrafficTag trafficTag) {
}

@Test
public void testAlibabaDubboProvider() {
// 定义参数
ExecuteContext context;
ExecuteContext returnContext;
Map<String, String> attachments;
Map<String, List<String>> expectTag;

// invoker为consumer端
context = buildContext(new RpcInvocation(), new HashMap<>(), "consumer");
interceptor.before(context);
Assert.assertNull(TrafficUtils.getTrafficTag());

// 后续test均为provider端, Invocation对象为null
context = buildContext(null, new HashMap<>(), "provider");
interceptor.before(context);
Assert.assertNull(TrafficUtils.getTrafficTag());

// 流量标签透传开关关闭
tagTransmissionConfig.setEnabled(false);
context = buildContext(new RpcInvocation(), new HashMap<>(), "provider");
interceptor.before(context);
Assert.assertNull(TrafficUtils.getTrafficTag());
tagTransmissionConfig.setEnabled(true);

// Invocation对象的attachments包含全部key
attachments = new HashMap<>();
attachments.put("id", "001");
attachments.put("name", "test001");
context = buildContext(new RpcInvocation(), attachments, "provider");
returnContext = interceptor.before(context);
expectTag = buildExpectTrafficTag("id", "name");
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(returnContext);

// Invocation对象的attachments包含部分流量标签
attachments = new HashMap<>();
attachments.put("id", "001");
context = buildContext(new RpcInvocation(), attachments, "provider");
returnContext = interceptor.before(context);
expectTag = buildExpectTrafficTag("id");
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(returnContext);
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation, Map<String, String> headers, String side) {
URL url = new URL("http", "127.0.0.1", 8080);
url = url.addParameter("side", side);
Invoker invoker = new DubboInvoker<>(String.class, url, null);
if (rpcInvocation != null) {
rpcInvocation.setAttachments(headers);
}
Object[] arguments = new Object[]{invoker, rpcInvocation};
return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.rpc.dubbo;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.utils.tag.TrafficTag;
import com.huaweicloud.sermant.core.utils.tag.TrafficUtils;
import com.huaweicloud.sermant.tag.transmission.interceptors.rpc.AbstractRpcInterceptorTest;

import org.apache.dubbo.rpc.RpcInvocation;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
* ApacheDubboConsumerInterceptor类的单元测试
*
* @author daizhenyu
* @since 2023-08-29
**/
public class ApacheDubboConsumerInterceptorTest extends AbstractRpcInterceptorTest {
private final ApacheDubboConsumerInterceptor interceptor = new ApacheDubboConsumerInterceptor();

public ApacheDubboConsumerInterceptorTest() {
}

@Override
public void doBefore(TrafficTag trafficTag) {
TrafficUtils.setTrafficTag(trafficTag);
}

@Test
public void testApacheDubboConsumer() {
// 定义参数
ExecuteContext context;
ExecuteContext returnContext;
Map<String, String> expectAttachments;

// RpcInvocation为null
context = buildContext(null);
returnContext = interceptor.before(context);
Assert.assertNull(returnContext.getArguments()[1]);

// 流量标签透传开关关闭
tagTransmissionConfig.setEnabled(false);
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
expectAttachments = buildExpectAttachments();
Assert.assertEquals(expectAttachments, ((RpcInvocation) returnContext.getArguments()[1]).getAttachments());
tagTransmissionConfig.setEnabled(true);

// TrafficTag包含完整的流量标签
expectAttachments = buildExpectAttachments("id", "name");
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertEquals(((RpcInvocation) returnContext.getArguments()[1]).getAttachments(),
expectAttachments);

// TrafficTag包含部分的流量标签
TrafficUtils.getTrafficTag().getTag().remove("id");
expectAttachments = buildExpectAttachments("name");
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertEquals(((RpcInvocation) returnContext.getArguments()[1]).getAttachments(),
expectAttachments);

// TrafficTag没有流量标签
TrafficUtils.removeTrafficTag();
expectAttachments = buildExpectAttachments();
context = buildContext(new RpcInvocation());
returnContext = interceptor.before(context);
Assert.assertEquals(expectAttachments, ((RpcInvocation) returnContext.getArguments()[1]).getAttachments());
}

private Map<String, String> buildExpectAttachments(String... keys) {
Map<String, String> expectAttachments = new HashMap<>();
for (String key : keys) {
expectAttachments.put(key, fullTrafficTag.get(key).get(0));
}
return expectAttachments;
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation) {
Object[] arguments = new Object[]{null, rpcInvocation};
return ExecuteContext.forMemberMethod(new Object(), null, arguments, null, null);
}
}
Loading

0 comments on commit dbf95d7

Please sign in to comment.