You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by cr...@apache.org on 2021/09/12 08:58:54 UTC

[dubbo] branch 3.0 updated: UT: refer metadata service (#8761)

This is an automated email from the ASF dual-hosted git repository.

crazyhzm pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 27ef85c  UT: refer metadata service (#8761)
27ef85c is described below

commit 27ef85c9d7c7375d75ee8d4ed17b562caaf4afe7
Author: huazhongming <cr...@gmail.com>
AuthorDate: Sun Sep 12 16:58:46 2021 +0800

    UT: refer metadata service (#8761)
    
    * add ut for refer metadata service
    
    * fix mockito
    
    * fix
    
    * fix
    
    * fix
---
 .github/workflows/build-and-test-3.yml             |   2 +-
 .../registry/client/DefaultServiceInstance.java    |  12 +-
 .../registry/client/metadata/MetadataUtils.java    |  13 +-
 .../SpringCloudMetadataServiceURLBuilder.java      |   6 +-
 .../client/metadata/MetadataUtilsTest.java         | 184 +++++++++++++++++++++
 .../ProtocolPortsMetadataCustomizerTest.java       |  11 +-
 6 files changed, 217 insertions(+), 11 deletions(-)

diff --git a/.github/workflows/build-and-test-3.yml b/.github/workflows/build-and-test-3.yml
index 9712bb8..1e60559 100644
--- a/.github/workflows/build-and-test-3.yml
+++ b/.github/workflows/build-and-test-3.yml
@@ -94,7 +94,7 @@ jobs:
         if: ${{ startsWith( matrix.os, 'ubuntu') }}
         run: ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Pjacoco -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=false -Dcheckstyle_unix.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true
       - name: "Test with Maven without Integration Tests"
-        timeout-minutes: 80
+        timeout-minutes: 90
         if: ${{ startsWith( matrix.os, 'windows') }}
         run: ./mvnw --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Pjacoco -D"http.keepAlive=false" -D"maven.wagon.http.pool=false" -D"maven.wagon.httpconnectionManager.ttlSeconds=120" -D"maven.wagon.http.retryHandler.count=5" -DskipTests=false -DskipIntegrationTests=true -D"checkstyle.skip=false" -D"checkstyle_unix.skip=true" -D"rat.skip=false" -D"maven.javadoc.skip=true"
       - name: "Upload coverage to Codecov"
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
index aba5ccc..3d9a5d4 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java
@@ -58,8 +58,15 @@ public class DefaultServiceInstance implements ServiceInstance {
 
     private transient String address;
     private transient MetadataInfo serviceMetadata;
-    // used at runtime
-    private transient String registryCluster; // extendParams can be more flexiable, but one single property uses less space
+
+    /**
+     * used at runtime
+     */
+    private transient String registryCluster;
+
+    /**
+     * extendParams can be more flexible, but one single property uses less space
+     */
     private transient Map<String, String> extendParams;
     private transient List<Endpoint> endpoints;
     private transient Map<String, Object> attributes = new HashMap<>();
@@ -174,6 +181,7 @@ public class DefaultServiceInstance implements ServiceInstance {
         return registryCluster;
     }
 
+    @Override
     public void setRegistryCluster(String registryCluster) {
         this.registryCluster = registryCluster;
     }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
index 53f09db..97034d6 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
@@ -78,14 +78,14 @@ public class MetadataUtils {
     }
 
     private static MetadataService referProxy(String key, ServiceInstance instance) {
-        MetadataServiceURLBuilder builder = null;
+        MetadataServiceURLBuilder builder;
         ExtensionLoader<MetadataServiceURLBuilder> loader = instance.getOrDefaultApplicationModel()
             .getExtensionLoader(MetadataServiceURLBuilder.class);
 
         Map<String, String> metadata = instance.getMetadata();
         // METADATA_SERVICE_URLS_PROPERTY_NAME is a unique key exists only on instances of spring-cloud-alibaba.
-        String dubboURLsJSON = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME);
-        if (metadata.isEmpty() || StringUtils.isEmpty(dubboURLsJSON)) {
+        String dubboUrlsForJson = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME);
+        if (metadata.isEmpty() || StringUtils.isEmpty(dubboUrlsForJson)) {
             builder = loader.getExtension(StandardMetadataServiceURLBuilder.NAME);
         } else {
             builder = loader.getExtension(SpringCloudMetadataServiceURLBuilder.NAME);
@@ -107,4 +107,11 @@ public class MetadataUtils {
         return proxyFactory.getProxy(invoker);
     }
 
+    public static ConcurrentMap<String, MetadataService> getMetadataServiceProxies() {
+        return metadataServiceProxies;
+    }
+
+    public static ConcurrentMap<String, Invoker<?>> getMetadataServiceInvokers() {
+        return metadataServiceInvokers;
+    }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java
index c564060..f3ff0e8 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/SpringCloudMetadataServiceURLBuilder.java
@@ -41,11 +41,11 @@ public class SpringCloudMetadataServiceURLBuilder implements MetadataServiceURLB
     @Override
     public List<URL> build(ServiceInstance serviceInstance) {
         Map<String, String> metadata = serviceInstance.getMetadata();
-        String dubboURLsJSON = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME);
-        if (StringUtils.isBlank(dubboURLsJSON)) {
+        String dubboUrlsForJson = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME);
+        if (StringUtils.isBlank(dubboUrlsForJson)) {
             return Collections.emptyList();
         }
-        List<String> urlStrings = JSON.parseArray(dubboURLsJSON, String.class);
+        List<String> urlStrings = JSON.parseArray(dubboUrlsForJson, String.class);
         return urlStrings.stream().map(URL::valueOf).collect(Collectors.toList());
     }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataUtilsTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataUtilsTest.java
new file mode 100644
index 0000000..17cbccd
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataUtilsTest.java
@@ -0,0 +1,184 @@
+/*
+ * 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.registry.client.metadata;
+
+
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.metadata.MetadataService;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import org.apache.dubbo.registry.integration.DemoService;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ScopeModelUtil;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.any;
+
+public class MetadataUtilsTest {
+
+    @AfterEach
+    public void tearDown() throws IOException {
+        Mockito.framework().clearInlineMocks();
+    }
+
+    @Test
+    public void testComputeKey() {
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "1");
+        ServiceInstance serviceInstance = mock(ServiceInstance.class);
+        when(serviceInstance.getServiceName()).thenReturn(DemoService.class.getName());
+        when(serviceInstance.getMetadata()).thenReturn(metadata);
+        when(serviceInstance.getAddress()).thenReturn("127.0.0.1");
+
+        String expected = "org.apache.dubbo.registry.integration.DemoService##127.0.0.1##1";
+
+        Assertions.assertEquals(expected, MetadataUtils.computeKey(serviceInstance));
+    }
+
+
+    @Test
+    public void testCreateMetadataInvoker() {
+
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "1");
+        ServiceInstance serviceInstance = mock(ServiceInstance.class);
+        when(serviceInstance.getServiceName()).thenReturn(DemoService.class.getName());
+        when(serviceInstance.getMetadata()).thenReturn(metadata);
+        when(serviceInstance.getAddress()).thenReturn("127.0.0.1");
+        when(serviceInstance.getApplicationModel()).thenReturn(ApplicationModel.defaultModel());
+
+        String key = "org.apache.dubbo.registry.integration.DemoService##127.0.0.1##1";
+        Assertions.assertFalse(MetadataUtils.metadataServiceProxies.containsKey(key));
+        ApplicationConfig applicationConfig = new ApplicationConfig();
+        applicationConfig.setName("application1");
+        Map<String, String> parameters = new HashMap<>();
+        parameters.put("key1", "value1");
+        parameters.put("key2", "value2");
+        applicationConfig.setMetadataServicePort(7001);
+        applicationConfig.setParameters(parameters);
+
+        ApplicationModel applicationModel = spy(ApplicationModel.defaultModel());
+        applicationModel.getApplicationConfigManager().setApplication(applicationConfig);
+
+        Protocol protocol = mock(Protocol.class);
+        ProxyFactory proxyFactory = mock(ProxyFactory.class);
+        Invoker<Object> invoker = mock(Invoker.class);
+        MetadataService metadataService = mock(MetadataService.class);
+
+        when(serviceInstance.getOrDefaultApplicationModel()).thenReturn(applicationModel);
+
+        ExtensionLoader<Protocol> protocolExtensionLoader = mock(ExtensionLoader.class);
+        when(protocolExtensionLoader.getAdaptiveExtension()).thenReturn(protocol);
+
+        ExtensionLoader<ProxyFactory> proxyFactoryExtensionLoader = mock(ExtensionLoader.class);
+        when(proxyFactoryExtensionLoader.getAdaptiveExtension()).thenReturn(proxyFactory);
+
+        when(applicationModel.getExtensionLoader(Protocol.class)).thenReturn(protocolExtensionLoader);
+        when(applicationModel.getExtensionLoader(ProxyFactory.class)).thenReturn(proxyFactoryExtensionLoader);
+
+        when(protocol.refer(any(), any())).thenReturn(invoker);
+        when(proxyFactory.getProxy(invoker)).thenReturn(metadataService);
+
+        try (MockedStatic<ScopeModelUtil> scopeModelUtilMockedStatic = mockStatic(ScopeModelUtil.class)) {
+            scopeModelUtilMockedStatic
+                .when(() -> ScopeModelUtil.getApplicationModel(serviceInstance.getApplicationModel()))
+                .thenReturn(applicationModel);
+            MetadataUtils.getMetadataServiceProxy(serviceInstance);
+
+            Assertions.assertEquals(1, MetadataUtils.getMetadataServiceProxies().size());
+            Assertions.assertEquals(1, MetadataUtils.getMetadataServiceInvokers().size());
+            Assertions.assertEquals(metadataService, MetadataUtils.getMetadataServiceProxy(serviceInstance));
+        }
+
+        MetadataUtils.destroyMetadataServiceProxy(serviceInstance);
+        applicationModel.destroy();
+    }
+
+
+    @Test
+    public void testDestroyMetadataServiceProxy() {
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "1");
+        ServiceInstance serviceInstance = mock(ServiceInstance.class);
+        when(serviceInstance.getServiceName()).thenReturn(DemoService.class.getName());
+        when(serviceInstance.getMetadata()).thenReturn(metadata);
+        when(serviceInstance.getAddress()).thenReturn("127.0.0.1");
+        when(serviceInstance.getApplicationModel()).thenReturn(ApplicationModel.defaultModel());
+
+        String key = "org.apache.dubbo.registry.integration.DemoService##127.0.0.1##1";
+        Assertions.assertFalse(MetadataUtils.metadataServiceProxies.containsKey(key));
+        ApplicationConfig applicationConfig = new ApplicationConfig();
+        applicationConfig.setName("application1");
+        Map<String, String> parameters = new HashMap<>();
+        parameters.put("key1", "value1");
+        parameters.put("key2", "value2");
+        applicationConfig.setMetadataServicePort(7001);
+        applicationConfig.setParameters(parameters);
+
+        ApplicationModel applicationModel = spy(ApplicationModel.defaultModel());
+        applicationModel.getApplicationConfigManager().setApplication(applicationConfig);
+        when(serviceInstance.getOrDefaultApplicationModel()).thenReturn(applicationModel);
+
+        Protocol protocol = mock(Protocol.class);
+        ProxyFactory proxyFactory = mock(ProxyFactory.class);
+        Invoker<Object> invoker = mock(Invoker.class);
+        MetadataService metadataService = mock(MetadataService.class);
+
+        ExtensionLoader<Protocol> protocolExtensionLoader = mock(ExtensionLoader.class);
+        when(protocolExtensionLoader.getAdaptiveExtension()).thenReturn(protocol);
+
+        ExtensionLoader<ProxyFactory> proxyFactoryExtensionLoader = mock(ExtensionLoader.class);
+        when(proxyFactoryExtensionLoader.getAdaptiveExtension()).thenReturn(proxyFactory);
+
+        when(applicationModel.getExtensionLoader(Protocol.class)).thenReturn(protocolExtensionLoader);
+        when(applicationModel.getExtensionLoader(ProxyFactory.class)).thenReturn(proxyFactoryExtensionLoader);
+
+        when(protocol.refer(any(), any())).thenReturn(invoker);
+        when(proxyFactory.getProxy(invoker)).thenReturn(metadataService);
+
+        try (MockedStatic<ScopeModelUtil> scopeModelUtilMockedStatic = mockStatic(ScopeModelUtil.class)) {
+            scopeModelUtilMockedStatic
+                .when(() -> ScopeModelUtil.getApplicationModel(serviceInstance.getApplicationModel()))
+                .thenReturn(applicationModel);
+            MetadataUtils.getMetadataServiceProxy(serviceInstance);
+        }
+
+        MetadataUtils.destroyMetadataServiceProxy(serviceInstance);
+
+        Assertions.assertEquals(0, MetadataUtils.getMetadataServiceProxies().size());
+        Assertions.assertEquals(0, MetadataUtils.getMetadataServiceInvokers().size());
+        applicationModel.destroy();
+    }
+
+
+}
diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ProtocolPortsMetadataCustomizerTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ProtocolPortsMetadataCustomizerTest.java
index 5113448..b0417f0 100644
--- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ProtocolPortsMetadataCustomizerTest.java
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ProtocolPortsMetadataCustomizerTest.java
@@ -30,9 +30,11 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.AfterEach;
 import org.mockito.MockedStatic;
 import org.mockito.Mockito;
 
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -72,16 +74,21 @@ public class ProtocolPortsMetadataCustomizerTest {
         instance = createInstance();
         metadataService = mock(InMemoryWritableMetadataService.class);
 
-        URL duboURL = URL.valueOf("dubbo://30.10.104.63:20880/org.apache.dubbo.demo.GreetingService?" +
+        URL dubboUrl = URL.valueOf("dubbo://30.10.104.63:20880/org.apache.dubbo.demo.GreetingService?" +
             "REGISTRY_CLUSTER=registry1&anyhost=true&application=demo-provider2&delay=5000&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=greeting&interface=org.apache.dubbo.demo.GreetingService&metadata-type=remote&methods=hello&pid=55805&release=&revision=1.0.0&service-name-mapping=true&side=provider&timeout=5000&timestamp=1630229110058&version=1.0.0");
         URL triURL = URL.valueOf("tri://30.10.104.63:50332/org.apache.dubbo.demo.GreetingService?" +
             "REGISTRY_CLUSTER=registry1&anyhost=true&application=demo-provider2&delay=5000&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=greeting&interface=org.apache.dubbo.demo.GreetingService&metadata-type=remote&methods=hello&pid=55805&release=&revision=1.0.0&service-name-mapping=true&side=provider&timeout=5000&timestamp=1630229110058&version=1.0.0");
         Set<URL> urls = new HashSet<>();
-        urls.add(duboURL);
+        urls.add(dubboUrl);
         urls.add(triURL);
         when(metadataService.getExportedServiceURLs()).thenReturn(urls);
     }
 
+    @AfterEach
+    public void tearDown() throws IOException {
+        Mockito.framework().clearInlineMocks();
+    }
+
     @Test
     public void test() {
         ProtocolPortsMetadataCustomizer customizer = new ProtocolPortsMetadataCustomizer();