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/25 03:30:39 UTC

[dubbo] branch 3.0 updated: Add test case for ServiceInstanceMetadataUtilsTest (#8914)

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 5fb4a50  Add test case for ServiceInstanceMetadataUtilsTest (#8914)
5fb4a50 is described below

commit 5fb4a50add69c4bcf40a41f22d32f6ff72aa45f7
Author: 灼华 <43...@users.noreply.github.com>
AuthorDate: Sat Sep 25 11:30:17 2021 +0800

    Add test case for ServiceInstanceMetadataUtilsTest (#8914)
    
    * Add test case for ServiceInstanceMetadataUtilsTest
    
    * ApplicationModel reset
---
 .../metadata/ServiceInstanceMetadataUtilsTest.java | 195 ++++++++++++++++++---
 1 file changed, 170 insertions(+), 25 deletions(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtilsTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtilsTest.java
index 0c208af..70c020c 100644
--- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtilsTest.java
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtilsTest.java
@@ -16,16 +16,44 @@
  */
 package org.apache.dubbo.registry.client.metadata;
 
-import org.apache.dubbo.common.URL;
-
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.metadata.MetadataInfo;
+import org.apache.dubbo.metadata.MetadataService;
+import org.apache.dubbo.metadata.WritableMetadataService;
+import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.client.DefaultServiceInstance;
+import org.apache.dubbo.registry.client.InMemoryServiceDiscovery;
+import org.apache.dubbo.registry.client.ServiceDiscovery;
+import org.apache.dubbo.registry.client.ServiceDiscoveryRegistry;
+import org.apache.dubbo.registry.support.RegistryManager;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
+import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.INSTANCE_REVISION_UPDATED_KEY;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_CLUSTER_PROPERTY_NAME;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_STORAGE_TYPE_PROPERTY_NAME;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
@@ -41,6 +69,25 @@ public class ServiceInstanceMetadataUtilsTest {
     private static final String VALUE_URL = "{\"version\":\"1.0.0\",\"dubbo\":\"2.0.2\",\"release\":\"2.7.1\",\"port\":\"20880\",\"protocol\":\"dubbo\"}";
     private static final String VALUE_URL2 = "{\"version\":\"1.0.0\",\"dubbo\":\"2.0.2\",\"release\":\"2.7.1\",\"port\":\"20880\",\"protocol\":\"rest\"}";
 
+    private DefaultServiceInstance serviceInstance;
+
+    @BeforeEach
+    public void init() {
+        serviceInstance = new DefaultServiceInstance("test", "127.0.0.1", 8080, ApplicationModel.defaultModel());
+    }
+
+    @BeforeAll
+    public static void setUp() {
+        ApplicationConfig applicationConfig = new ApplicationConfig("demo");
+        ApplicationModel.defaultModel().getApplicationConfigManager().setApplication(applicationConfig);
+    }
+
+    @AfterAll
+    public static void clearUp() {
+        ApplicationModel.reset();
+    }
+
+
     @Test
     public void testMetadataServiceURLParameters() {
 
@@ -65,27 +112,125 @@ public class ServiceInstanceMetadataUtilsTest {
     }
 
     @Test
-    public void testProtocolPorts() {
-
-//        Map<String, String> metadata = new LinkedHashMap<>();
-//
-//        String key = protocolPortMetadataKey("dubbo");
-//        assertEquals("dubbo.protocols.dubbo.port", key);
-//
-//        metadata.put(key, "20880");
-//
-//        key = protocolPortMetadataKey("rest");
-//        assertEquals("dubbo.protocols.rest.port", key);
-//
-//        metadata.put(key, "8080");
-//
-//        Map<String, Integer> protocolPorts = getProtocolPorts(metadata);
-//
-//        Map<String, Integer> expected = new LinkedHashMap<>();
-//
-//        expected.put("dubbo", 20880);
-//        expected.put("rest", 8080);
-//
-//        assertEquals(expected, protocolPorts);
+    public void getMetadataServiceURLsParams() {
+        Map<String, String> urlParams = new HashMap<>();
+        urlParams.put("dubbo", "1111");
+        urlParams.put("rest", "2222");
+        serviceInstance.getMetadata().put(METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME, new Gson().toJson(urlParams));
+        Map<String, String> metadataServiceURLsParams = ServiceInstanceMetadataUtils.getMetadataServiceURLsParams(serviceInstance);
+        Assertions.assertEquals(metadataServiceURLsParams.get("dubbo"), "1111");
+        Assertions.assertEquals(metadataServiceURLsParams.get("rest"), "2222");
+    }
+
+    @Test
+    public void testMetadataStorageType() {
+        Assertions.assertEquals(ServiceInstanceMetadataUtils.getMetadataStorageType(serviceInstance), DEFAULT_METADATA_STORAGE_TYPE);
+        serviceInstance.getMetadata().put(METADATA_STORAGE_TYPE_PROPERTY_NAME, REMOTE_METADATA_STORAGE_TYPE);
+        Assertions.assertEquals(ServiceInstanceMetadataUtils.getMetadataStorageType(serviceInstance), REMOTE_METADATA_STORAGE_TYPE);
+    }
+
+    @Test
+    public void getRemoteCluster() {
+        Assertions.assertNull(ServiceInstanceMetadataUtils.getRemoteCluster(serviceInstance));
+
+        serviceInstance.getMetadata().put(METADATA_CLUSTER_PROPERTY_NAME, "REGISTRY_CLUSTER_9103");
+        Assertions.assertEquals(ServiceInstanceMetadataUtils.getRemoteCluster(serviceInstance), "REGISTRY_CLUSTER_9103");
+    }
+
+    @Test
+    public void testInstanceUpdateKey() {
+        serviceInstance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
+        Assertions.assertTrue(ServiceInstanceMetadataUtils.isInstanceUpdated(serviceInstance));
+
+        ServiceInstanceMetadataUtils.resetInstanceUpdateKey(serviceInstance);
+        Assertions.assertFalse(ServiceInstanceMetadataUtils.isInstanceUpdated(serviceInstance));
+    }
+
+    @Test
+    public void testEndpoints() {
+        Assertions.assertFalse(ServiceInstanceMetadataUtils.hasEndpoints(serviceInstance));
+
+        Map<String, Integer> endpoints = new HashMap<>();
+        endpoints.put("dubbo", 20880);
+        endpoints.put("rest", 8080);
+        ServiceInstanceMetadataUtils.setEndpoints(serviceInstance, endpoints);
+        Assertions.assertTrue(ServiceInstanceMetadataUtils.hasEndpoints(serviceInstance));
+
+        for (Map.Entry<String, Integer> entry : endpoints.entrySet()) {
+            String protocol = entry.getKey();
+            Integer port = entry.getValue();
+            DefaultServiceInstance.Endpoint endpoint = ServiceInstanceMetadataUtils.getEndpoint(serviceInstance, protocol);
+            Assertions.assertEquals(endpoint.getPort(), port);
+        }
+    }
+
+    @Test
+    public void testCalInstanceRevision() {
+        URL url1 = URL.valueOf("test://127.0.0.1:8080/" + ServiceInstanceMetadataUtils.class.getName() + "?version=1.0.0");
+        URL url2 = URL.valueOf("test://127.0.0.1:8080/" + ServiceInstanceMetadataUtils.class.getName() + "?version=2.0.0");
+
+        ServiceDiscovery serviceDiscovery = Mockito.mock(ServiceDiscovery.class);
+
+        WritableMetadataService writableMetadataService = WritableMetadataService.getDefaultExtension(serviceInstance.getApplicationModel());
+        Map<String, MetadataInfo> metadataInfoMap = writableMetadataService.getMetadataInfos();
+        MetadataInfo metadataInfo = new MetadataInfo("demo");
+        metadataInfo.addService(new MetadataInfo.ServiceInfo(url1));
+        metadataInfoMap.put(DEFAULT_KEY, metadataInfo);
+
+        ServiceInstanceMetadataUtils.calInstanceRevision(serviceDiscovery, serviceInstance);
+        Assertions.assertEquals(metadataInfo.calAndGetRevision(), serviceInstance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME));
+        Assertions.assertNull(serviceInstance.getExtendParams().get(INSTANCE_REVISION_UPDATED_KEY));
+
+        metadataInfoMap.get(DEFAULT_KEY).addService(new MetadataInfo.ServiceInfo(url2));
+        ServiceInstanceMetadataUtils.calInstanceRevision(serviceDiscovery, serviceInstance);
+        Assertions.assertEquals(metadataInfo.calAndGetRevision(), serviceInstance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME));
+        Assertions.assertEquals(serviceInstance.getExtendParams().get(INSTANCE_REVISION_UPDATED_KEY), "true");
+    }
+
+    @Test
+    public void testRegisterMetadataAndInstance() throws Exception {
+        InMemoryServiceDiscovery inMemoryServiceDiscovery = prepare();
+        ServiceInstanceMetadataUtils.registerMetadataAndInstance(serviceInstance);
+
+        Assertions.assertTrue(inMemoryServiceDiscovery.getServices().contains(serviceInstance.getServiceName()));
+    }
+
+    @Test
+    public void refreshMetadataAndInstance() throws Exception {
+        InMemoryServiceDiscovery inMemoryServiceDiscovery = prepare();
+
+        Assertions.assertNull(inMemoryServiceDiscovery.getLocalInstance());
+
+        ServiceInstanceMetadataUtils.refreshMetadataAndInstance(serviceInstance);
+        Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance(), serviceInstance);
+
+    }
+
+    private InMemoryServiceDiscovery prepare() throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchFieldException {
+
+
+
+        WritableMetadataService writableMetadataService = WritableMetadataService.getDefaultExtension(ApplicationModel.defaultModel());
+        // Prevent NPE  when calling the refreshMetadataAndInstance method (customizeInstance -> MetadataServiceURLParamsMetadataCustomizer.customize)
+        URL metadataURL = URL.valueOf("dubbo://127.0.0.1:8080/" + MetadataService.class);
+        writableMetadataService.setMetadataServiceURL(metadataURL);
+
+
+        // Construct serviceDiscoveryRegistry
+        InMemoryServiceDiscovery inMemoryServiceDiscovery = new InMemoryServiceDiscovery();
+        URL registryURL = URL.valueOf("registry://localhost:2181?registry=zookeeper");
+        Constructor<ServiceDiscoveryRegistry> constructor = ServiceDiscoveryRegistry.class.getDeclaredConstructor(URL.class, ServiceDiscovery.class, WritableMetadataService.class);
+        constructor.setAccessible(true);
+        ServiceDiscoveryRegistry serviceDiscoveryRegistry = constructor.newInstance(registryURL, inMemoryServiceDiscovery, writableMetadataService);
+
+        // Add serviceDiscoveryRegistry to RegisterManger
+        RegistryManager manager = ApplicationModel.defaultModel().getBeanFactory().getBean(RegistryManager.class);
+        Field field = manager.getClass().getDeclaredField("registries");
+        field.setAccessible(true);
+        Map<String, Registry> registries = new ConcurrentHashMap<>();
+        registries.put("127.0.0.1:2181", serviceDiscoveryRegistry);
+        field.set(manager, registries);
+        return inMemoryServiceDiscovery;
     }
+
 }