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/26 11:10:31 UTC

[dubbo] branch 3.0 updated: Add unit test for CuratorFrameworkUtils and AbstractServiceDiscoveryFactory (#8886)

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 35c2d7e  Add unit test for CuratorFrameworkUtils and AbstractServiceDiscoveryFactory (#8886)
35c2d7e is described below

commit 35c2d7ec7e8b56624c23954abfa1a209034d291e
Author: 灼华 <43...@users.noreply.github.com>
AuthorDate: Sun Sep 26 19:09:50 2021 +0800

    Add unit test for CuratorFrameworkUtils and AbstractServiceDiscoveryFactory (#8886)
---
 .../AbstractServiceDiscoveryFactoryTest.java       |  48 +++++++++
 .../client/support/MockServiceDiscovery.java       |  55 ++++++++++
 .../support/MockServiceDiscoveryFactory.java       |  28 ++++++
 ...e.dubbo.registry.client.ServiceDiscoveryFactory |   1 +
 ...g.apache.dubbo.registry.client.ServiceDiscovery |   3 +-
 .../zookeeper/util/CuratorFrameworkUtilsTest.java  | 111 +++++++++++++++++++++
 6 files changed, 245 insertions(+), 1 deletion(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactoryTest.java
new file mode 100644
index 0000000..fd97ac7
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/AbstractServiceDiscoveryFactoryTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.URL;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+/**
+ * {@link AbstractServiceDiscoveryFactory}
+ */
+public class AbstractServiceDiscoveryFactoryTest {
+
+    @Test
+    public void testGetServiceDiscoveryWithCache() {
+        URL url = URL.valueOf("mock://127.0.0.1:8888");
+        ServiceDiscoveryFactory factory = ServiceDiscoveryFactory.getExtension(url);
+        ServiceDiscovery serviceDiscovery1 = factory.getServiceDiscovery(url);
+        ServiceDiscovery serviceDiscovery2 = factory.getServiceDiscovery(url);
+        Assertions.assertEquals(serviceDiscovery1, serviceDiscovery2);
+
+        url = url.setPath("test");
+        ServiceDiscovery serviceDiscovery3 = factory.getServiceDiscovery(url);
+        Assertions.assertNotEquals(serviceDiscovery2, serviceDiscovery3);
+
+        AbstractServiceDiscoveryFactory abstractServiceDiscoveryFactory = (AbstractServiceDiscoveryFactory) factory;
+        List<ServiceDiscovery> allServiceDiscoveries = abstractServiceDiscoveryFactory.getAllServiceDiscoveries();
+        Assertions.assertEquals(allServiceDiscoveries.size(), 2);
+        Assertions.assertTrue(allServiceDiscoveries.contains(serviceDiscovery1));
+        Assertions.assertTrue(allServiceDiscoveries.contains(serviceDiscovery3));
+    }
+}
diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscovery.java
new file mode 100644
index 0000000..f225608
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscovery.java
@@ -0,0 +1,55 @@
+/*
+ * 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.support;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
+import org.apache.dubbo.registry.client.ServiceInstance;
+
+import java.util.Set;
+
+public class MockServiceDiscovery extends AbstractServiceDiscovery {
+    @Override
+    public void doInitialize(URL registryURL) throws Exception {
+
+    }
+
+    @Override
+    public void doRegister(ServiceInstance serviceInstance) throws RuntimeException {
+
+    }
+
+    @Override
+    public void doUpdate(ServiceInstance serviceInstance) throws RuntimeException {
+
+    }
+
+    @Override
+    public void doUnregister(ServiceInstance serviceInstance) {
+
+    }
+
+    @Override
+    public void doDestroy() throws Exception {
+
+    }
+
+    @Override
+    public Set<String> getServices() {
+        return null;
+    }
+}
diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscoveryFactory.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscoveryFactory.java
new file mode 100644
index 0000000..fa407c2
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/support/MockServiceDiscoveryFactory.java
@@ -0,0 +1,28 @@
+/*
+ * 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.support;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.registry.client.AbstractServiceDiscoveryFactory;
+import org.apache.dubbo.registry.client.ServiceDiscovery;
+
+public class MockServiceDiscoveryFactory extends AbstractServiceDiscoveryFactory {
+    @Override
+    protected ServiceDiscovery createDiscovery(URL registryURL) {
+        return new MockServiceDiscovery();
+    }
+}
diff --git a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.registry.client.ServiceDiscoveryFactory b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.registry.client.ServiceDiscoveryFactory
new file mode 100644
index 0000000..c4cffd6
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/dubbo/org.apache.dubbo.registry.client.ServiceDiscoveryFactory
@@ -0,0 +1 @@
+mock=org.apache.dubbo.registry.client.support.MockServiceDiscoveryFactory
diff --git a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery
index 53725cb..60edf7f 100644
--- a/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery
+++ b/dubbo-registry/dubbo-registry-api/src/test/resources/META-INF/services/org.apache.dubbo.registry.client.ServiceDiscovery
@@ -1 +1,2 @@
-in-memory=org.apache.dubbo.registry.client.InMemoryServiceDiscovery
\ No newline at end of file
+in-memory=org.apache.dubbo.registry.client.InMemoryServiceDiscovery
+mock=org.apache.dubbo.registry.client.support.MockServiceDiscovery
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtilsTest.java b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtilsTest.java
new file mode 100644
index 0000000..6b2d9d5
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtilsTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.zookeeper.util;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.test.TestingServer;
+import org.apache.curator.x.discovery.ServiceDiscovery;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.registry.client.DefaultServiceInstance;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import org.apache.dubbo.registry.zookeeper.ZookeeperInstance;
+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.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
+import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_STORAGE_TYPE_PROPERTY_NAME;
+import static org.apache.dubbo.registry.zookeeper.util.CuratorFrameworkParams.ROOT_PATH;
+
+/**
+ * {@link CuratorFrameworkUtils} Test
+ */
+class CuratorFrameworkUtilsTest {
+    private static TestingServer zkServer;
+    private static URL registryUrl;
+
+    @BeforeAll
+    public static void init() throws Exception {
+        int zkServerPort = getAvailablePort();
+        zkServer = new TestingServer(zkServerPort, true);
+        zkServer.start();
+
+        registryUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort);
+        registryUrl.setScopeModel(ApplicationModel.defaultModel());
+    }
+
+    @AfterAll
+    public static void tearDown() throws Exception {
+        zkServer.stop();
+    }
+
+
+    @Test
+    void testBuildCuratorFramework() throws Exception {
+        CuratorFramework curatorFramework = CuratorFrameworkUtils.buildCuratorFramework(registryUrl);
+        Assertions.assertNotNull(curatorFramework);
+        Assertions.assertTrue(curatorFramework.getZookeeperClient().isConnected());
+        curatorFramework.getZookeeperClient().close();
+    }
+
+    @Test
+    void testBuildServiceDiscovery() throws Exception {
+        CuratorFramework curatorFramework = CuratorFrameworkUtils.buildCuratorFramework(registryUrl);
+        ServiceDiscovery<ZookeeperInstance> discovery = CuratorFrameworkUtils.buildServiceDiscovery(curatorFramework, ROOT_PATH.getParameterValue(registryUrl));
+        Assertions.assertNotNull(discovery);
+        discovery.close();
+        curatorFramework.getZookeeperClient().close();
+    }
+
+    @Test
+    void testBuild() {
+        ServiceInstance dubboServiceInstance = new DefaultServiceInstance("A", "127.0.0.1", 8888, ApplicationModel.defaultModel());
+        Map<String, String> metadata = dubboServiceInstance.getMetadata();
+        metadata.put(METADATA_STORAGE_TYPE_PROPERTY_NAME, "remote");
+        metadata.put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, "111");
+        metadata.put("site", "dubbo");
+
+        // convert {org.apache.dubbo.registry.client.ServiceInstance} to {org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance>}
+        org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> curatorServiceInstance = CuratorFrameworkUtils.build(dubboServiceInstance);
+        Assertions.assertEquals(curatorServiceInstance.getId(), dubboServiceInstance.getAddress());
+        Assertions.assertEquals(curatorServiceInstance.getName(), dubboServiceInstance.getServiceName());
+        Assertions.assertEquals(curatorServiceInstance.getAddress(), dubboServiceInstance.getHost());
+        Assertions.assertEquals(curatorServiceInstance.getPort(), dubboServiceInstance.getPort());
+
+        ZookeeperInstance payload = curatorServiceInstance.getPayload();
+        Assertions.assertNotNull(payload);
+        Assertions.assertEquals(payload.getMetadata(), metadata);
+        Assertions.assertEquals(payload.getName(), dubboServiceInstance.getServiceName());
+
+        // convert {org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance>} to {org.apache.dubbo.registry.client.ServiceInstance}
+        ServiceInstance serviceInstance = CuratorFrameworkUtils.build(registryUrl, curatorServiceInstance);
+        Assertions.assertEquals(serviceInstance, dubboServiceInstance);
+
+        // convert {Collection<org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance>>} to {List<org.apache.dubbo.registry.client.ServiceInstance>}
+        List<ServiceInstance> serviceInstances = CuratorFrameworkUtils.build(registryUrl, Arrays.asList(curatorServiceInstance));
+        Assertions.assertNotNull(serviceInstances);
+        Assertions.assertEquals(serviceInstances.get(0), dubboServiceInstance);
+    }
+
+}