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/07/29 07:43:30 UTC

[dubbo] branch 3.0 updated: test: Add testcase to check exported services (#8355)

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 5d9512a  test: Add testcase to check exported services (#8355)
5d9512a is described below

commit 5d9512a9a8e736be52261e596a0e38f8ac344baf
Author: Xiong, Pin <pi...@foxmail.com>
AuthorDate: Thu Jul 29 02:42:54 2021 -0500

    test: Add testcase to check exported services (#8355)
    
    1. Use SPI to load ServiceListener
    2. Check if MetadataService exported successfully
    3. Check if ServiceConfig exported successfully
---
 ...RegistryCenterDubboProtocolIntegrationTest.java | 153 +++++++++++++--------
 .../single/listener/ExportedServiceListener.java   |  61 ++++++++
 .../org.apache.dubbo.config.ServiceListener        |   1 +
 3 files changed, 161 insertions(+), 54 deletions(-)

diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/SingleRegistryCenterDubboProtocolIntegrationTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/SingleRegistryCenterDubboProtocolIntegrationTest.java
index dcd4a61..079b040 100644
--- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/SingleRegistryCenterDubboProtocolIntegrationTest.java
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/SingleRegistryCenterDubboProtocolIntegrationTest.java
@@ -18,14 +18,18 @@ package org.apache.dubbo.integration.single;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.config.RegistryConfig;
 import org.apache.dubbo.config.ServiceConfig;
 import org.apache.dubbo.config.ReferenceConfig;
 import org.apache.dubbo.config.ApplicationConfig;
 import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.ServiceListener;
 import org.apache.dubbo.config.bootstrap.DubboBootstrap;
 import org.apache.dubbo.integration.IntegrationTest;
+import org.apache.dubbo.integration.single.listener.ExportedServiceListener;
 import org.apache.dubbo.metadata.MetadataInfo;
+import org.apache.dubbo.metadata.MetadataService;
 import org.apache.dubbo.metadata.WritableMetadataService;
 import org.apache.dubbo.registry.ListenerRegistryWrapper;
 import org.apache.dubbo.registry.Registry;
@@ -91,6 +95,11 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
      */
     private SingleRegistryCenterIntegrationService singleRegistryCenterIntegrationService;
 
+    /**
+     * Define the {@link ExportedServiceListener} instance to obtain the exported services.
+     */
+    private ExportedServiceListener exportedServiceListener;
+
     @BeforeEach
     public void setUp() throws Exception {
         logger.info(getClass().getSimpleName() + " testcase is beginning...");
@@ -129,12 +138,15 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
 
     /**
      * There are some checkpoints needed to check as follow :
-     * <li>ZookeeperServer's status</li>
-     * <li>ServiceConfig is exported or not</li>
-     * <li>ServiceConfig's exportedUrl has values or not</li>
-     * <li>DubboBootstrap is initialized or not</li>
-     * <li>DubboBootstrap is started or not</li>
-     * <li>DubboBootstrap is shutdown or not</li>
+     * <ul>
+     *     <li>ZookeeperServer's status</li>
+     *     <li>ServiceConfig is exported or not</li>
+     *     <li>ServiceConfig's exportedUrl has values or not</li>
+     *     <li>DubboBootstrap is initialized or not</li>
+     *     <li>DubboBootstrap is started or not</li>
+     *     <li>DubboBootstrap is shutdown or not</li>
+     *     <li>The ServiceListener is loaded by SPI or not</li>
+     * </ul>
      */
     private void beforeExport() {
         // ZookeeperServer's status
@@ -149,32 +161,37 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
         Assertions.assertFalse(DubboBootstrap.getInstance().isStarted());
         // DubboBootstrap is shutdown or not
         Assertions.assertFalse(DubboBootstrap.getInstance().isShutdown());
+        // The ServiceListener is loaded by SPI or not
+        Assertions.assertNull(exportedServiceListener);
     }
 
     /**
      * There are some checkpoints needed to check as follow :
-     * <li>DubboBootstrap is initialized or not</li>
-     * <li>DubboBootstrap is started or not</li>
-     * <li>DubboBootstrap is shutdown or not</li>
-     * <li>Service has been exported or not</li>
-     * <li>There is exported urls or not</li>
-     * <li>Protocol name is right or not</li>
-     * <li>Protocol port is right or not</li>
-     * <li>ServiceDiscoveryRegistry's protocol is right or not</li>
-     * <li>ServiceDiscoveryRegistry is destroy or not</li>
-     * <li>Registered service in registry center is right or not</li>
-     * <li>Exported url is right or not in InMemoryWritableMetadataService</li>
-     * <li>MetadataInfo exists or not in InMemoryWritableMetadataService</li>
-     * <li>MetadataInfo has reported or not</li>
-     * <li>MetadataInfo has reported or not has service or not</li>
-     * <li>MetadataInfo's application name is right or not</li>
-     * <li>MetadataInfo's service exists or not</li>
-     * <li>The name of MetadataInfo's service is right or not</li>
-     * <li>The group of MetadataInfo's service is right or not</li>
-     * <li>The version of MetadataInfo's service is right or not</li>
-     * <li>The protocol of MetadataInfo's service is right or not</li>
-     * <li>The serviceKey of MetadataInfo's service is right or not</li>
-     * <li>The matchKey of MetadataInfo's service is right or not</li>
+     * <ul>
+     *     <li>DubboBootstrap is initialized or not</li>
+     *     <li>DubboBootstrap is started or not</li>
+     *     <li>DubboBootstrap is shutdown or not</li>
+     *     <li>Service has been exported or not</li>
+     *     <li>There is exported urls or not</li>
+     *     <li>Protocol name is right or not</li>
+     *     <li>Protocol port is right or not</li>
+     *     <li>ServiceDiscoveryRegistry's protocol is right or not</li>
+     *     <li>ServiceDiscoveryRegistry is destroy or not</li>
+     *     <li>Registered service in registry center is right or not</li>
+     *     <li>Exported url is right or not in InMemoryWritableMetadataService</li>
+     *     <li>MetadataInfo exists or not in InMemoryWritableMetadataService</li>
+     *     <li>MetadataInfo has reported or not</li>
+     *     <li>MetadataInfo has reported or not has service or not</li>
+     *     <li>MetadataInfo's application name is right or not</li>
+     *     <li>MetadataInfo's service exists or not</li>
+     *     <li>The name of MetadataInfo's service is right or not</li>
+     *     <li>The group of MetadataInfo's service is right or not</li>
+     *     <li>The version of MetadataInfo's service is right or not</li>
+     *     <li>The protocol of MetadataInfo's service is right or not</li>
+     *     <li>The serviceKey of MetadataInfo's service is right or not</li>
+     *     <li>The matchKey of MetadataInfo's service is right or not</li>
+     *     <li>The exported service are right or not</li>
+     * </ul>
      */
     private void afterExport() {
         // DubboBootstrap is initialized or not
@@ -234,17 +251,38 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
         // MetadataInfo's service exists or not
         Assertions.assertNotNull(serviceInfo);
         // The name of MetadataInfo's service is right or not
-        Assertions.assertEquals(serviceInfo.getName(),SingleRegistryCenterIntegrationService.class.getName());
+        Assertions.assertEquals(serviceInfo.getName(), SingleRegistryCenterIntegrationService.class.getName());
         // The group of MetadataInfo's service is right or not
         Assertions.assertNull(serviceInfo.getGroup());
         // The version of MetadataInfo's service is right or not
         Assertions.assertNull(serviceInfo.getVersion());
         // The protocol of MetadataInfo's service is right or not
-        Assertions.assertEquals(serviceInfo.getProtocol(),PROTOCOL_NAME);
+        Assertions.assertEquals(serviceInfo.getProtocol(), PROTOCOL_NAME);
         // The serviceKey of MetadataInfo's service is right or not
-        Assertions.assertEquals(serviceInfo.getServiceKey(),SingleRegistryCenterIntegrationService.class.getName());
+        Assertions.assertEquals(serviceInfo.getServiceKey(), SingleRegistryCenterIntegrationService.class.getName());
         // The matchKey of MetadataInfo's service is right or not
-        Assertions.assertEquals(serviceInfo.getMatchKey(),key);
+        Assertions.assertEquals(serviceInfo.getMatchKey(), key);
+        // The exported services are right or not
+        // 1. The exported service must contain SingleRegistryCenterIntegrationService and MetadataService
+        // 2. The exported service's interface must be SingleRegistryCenterIntegrationService.class
+        // 3. All exported service must be exported
+        exportedServiceListener = (ExportedServiceListener) ExtensionLoader.getExtensionLoader(ServiceListener.class).getExtension("exported");
+        Assertions.assertNotNull(exportedServiceListener);
+        Assertions.assertEquals(exportedServiceListener.getExportedServices().size(), 2);
+        ServiceConfig singleRegistryCenterServiceConfig = null;
+        ServiceConfig metadataServiceServiceConfig = null;
+        for (ServiceConfig exportedServiceConfig: exportedServiceListener.getExportedServices()) {
+            if(exportedServiceConfig.getInterfaceClass() == SingleRegistryCenterIntegrationService.class){
+                singleRegistryCenterServiceConfig = exportedServiceConfig;
+            }
+            if(exportedServiceConfig.getInterfaceClass() == MetadataService.class){
+                metadataServiceServiceConfig = exportedServiceConfig;
+            }
+        }
+        Assertions.assertNotNull(singleRegistryCenterServiceConfig);
+        Assertions.assertNotNull(metadataServiceServiceConfig);
+        Assertions.assertTrue(singleRegistryCenterServiceConfig.isExported());
+        Assertions.assertTrue(metadataServiceServiceConfig.isExported());
     }
 
     /**
@@ -274,7 +312,7 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
     /**
      * Initialize the consumer.
      */
-    private void initConsumer(){
+    private void initConsumer() {
         referenceConfig = new ReferenceConfig<>();
         referenceConfig.setInterface(SingleRegistryCenterIntegrationService.class);
         referenceConfig.setBootstrap(DubboBootstrap.getInstance());
@@ -287,29 +325,33 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
 
     /**
      * There are some checkpoints needed to check before referring as follow :
-     * <li>ReferenceConfig has integrated into DubboBootstrap or not</li>
+     * <ul>
+     *     <li>ReferenceConfig has integrated into DubboBootstrap or not</li>
+     * </ul>
      */
-    private void beforeRefer(){
+    private void beforeRefer() {
         // ReferenceConfig has integrated into DubboBootstrap or not
-        Assertions.assertEquals(referenceConfig.getBootstrap(),DubboBootstrap.getInstance());
+        Assertions.assertEquals(referenceConfig.getBootstrap(), DubboBootstrap.getInstance());
     }
 
     /**
      * There are some checkpoints needed to check after referred as follow :
-     * <li>SingleRegistryCenterIntegrationService instance can't be null</li>
-     * <li>RPC works well or not</li>
-     * <li>Invoker is right or not</li>
-     * <li>Directory is null or not</li>
-     * <li>Registered interface is right or not</li>
-     * <li>Directory is available or not</li>
-     * <li>Directory is destroyed or not</li>
-     * <li>Directory has received notification or not</li>
-     * <li>ServiceDiscoveryRegistryDirectory should register or not</li>
-     * <li>ServiceDiscoveryRegistryDirectory's registered consumer url is right or not</li>
-     * <li>ServiceDiscoveryRegistryDirectory's registry is right or not</li>
-     * <li>Directory's invokers are right or not</li>
+     * <ul>
+     *     <li>SingleRegistryCenterIntegrationService instance can't be null</li>
+     *     <li>RPC works well or not</li>
+     *     <li>Invoker is right or not</li>
+     *     <li>Directory is null or not</li>
+     *     <li>Registered interface is right or not</li>
+     *     <li>Directory is available or not</li>
+     *     <li>Directory is destroyed or not</li>
+     *     <li>Directory has received notification or not</li>
+     *     <li>ServiceDiscoveryRegistryDirectory should register or not</li>
+     *     <li>ServiceDiscoveryRegistryDirectory's registered consumer url is right or not</li>
+     *     <li>ServiceDiscoveryRegistryDirectory's registry is right or not</li>
+     *     <li>Directory's invokers are right or not</li>
+     * </ul>
      */
-    private void afterRefer(){
+    private void afterRefer() {
         // SingleRegistryCenterIntegrationService instance can't be null
         Assertions.assertNotNull(singleRegistryCenterIntegrationService);
         // Invoker is right or not
@@ -319,13 +361,13 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
         Assertions.assertEquals("Hello Reference",
             singleRegistryCenterIntegrationService.hello("Reference"));
         // get ServiceDiscoveryRegistryDirectory instance
-        Directory directory = ((MigrationInvoker)referenceConfig.getInvoker()).getDirectory();
+        Directory directory = ((MigrationInvoker) referenceConfig.getInvoker()).getDirectory();
         // Directory is null or not
         Assertions.assertNotNull(directory);
         // Check Directory's type
         Assertions.assertTrue(directory instanceof ServiceDiscoveryRegistryDirectory);
         // Registered interface is right or not
-        Assertions.assertEquals(directory.getInterface(),SingleRegistryCenterIntegrationService.class);
+        Assertions.assertEquals(directory.getInterface(), SingleRegistryCenterIntegrationService.class);
         // Directory is available or not
         Assertions.assertTrue(directory.isAvailable());
         // Directory is destroyed or not
@@ -336,12 +378,12 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
         // ServiceDiscoveryRegistryDirectory should register or not
         Assertions.assertTrue(serviceDiscoveryRegistryDirectory.isShouldRegister());
         // ServiceDiscoveryRegistryDirectory's registered consumer url is right or not
-        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getRegisteredConsumerUrl().getCategory(),CONSUMERS_CATEGORY);
+        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getRegisteredConsumerUrl().getCategory(), CONSUMERS_CATEGORY);
         // ServiceDiscoveryRegistryDirectory's registry is right or not
         Assertions.assertTrue(serviceDiscoveryRegistryDirectory.getRegistry() instanceof ListenerRegistryWrapper);
         // Directory's invokers are right or not
-        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getAllInvokers().size(),1);
-        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getInvokers(),serviceDiscoveryRegistryDirectory.getAllInvokers());
+        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getAllInvokers().size(), 1);
+        Assertions.assertEquals(serviceDiscoveryRegistryDirectory.getInvokers(), serviceDiscoveryRegistryDirectory.getAllInvokers());
     }
 
     @AfterEach
@@ -352,6 +394,9 @@ public class SingleRegistryCenterDubboProtocolIntegrationTest implements Integra
         PROTOCOL_PORT = 0;
         serviceConfig = null;
         referenceConfig = null;
+        // The exported service has been unexported
+        Assertions.assertTrue(exportedServiceListener.getExportedServices().isEmpty());
+        exportedServiceListener = null;
         logger.info(getClass().getSimpleName() + " testcase is ending...");
         // destroy zookeeper only once
         logger.info(SingleZooKeeperServer.getZookeeperServerName() + " is beginning to shutdown...");
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/listener/ExportedServiceListener.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/listener/ExportedServiceListener.java
new file mode 100644
index 0000000..a8bf95b
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/integration/single/listener/ExportedServiceListener.java
@@ -0,0 +1,61 @@
+/*
+ * 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.integration.single.listener;
+
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.ServiceListener;
+import org.apache.dubbo.integration.single.SingleRegistryCenterIntegrationService;
+import org.apache.dubbo.metadata.MetadataService;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * This implementation of {@link ServiceListener} is to record exported services
+ */
+public class ExportedServiceListener implements ServiceListener {
+
+    private List<ServiceConfig> exportedServices = new ArrayList<>(2);
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void exported(ServiceConfig sc) {
+        //All exported services will be added
+        if(sc.getInterfaceClass()== MetadataService.class ||
+        sc.getInterfaceClass() == SingleRegistryCenterIntegrationService.class){
+            exportedServices.add(sc);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void unexported(ServiceConfig sc) {
+        //remove the exported services.
+        exportedServices.remove(sc);
+    }
+
+    /**
+     * Return all exported services.
+     */
+    public List<ServiceConfig> getExportedServices(){
+        return Collections.unmodifiableList(exportedServices);
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
index d7ce28b..3ba216b 100644
--- a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
+++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/org.apache.dubbo.config.ServiceListener
@@ -1 +1,2 @@
 mock=org.apache.dubbo.config.mock.MockServiceListener
+exported=org.apache.dubbo.integration.single.listener.ExportedServiceListener