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:50:40 UTC

[dubbo] branch master updated: Add unit test for ZoneAwareClusterInterceptor (#7974)

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 9892958  Add unit test for ZoneAwareClusterInterceptor (#7974)
9892958 is described below

commit 9892958fb3c37ecb1a19516544e0bf562fdd82df
Author: passer <wh...@gmail.com>
AuthorDate: Mon Jun 7 14:50:32 2021 +0800

    Add unit test for ZoneAwareClusterInterceptor (#7974)
---
 .../interceptor/ZoneAwareClusterInterceptor.java   |  7 +-
 .../ZoneAwareClusterInterceptorTest.java           | 97 ++++++++++++++++++++++
 .../internal/org.apache.dubbo.rpc.ZoneDetector     |  1 +
 3 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptor.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptor.java
index 6daec08..44862e7 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptor.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptor.java
@@ -26,6 +26,7 @@ import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
 
 import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE;
 import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_FORCE;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
 
 /**
  * Determines the zone information of current request.
@@ -35,14 +36,16 @@ import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_
 @Activate(value = "cluster:zone-aware")
 public class ZoneAwareClusterInterceptor implements ClusterInterceptor {
 
+    public static final String NAME = "zone-aware";
+
     @Override
     public void before(AbstractClusterInvoker<?> clusterInvoker, Invocation invocation) {
         RpcContext rpcContext = RpcContext.getContext();
         String zone = (String) rpcContext.getAttachment(REGISTRY_ZONE);
         String force = (String) rpcContext.getAttachment(REGISTRY_ZONE_FORCE);
         ExtensionLoader<ZoneDetector> loader = ExtensionLoader.getExtensionLoader(ZoneDetector.class);
-        if (StringUtils.isEmpty(zone) && loader.hasExtension("default")) {
-            ZoneDetector detector = loader.getExtension("default");
+        if (StringUtils.isEmpty(zone) && loader.hasExtension(DEFAULT_KEY)) {
+            ZoneDetector detector = loader.getExtension(DEFAULT_KEY);
             zone = detector.getZoneOfCurrentRequest(invocation);
             force = detector.isZoneForcingEnabled(invocation, zone);
         }
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptorTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptorTest.java
new file mode 100644
index 0000000..da5e20e
--- /dev/null
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/interceptor/ZoneAwareClusterInterceptorTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.rpc.Invocation;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.ZoneDetector;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.apache.dubbo.rpc.cluster.interceptor.ZoneAwareClusterInterceptor.NAME;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_FORCE;
+
+public class ZoneAwareClusterInterceptorTest {
+
+    private ZoneAwareClusterInterceptor interceptor;
+
+    @BeforeEach
+    public void setUp() {
+        interceptor = (ZoneAwareClusterInterceptor) ExtensionLoader.
+                getExtensionLoader(ClusterInterceptor.class).getExtension(NAME);
+    }
+
+    @Test
+    public void testGetExtension() {
+        Assertions.assertNotNull(interceptor);
+        Assertions.assertTrue(interceptor instanceof ZoneAwareClusterInterceptor);
+    }
+
+    @Test
+    public void testGetActivateExtension() {
+        URL url = URL.valueOf("test://test:80?cluster=zone-aware");
+        List<ClusterInterceptor> interceptors = ExtensionLoader.
+                getExtensionLoader(ClusterInterceptor.class).getActivateExtension(url, "cluster");
+
+        Assertions.assertNotNull(interceptors);
+        long count = interceptors.stream().
+                filter(interceptor -> interceptor instanceof ZoneAwareClusterInterceptor).count();
+
+        Assertions.assertEquals(1L, count);
+    }
+
+    @Test
+    public void testBefore() {
+        RpcInvocation invocation = new RpcInvocation();
+        interceptor.before(null, invocation);
+        Assertions.assertEquals("mock_zone", invocation.getAttachment(REGISTRY_ZONE));
+        Assertions.assertEquals("mock_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
+
+        RpcContext context = RpcContext.getContext();
+        context.setAttachment(REGISTRY_ZONE, "context_zone");
+        context.setAttachment(REGISTRY_ZONE_FORCE, "context_force");
+        interceptor.before(null, invocation);
+        Assertions.assertEquals("context_zone", invocation.getAttachment(REGISTRY_ZONE));
+        Assertions.assertEquals("context_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
+    }
+
+    @Test
+    public void testAfter() {
+        // pass
+        interceptor.after(null, null);
+    }
+
+    public static class MockZoneDetector implements ZoneDetector {
+
+        @Override
+        public String getZoneOfCurrentRequest(Invocation invocation) {
+            return "mock_zone";
+        }
+
+        @Override
+        public String isZoneForcingEnabled(Invocation invocation, String zone) {
+            return "mock_force";
+        }
+    }
+}
diff --git a/dubbo-cluster/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.ZoneDetector b/dubbo-cluster/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.ZoneDetector
new file mode 100644
index 0000000..c4cccf7
--- /dev/null
+++ b/dubbo-cluster/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.ZoneDetector
@@ -0,0 +1 @@
+default=org.apache.dubbo.rpc.cluster.interceptor.ZoneAwareClusterInterceptorTest$MockZoneDetector
\ No newline at end of file