You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by vi...@apache.org on 2019/08/30 08:26:29 UTC
[dubbo] branch cloud-native updated: unit test for metadata
This is an automated email from the ASF dual-hosted git repository.
victory pushed a commit to branch cloud-native
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/cloud-native by this push:
new 0bd3a97 unit test for metadata
0bd3a97 is described below
commit 0bd3a97bdb61bd083d7310108a16ae12288283e9
Author: cvictory <sh...@gmail.com>
AuthorDate: Fri Aug 30 16:25:42 2019 +0800
unit test for metadata
---
.../metadata/store/etcd/EtcdMetadataReport.java | 6 +-
.../store/etcd/EtcdMetadataReportTest.java | 125 ++++++++++++++++++++-
.../zookeeper/ZookeeperMetadataReportTest.java | 118 +++++++++++++++++++
.../registry/etcd/EtcdServiceDiscoveryTest.java | 8 ++
4 files changed, 253 insertions(+), 4 deletions(-)
diff --git a/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java b/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java
index 1e97927..a80c6e8 100644
--- a/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java
+++ b/dubbo-metadata/dubbo-metadata-report-etcd/src/main/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReport.java
@@ -122,12 +122,12 @@ public class EtcdMetadataReport extends AbstractMetadataReport {
@Override
protected String doGetSubscribedURLs(SubscriberMetadataIdentifier subscriberMetadataIdentifier) {
- throw new UnsupportedOperationException("This extension does not support working as a remote metadata center.");
+ return etcdClient.getKVValue(getNodeKey(subscriberMetadataIdentifier));
}
@Override
- public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) {
- throw new UnsupportedOperationException("This extension does not support working as a remote metadata center.");
+ public String getServiceDefinition(MetadataIdentifier metadataIdentifier) {
+ return etcdClient.getKVValue(getNodeKey(metadataIdentifier));
}
private void storeMetadata(MetadataIdentifier identifier, String v) {
diff --git a/dubbo-metadata/dubbo-metadata-report-etcd/src/test/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-etcd/src/test/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReportTest.java
index c6c9843..a502b97 100644
--- a/dubbo-metadata/dubbo-metadata-report-etcd/src/test/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReportTest.java
+++ b/dubbo-metadata/dubbo-metadata-report-etcd/src/test/java/org/apache/dubbo/metadata/store/etcd/EtcdMetadataReportTest.java
@@ -22,6 +22,8 @@ import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
+import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
+import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
import com.google.gson.Gson;
import io.etcd.jetcd.ByteSequence;
@@ -37,10 +39,12 @@ import org.junit.jupiter.api.Test;
import java.net.URI;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
@@ -74,11 +78,13 @@ public class EtcdMetadataReportTest {
}
@Test
- @Disabled("Disabled because https://github.com/apache/dubbo/issues/4185")
public void testStoreProvider() throws Exception {
String version = "1.0.0";
String group = null;
String application = "etcd-metdata-report-test";
+
+ String r = etcdMetadataReport.getServiceDefinition(new MetadataIdentifier(TEST_SERVICE, version, group, "provider", application));
+ Assertions.assertNull(r);
MetadataIdentifier providerIdentifier =
storeProvider(etcdMetadataReport, TEST_SERVICE, version, group, application);
@@ -90,6 +96,9 @@ public class EtcdMetadataReportTest {
Gson gson = new Gson();
FullServiceDefinition fullServiceDefinition = gson.fromJson(fileContent, FullServiceDefinition.class);
Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "etcdTest");
+
+ r = etcdMetadataReport.getServiceDefinition(new MetadataIdentifier(TEST_SERVICE, version, group, "provider", application));
+ Assertions.assertNotNull(r);
}
@Test
@@ -106,6 +115,113 @@ public class EtcdMetadataReportTest {
Assertions.assertEquals(fileContent, "{\"paramConsumerTest\":\"etcdConsumer\"}");
}
+ @Test
+ public void testDoSaveMetadata() throws ExecutionException, InterruptedException {
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(TEST_SERVICE, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, version,
+ group, "provider", revision, protocol);
+ etcdMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+
+ CompletableFuture<GetResponse> response = etcdClientForTest.getKVClient().get(ByteSequence.from(
+ etcdMetadataReport.getNodeKey(serviceMetadataIdentifier), StandardCharsets.UTF_8));
+ String fileContent = response.get().getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, URL.encode(url.toFullString()));
+ }
+
+ @Test
+ public void testDoRemoveMetadata() throws ExecutionException, InterruptedException {
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(TEST_SERVICE, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, version,
+ group, "provider", revision, protocol);
+ etcdMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+ CompletableFuture<GetResponse> response = etcdClientForTest.getKVClient().get(ByteSequence.from(
+ etcdMetadataReport.getNodeKey(serviceMetadataIdentifier), StandardCharsets.UTF_8));
+ String fileContent = response.get().getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);
+ Assertions.assertNotNull(fileContent);
+
+
+ etcdMetadataReport.doRemoveMetadata(serviceMetadataIdentifier);
+
+ response = etcdClientForTest.getKVClient().get(ByteSequence.from(
+ etcdMetadataReport.getNodeKey(serviceMetadataIdentifier), StandardCharsets.UTF_8));
+ Assertions.assertTrue(response.get().getKvs().isEmpty());
+ }
+
+ @Test
+ public void testDoGetExportedURLs() throws ExecutionException, InterruptedException {
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(TEST_SERVICE, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, version,
+ group, "provider", revision, protocol);
+ etcdMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+
+ List<String> r = etcdMetadataReport.doGetExportedURLs(serviceMetadataIdentifier);
+ Assertions.assertTrue(r.size() == 1);
+
+ String fileContent = r.get(0);
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, url.toFullString());
+ }
+
+ @Test
+ public void testDoSaveSubscriberData() throws ExecutionException, InterruptedException {
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(TEST_SERVICE, version, group, application);
+ SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(application, revision);
+ Gson gson = new Gson();
+ String r = gson.toJson(Arrays.asList(url));
+ etcdMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);
+
+ CompletableFuture<GetResponse> response = etcdClientForTest.getKVClient().get(ByteSequence.from(
+ etcdMetadataReport.getNodeKey(subscriberMetadataIdentifier), StandardCharsets.UTF_8));
+ String fileContent = response.get().getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, r);
+ }
+
+ @Test
+ public void testDoGetSubscribedURLs() throws ExecutionException, InterruptedException {
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(TEST_SERVICE, version, group, application);
+ SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(application, revision);
+ Gson gson = new Gson();
+ String r = gson.toJson(Arrays.asList(url));
+ etcdMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);
+
+ CompletableFuture<GetResponse> response = etcdClientForTest.getKVClient().get(ByteSequence.from(
+ etcdMetadataReport.getNodeKey(subscriberMetadataIdentifier), StandardCharsets.UTF_8));
+ String fileContent = etcdMetadataReport.doGetSubscribedURLs(subscriberMetadataIdentifier);
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, r);
+ }
+
private MetadataIdentifier storeProvider(EtcdMetadataReport etcdMetadataReport, String interfaceName, String version,
String group, String application)
throws ClassNotFoundException, InterruptedException {
@@ -124,6 +240,13 @@ public class EtcdMetadataReportTest {
return providerMetadataIdentifier;
}
+ private URL generateURL(String interfaceName, String version, String group, String application) {
+ URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":8989/" + interfaceName +
+ "?paramTest=etcdTest&version=" + version + "&application="
+ + application + (group == null ? "" : "&group=" + group));
+ return url;
+ }
+
private MetadataIdentifier storeConsumer(EtcdMetadataReport etcdMetadataReport, String interfaceName,
String version, String group, String application) throws InterruptedException {
diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java
index 6b5d3b5..454f25e 100644
--- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java
+++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java
@@ -22,6 +22,8 @@ import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
+import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
+import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
import org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter;
import com.google.gson.Gson;
@@ -31,8 +33,11 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.concurrent.ExecutionException;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
@@ -120,6 +125,112 @@ public class ZookeeperMetadataReportTest {
Assertions.assertEquals(fileContent, "{\"paramConsumerTest\":\"zkCm\"}");
}
+ @Test
+ public void testDoSaveMetadata() throws ExecutionException, InterruptedException {
+ String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(interfaceName, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(interfaceName, version,
+ group, "provider", revision, protocol);
+ zookeeperMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+
+ String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(serviceMetadataIdentifier));
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, URL.encode(url.toFullString()));
+ }
+
+ @Test
+ public void testDoRemoveMetadata() throws ExecutionException, InterruptedException {
+ String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(interfaceName, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(interfaceName, version,
+ group, "provider", revision, protocol);
+ zookeeperMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+ String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(serviceMetadataIdentifier));
+
+ Assertions.assertNotNull(fileContent);
+
+
+ zookeeperMetadataReport.doRemoveMetadata(serviceMetadataIdentifier);
+
+ fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(serviceMetadataIdentifier));
+ Assertions.assertNull(fileContent);
+ }
+
+ @Test
+ public void testDoGetExportedURLs() throws ExecutionException, InterruptedException {
+ String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(interfaceName, version, group, application);
+ ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(interfaceName, version,
+ group, "provider", revision, protocol);
+ zookeeperMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
+
+ List<String> r = zookeeperMetadataReport.doGetExportedURLs(serviceMetadataIdentifier);
+ Assertions.assertTrue(r.size() == 1);
+
+ String fileContent = r.get(0);
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, url.toFullString());
+ }
+
+ @Test
+ public void testDoSaveSubscriberData() throws ExecutionException, InterruptedException {
+ String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(interfaceName, version, group, application);
+ SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(application, revision);
+ Gson gson = new Gson();
+ String r = gson.toJson(Arrays.asList(url));
+ zookeeperMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);
+
+ String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(subscriberMetadataIdentifier));
+
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, r);
+ }
+
+ @Test
+ public void testDoGetSubscribedURLs() throws ExecutionException, InterruptedException {
+ String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
+ String version = "1.0.0";
+ String group = null;
+ String application = "etc-metadata-report-consumer-test";
+ String revision = "90980";
+ String protocol = "xxx";
+ URL url = generateURL(interfaceName, version, group, application);
+ SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(application, revision);
+ Gson gson = new Gson();
+ String r = gson.toJson(Arrays.asList(url));
+ zookeeperMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);
+
+ String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(subscriberMetadataIdentifier));
+
+ Assertions.assertNotNull(fileContent);
+
+ Assertions.assertEquals(fileContent, r);
+ }
+
private MetadataIdentifier storePrivider(ZookeeperMetadataReport zookeeperMetadataReport, String interfaceName, String version, String group, String application) throws ClassNotFoundException, InterruptedException {
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + interfaceName + "?paramTest=zkTest&version=" + version + "&application="
@@ -156,4 +267,11 @@ public class ZookeeperMetadataReportTest {
}
return value;
}
+
+ private URL generateURL(String interfaceName, String version, String group, String application) {
+ URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":8989/" + interfaceName +
+ "?paramTest=etcdTest&version=" + version + "&application="
+ + application + (group == null ? "" : "&group=" + group));
+ return url;
+ }
}
diff --git a/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryTest.java b/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryTest.java
new file mode 100644
index 0000000..5955f7a
--- /dev/null
+++ b/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryTest.java
@@ -0,0 +1,8 @@
+package org.apache.dubbo.registry.etcd;
+
+/**
+ * @author cvictory ON 2019-08-30
+ */
+public class EtcdServiceDiscoveryTest {
+
+}