You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/06/07 06:49:38 UTC
[dubbo] branch master updated: Add unit test for
ConsumerContextClusterInterceptor(#7972)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 57c60c2 Add unit test for ConsumerContextClusterInterceptor(#7972)
57c60c2 is described below
commit 57c60c24f782c7a817ab2d985640b8f9547d4916
Author: passer <wh...@gmail.com>
AuthorDate: Mon Jun 7 14:49:22 2021 +0800
Add unit test for ConsumerContextClusterInterceptor(#7972)
---
.../ConsumerContextClusterInterceptor.java | 2 +
.../ConsumerContextClusterInterceptorTest.java | 107 +++++++++++++++++++++
2 files changed, 109 insertions(+)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
index c1046ee..4281cfb 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptor.java
@@ -26,6 +26,8 @@ import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
@Activate
public class ConsumerContextClusterInterceptor implements ClusterInterceptor, ClusterInterceptor.Listener {
+ public static final String NAME = "context";
+
@Override
public void before(AbstractClusterInvoker<?> invoker, Invocation invocation) {
if (invocation instanceof RpcInvocation) {
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptorTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptorTest.java
new file mode 100644
index 0000000..e4f76f5
--- /dev/null
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ConsumerContextClusterInterceptorTest.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.dubbo.rpc.cluster.interceptor;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.dubbo.rpc.cluster.interceptor.ConsumerContextClusterInterceptor.NAME;
+
+public class ConsumerContextClusterInterceptorTest {
+
+ private ConsumerContextClusterInterceptor interceptor;
+
+ @BeforeEach
+ public void setUp() {
+ interceptor = (ConsumerContextClusterInterceptor) ExtensionLoader.
+ getExtensionLoader(ClusterInterceptor.class).getExtension(NAME);
+ }
+
+ @Test
+ public void testGetExtension() {
+ Assertions.assertNotNull(interceptor);
+ Assertions.assertTrue(interceptor instanceof ConsumerContextClusterInterceptor);
+ }
+
+ @Test
+ public void testGetActivateExtension() {
+ URL url = URL.valueOf("test://test:80");
+ List<ClusterInterceptor> interceptors =
+ ExtensionLoader.getExtensionLoader(ClusterInterceptor.class).getActivateExtension(url, "");
+
+ Assertions.assertTrue(CollectionUtils.isNotEmpty(interceptors));
+ long count = interceptors.stream().
+ filter(interceptor -> interceptor instanceof ConsumerContextClusterInterceptor).count();
+ Assertions.assertEquals(1l, count);
+ }
+
+ @Test
+ public void testBefore() {
+ AbstractClusterInvoker mockInvoker = Mockito.mock(AbstractClusterInvoker.class);
+ RpcContext serverContextBefore = RpcContext.getServerContext();
+ RpcInvocation rpcInvocation = new RpcInvocation();
+
+ interceptor.before(mockInvoker, rpcInvocation);
+
+ RpcContext serverContextAfter = RpcContext.getServerContext();
+
+ Assertions.assertNotSame(serverContextBefore, serverContextAfter);
+ Assertions.assertSame(mockInvoker, rpcInvocation.getInvoker());
+ }
+
+ @Test
+ public void testAfter() {
+ RpcContext contextBefore = RpcContext.getContext();
+ interceptor.after(null, null);
+ RpcContext contextAfter = RpcContext.getContext();
+ Assertions.assertNotSame(contextBefore, contextAfter);
+ }
+
+ @Test
+ public void testOnMessage() {
+ RpcContext serverContext = RpcContext.getServerContext();
+ Result mockResult = Mockito.mock(Result.class);
+ Map<String, Object> map = Collections.singletonMap("key", "value");
+ Mockito.when(mockResult.getObjectAttachments()).thenReturn(map);
+ interceptor.onMessage(mockResult, null, null);
+
+ Map<String, Object> objectAttachments = serverContext.getObjectAttachments();
+ Assertions.assertNotNull(objectAttachments);
+ Assertions.assertTrue(objectAttachments.size() == 1);
+ Assertions.assertEquals("value", objectAttachments.get("key"));
+ }
+
+ @Test
+ public void testOnError() {
+ interceptor.onError(new RpcException(), null, null);
+ }
+
+}