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/09/21 15:46:50 UTC

[dubbo] branch 3.0 updated: Add unit test for MetadataReportInstance and AbstractServiceNameMapping (#8846)

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

albumenj 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 92ac457  Add unit test for MetadataReportInstance and AbstractServiceNameMapping (#8846)
92ac457 is described below

commit 92ac457fc41f52facf54b2cbdc54612f46ba8327
Author: 灼华 <43...@users.noreply.github.com>
AuthorDate: Tue Sep 21 23:46:41 2021 +0800

    Add unit test for MetadataReportInstance and AbstractServiceNameMapping (#8846)
---
 .../metadata/report/MetadataReportInstance.java    |   3 +-
 .../metadata/AbstractServiceNameMappingTest.java   | 198 +++++++++++++++++++++
 .../report/MetadataReportInstanceTest.java         |  85 +++++++++
 3 files changed, 284 insertions(+), 2 deletions(-)

diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
index d899956..ee40342 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
@@ -40,7 +40,7 @@ public class MetadataReportInstance {
     private final Map<String, MetadataReport> metadataReports = new HashMap<>();
 
     public void init(MetadataReportConfig config) {
-        if (init.get()) {
+        if (!init.compareAndSet(false, true)) {
             return;
         }
         ApplicationModel applicationModel = config.getApplicationModel();
@@ -63,7 +63,6 @@ public class MetadataReportInstance {
         if (metadataReport != null) {
             metadataReports.put(relatedRegistryId, metadataReport);
         }
-        init.set(true);
     }
 
     public Map<String, MetadataReport> getMetadataReports(boolean checked) {
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/AbstractServiceNameMappingTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/AbstractServiceNameMappingTest.java
new file mode 100644
index 0000000..2c974c8
--- /dev/null
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/AbstractServiceNameMappingTest.java
@@ -0,0 +1,198 @@
+/*
+ * 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.metadata;
+
+import org.apache.dubbo.common.URL;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDED_BY;
+import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY;
+
+/**
+ * @see AbstractServiceNameMapping
+ */
+class AbstractServiceNameMappingTest {
+
+    private MockServiceNameMapping mapping = new MockServiceNameMapping();
+    private MockWritableMetadataService writableMetadataService = new MockWritableMetadataService();
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        Field metadataService = mapping.getClass().getSuperclass().getDeclaredField("metadataService");
+        metadataService.setAccessible(true);
+        metadataService.set(mapping, writableMetadataService);
+    }
+
+    @Test
+    void testGetServices() {
+        URL url = URL.valueOf("dubbo://127.0.0.1:21880/" + AbstractServiceNameMappingTest.class);
+        url = url.addParameter(PROVIDED_BY, "app1,app2");
+        Set<String> services = mapping.getServices(url);
+        Assertions.assertTrue(services.contains("app1"));
+        Assertions.assertTrue(services.contains("app2"));
+
+        url = url.removeParameter(PROVIDED_BY);
+        services = mapping.getServices(url);
+        Assertions.assertTrue(services.contains("remote-app1"));
+        Assertions.assertTrue(services.contains("remote-app2"));
+
+
+        Map<String, Set<String>> cachedMapping = writableMetadataService.getCachedMapping();
+        Assertions.assertNotNull(cachedMapping);
+        Assertions.assertTrue(cachedMapping.containsKey(ServiceNameMapping.buildMappingKey(url)));
+        Assertions.assertIterableEquals(cachedMapping.get(ServiceNameMapping.buildMappingKey(url)), services);
+
+    }
+
+    @Test
+    public void testGetAndListener() {
+        URL url = URL.valueOf("dubbo://127.0.0.1:21880/" + AbstractServiceNameMappingTest.class);
+        URL registryURL = URL.valueOf("registry://127.0.0.1:7777/test");
+        registryURL = registryURL.addParameter(SUBSCRIBED_SERVICE_NAMES_KEY, "registry-app1");
+
+        Set<String> services = mapping.getAndListenServices(registryURL, url, null);
+        Assertions.assertTrue(services.contains("registry-app1"));
+
+        mapping.enabled = true;
+        services = mapping.getAndListenServices(registryURL, url, event -> {
+        });
+        Assertions.assertTrue(services.contains("remote-app3"));
+
+    }
+
+
+    private class MockServiceNameMapping extends AbstractServiceNameMapping {
+
+        public boolean enabled = false;
+
+        @Override
+        public Set<String> get(URL url) {
+            return new HashSet<>(Arrays.asList("remote-app1", "remote-app2"));
+        }
+
+        @Override
+        public Set<String> getAndListen(URL url, MappingListener mappingListener) {
+            if (!enabled) {
+                return Collections.emptySet();
+            }
+            return new HashSet<>(Arrays.asList("remote-app3"));
+        }
+
+        @Override
+        public boolean map(URL url) {
+            return false;
+        }
+    }
+
+    private class MockWritableMetadataService implements WritableMetadataService {
+        private final Map<String, Set<String>> serviceToAppsMapping = new HashMap<>();
+
+        @Override
+        public String serviceName() {
+            return null;
+        }
+
+        @Override
+        public SortedSet<String> getExportedURLs(String serviceInterface, String group, String version, String protocol) {
+            return null;
+        }
+
+        @Override
+        public String getServiceDefinition(String serviceKey) {
+            return null;
+        }
+
+        @Override
+        public MetadataInfo getMetadataInfo(String revision) {
+            return null;
+        }
+
+        @Override
+        public Map<String, MetadataInfo> getMetadataInfos() {
+            return null;
+        }
+
+        @Override
+        public boolean exportURL(URL url) {
+            return false;
+        }
+
+        @Override
+        public boolean unexportURL(URL url) {
+            return false;
+        }
+
+        @Override
+        public boolean subscribeURL(URL url) {
+            return false;
+        }
+
+        @Override
+        public boolean unsubscribeURL(URL url) {
+            return false;
+        }
+
+        @Override
+        public void publishServiceDefinition(URL url) {
+
+        }
+
+        @Override
+        public Set<String> getCachedMapping(String mappingKey) {
+            return serviceToAppsMapping.get(mappingKey);
+        }
+
+        @Override
+        public Set<String> getCachedMapping(URL consumerURL) {
+            String serviceKey = ServiceNameMapping.buildMappingKey(consumerURL);
+            return serviceToAppsMapping.get(serviceKey);
+        }
+
+        @Override
+        public Set<String> removeCachedMapping(String serviceKey) {
+            return serviceToAppsMapping.remove(serviceKey);
+        }
+
+        @Override
+        public void putCachedMapping(String serviceKey, Set<String> apps) {
+            serviceToAppsMapping.put(serviceKey, new TreeSet<>(apps));
+        }
+
+        @Override
+        public Map<String, Set<String>> getCachedMapping() {
+            return serviceToAppsMapping;
+        }
+
+        @Override
+        public MetadataInfo getDefaultMetadataInfo() {
+            return null;
+        }
+    }
+
+}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/MetadataReportInstanceTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/MetadataReportInstanceTest.java
new file mode 100644
index 0000000..94f27e4
--- /dev/null
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/MetadataReportInstanceTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.metadata.report;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.MetadataReportConfig;
+import org.apache.dubbo.config.context.ConfigManager;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+class MetadataReportInstanceTest {
+    private MetadataReportInstance metadataReportInstance;
+    private MetadataReportConfig metadataReportConfig;
+    private ConfigManager configManager;
+
+    private String registryId = "9103";
+
+    @BeforeEach
+    public void setUp() {
+        metadataReportInstance = new MetadataReportInstance();
+        configManager = mock(ConfigManager.class);
+        ApplicationModel applicationModel = spy(ApplicationModel.defaultModel());
+
+
+        URL url = URL.valueOf("metadata://127.0.0.1:20880/TestService?version=1.0.0&metadata=JTest");
+        metadataReportConfig = mock(MetadataReportConfig.class);
+        when(metadataReportConfig.getApplicationModel()).thenReturn(applicationModel);
+        when(metadataReportConfig.toUrl()).thenReturn(url);
+        when(metadataReportConfig.getScopeModel()).thenReturn(applicationModel);
+        when(metadataReportConfig.getRegistry()).thenReturn(registryId);
+
+        when(configManager.getMetadataConfigs()).thenReturn(Collections.emptyList());
+        when(applicationModel.getApplicationConfigManager()).thenReturn(configManager);
+        when(applicationModel.getCurrentConfig()).thenReturn(new ApplicationConfig("test"));
+
+    }
+
+    @Test
+    public void test() {
+        Assertions.assertThrows(IllegalStateException.class,
+            () -> metadataReportInstance.getMetadataReport(registryId),
+            "the metadata report was not initialized.");
+
+        Assertions.assertThrows(IllegalStateException.class,
+            () -> metadataReportInstance.getMetadataReports(true),
+            "the metadata report was not initialized.");
+
+
+        metadataReportInstance.init(metadataReportConfig);
+        MetadataReport metadataReport = metadataReportInstance.getMetadataReport(registryId);
+        Assertions.assertNotNull(metadataReport);
+
+        MetadataReport metadataReport2 = metadataReportInstance.getMetadataReport(registryId + "NOT_EXIST");
+        Assertions.assertEquals(metadataReport, metadataReport2);
+
+        Map<String, MetadataReport> metadataReports = metadataReportInstance.getMetadataReports(true);
+        Assertions.assertEquals(metadataReports.size(), 1);
+        Assertions.assertEquals(metadataReports.get(registryId), metadataReport);
+    }
+
+}