From 7725906c779ecf226f9917681e3f64d0c474be1f Mon Sep 17 00:00:00 2001 From: cvictory Date: Fri, 23 Aug 2019 11:12:58 +0800 Subject: [PATCH 1/7] nacos --- dubbo-bootstrap/pom.xml | 7 +++++++ .../bootstrap/NacosDubboServiceConsumerBootstrap.java | 1 + 2 files changed, 8 insertions(+) diff --git a/dubbo-bootstrap/pom.xml b/dubbo-bootstrap/pom.xml index 9818a3a8c3..18ec6fad65 100644 --- a/dubbo-bootstrap/pom.xml +++ b/dubbo-bootstrap/pom.xml @@ -79,6 +79,13 @@ test + + org.apache.dubbo + dubbo-metadata-report-redis + ${project.parent.version} + test + + org.apache.dubbo dubbo-configcenter-zookeeper diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/NacosDubboServiceConsumerBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/NacosDubboServiceConsumerBootstrap.java index 1bddcd37e6..69f430cb2b 100644 --- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/NacosDubboServiceConsumerBootstrap.java +++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/NacosDubboServiceConsumerBootstrap.java @@ -37,6 +37,7 @@ public class NacosDubboServiceConsumerBootstrap { .application(applicationConfig) // Zookeeper .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service&subscribed.services=dubbo-nacos-provider-demo")) +// .metadataReport(new MetadataReportConfig("nacos://127.0.0.1:8848")) .metadataReport(new MetadataReportConfig("nacos://127.0.0.1:8848")) // Nacos // .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1")) From b2d5e64ca08e7ca757dfa89e7b321382d1f0830b Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 29 Aug 2019 09:23:17 +0800 Subject: [PATCH 2/7] change pom version to 2.7.5-cloudnative-SNAPSHOT --- dubbo-all/pom.xml | 30 +++++++++++++++++++ dubbo-dependencies-bom/pom.xml | 2 +- .../dubbo-dependencies-zookeeper/pom.xml | 2 +- pom.xml | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index bbacb73413..93b13d0442 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -274,6 +274,16 @@ ${project.version} compile true + + + io.grpc + grpc-core + + + io.grpc + grpc-netty + + org.apache.dubbo @@ -449,6 +459,16 @@ ${project.version} compile true + + + io.grpc + grpc-core + + + io.grpc + grpc-netty + + org.apache.dubbo @@ -498,6 +518,16 @@ ${project.version} compile true + + + io.grpc + grpc-core + + + io.grpc + grpc-netty + + org.apache.dubbo diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index 8cd3ca87fe..3c5b432750 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -159,7 +159,7 @@ 6.1.26 2.0 1.1.0 - 2.7.4-SNAPSHOT + 2.7.5-cloudnative-SNAPSHOT diff --git a/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml b/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml index 49f4a56223..2fb9cf2057 100644 --- a/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml +++ b/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml @@ -32,7 +32,7 @@ pom - 2.7.4-SNAPSHOT + 2.7.5-cloudnative-SNAPSHOT 1.1.0 diff --git a/pom.xml b/pom.xml index e93d6eb9e4..52d24cee94 100644 --- a/pom.xml +++ b/pom.xml @@ -125,7 +125,7 @@ true true - 2.7.4-SNAPSHOT + 2.7.5-cloudnative-SNAPSHOT From ef4415e82545254de1da7e8d4840531cf5abf622 Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 29 Aug 2019 16:46:00 +0800 Subject: [PATCH 3/7] unit test --- .../InMemoryWritableMetadataService.java | 52 ++++- .../store/RemoteWritableMetadataService.java | 30 ++- ...RemoteWritableMetadataServiceDelegate.java | 14 +- .../InMemoryWritableMetadataServiceTest.java | 79 ++++++++ .../RemoteWritableMeatadataServiceTest.java | 88 +++++++-- ...teWritableMetadataServiceDelegateTest.java | 183 ++++++++++++++++++ .../test/JTestMetadataReport4Test.java | 13 +- 7 files changed, 415 insertions(+), 44 deletions(-) create mode 100644 dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataServiceTest.java create mode 100644 dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegateTest.java diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataService.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataService.java index 7c34e672d7..749f2344c0 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataService.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataService.java @@ -17,6 +17,8 @@ package org.apache.dubbo.metadata.store; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.metadata.WritableMetadataService; @@ -25,11 +27,14 @@ import org.apache.dubbo.metadata.definition.model.ServiceDefinition; import com.google.gson.Gson; +import java.util.Collection; import java.util.Comparator; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -46,19 +51,42 @@ import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty; * * @see MetadataService * @see WritableMetadataService - * @since 2.7.4 + * @since 2.7.5 */ -public class InMemoryWritableMetadataService extends BaseWritableMetadataService implements WritableMetadataService { - +public class InMemoryWritableMetadataService implements WritableMetadataService { + final Logger logger = LoggerFactory.getLogger(getClass()); private final Lock lock = new ReentrantLock(); + // =================================== Registration =================================== // + + /** + * All exported {@link URL urls} {@link Map} whose key is the return value of {@link URL#getServiceKey()} method + * and value is the {@link SortedSet sorted set} of the {@link URL URLs} + */ + ConcurrentNavigableMap> exportedServiceURLs = new ConcurrentSkipListMap<>(); + // ==================================================================================== // + // =================================== Subscription =================================== // + + /** + * The subscribed {@link URL urls} {@link Map} of {@link MetadataService}, + * whose key is the return value of {@link URL#getServiceKey()} method and value is + * the {@link SortedSet sorted set} of the {@link URL URLs} + */ + ConcurrentNavigableMap> subscribedServiceURLs = new ConcurrentSkipListMap<>(); + + ConcurrentNavigableMap serviceDefinitions = new ConcurrentSkipListMap<>(); + @Override public SortedSet getSubscribedURLs() { - return super.getSubscribedURLs(); + return getAllUnmodifiableServiceURLs(subscribedServiceURLs); + } + + SortedSet getAllUnmodifiableServiceURLs(Map> serviceURLs) { + return MetadataService.toSortedStrings(serviceURLs.values().stream().flatMap(Collection::stream)); } @Override @@ -129,8 +157,14 @@ public class InMemoryWritableMetadataService extends BaseWritableMetadataService boolean removeURL(Map> serviceURLs, URL url) { return executeMutually(() -> { - SortedSet urls = serviceURLs.getOrDefault(url.getServiceKey(), emptySortedSet()); - return urls.remove(url); + String key = url.getServiceKey(); + SortedSet urls = serviceURLs.getOrDefault(key, emptySortedSet()); + boolean r = urls.remove(url); + // if it is empty + if (urls.isEmpty()) { + serviceURLs.remove(key); + } + return r; }); } @@ -155,8 +189,8 @@ public class InMemoryWritableMetadataService extends BaseWritableMetadataService return success; } - private static SortedSet getServiceURLs(Map> exportedServiceURLs, String serviceKey, - String protocol) { + private SortedSet getServiceURLs(Map> exportedServiceURLs, String serviceKey, + String protocol) { SortedSet serviceURLs = exportedServiceURLs.get(serviceKey); @@ -167,7 +201,7 @@ public class InMemoryWritableMetadataService extends BaseWritableMetadataService return MetadataService.toSortedStrings(serviceURLs.stream().filter(url -> isAcceptableProtocol(protocol, url))); } - private static boolean isAcceptableProtocol(String protocol, URL url) { + private boolean isAcceptableProtocol(String protocol, URL url) { return protocol == null || protocol.equals(url.getParameter(PROTOCOL_KEY)) || protocol.equals(url.getProtocol()); diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataService.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataService.java index 90181ee185..22c028d72b 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataService.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataService.java @@ -19,6 +19,7 @@ package org.apache.dubbo.metadata.store; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder; @@ -54,13 +55,15 @@ import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; * * @since 2.7.5 */ -public class RemoteWritableMetadataService extends BaseWritableMetadataService implements WritableMetadataService { +public class RemoteWritableMetadataService implements WritableMetadataService { protected final Logger logger = LoggerFactory.getLogger(getClass()); private volatile String exportedRevision; private volatile String subscribedRevision; + private InMemoryWritableMetadataService writableMetadataService; - public RemoteWritableMetadataService() { + public RemoteWritableMetadataService(InMemoryWritableMetadataService writableMetadataService) { + this.writableMetadataService = writableMetadataService; } public MetadataReport getMetadataReport() { @@ -93,13 +96,15 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i public void publishProvider(URL providerUrl) throws RpcException { //first add into the list // remove the individul param - providerUrl = providerUrl.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, Constants.BIND_PORT_KEY, TIMESTAMP_KEY); + providerUrl = providerUrl.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, + Constants.BIND_PORT_KEY, TIMESTAMP_KEY); try { String interfaceName = providerUrl.getParameter(INTERFACE_KEY); if (StringUtils.isNotEmpty(interfaceName)) { Class interfaceClass = Class.forName(interfaceName); - FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(interfaceClass, providerUrl.getParameters()); + FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(interfaceClass, + providerUrl.getParameters()); getMetadataReport().storeProviderMetadata(new MetadataIdentifier(providerUrl.getServiceInterface(), providerUrl.getParameter(VERSION_KEY), providerUrl.getParameter(GROUP_KEY), PROVIDER_SIDE, providerUrl.getParameter(APPLICATION_KEY)), fullServiceDefinition); @@ -114,7 +119,8 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i @Deprecated public void publishConsumer(URL consumerURL) throws RpcException { - consumerURL = consumerURL.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, Constants.BIND_PORT_KEY, TIMESTAMP_KEY); + consumerURL = consumerURL.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, + Constants.BIND_PORT_KEY, TIMESTAMP_KEY); getMetadataReport().storeConsumerMetadata(new MetadataIdentifier(consumerURL.getServiceInterface(), consumerURL.getParameter(VERSION_KEY), consumerURL.getParameter(GROUP_KEY), CONSUMER_SIDE, consumerURL.getParameter(APPLICATION_KEY)), consumerURL.getParameters()); @@ -153,12 +159,14 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i result = false; } } - if (!StringUtils.isEmpty(subscribedRevision) && !subscribedRevision.equals(this.subscribedRevision)) { + if (!StringUtils.isEmpty(subscribedRevision) && !subscribedRevision.equals(this.subscribedRevision) + && CollectionUtils.isNotEmpty(writableMetadataService.getSubscribedURLs())) { this.subscribedRevision = subscribedRevision; SubscriberMetadataIdentifier metadataIdentifier = new SubscriberMetadataIdentifier(); metadataIdentifier.setApplication(serviceName()); metadataIdentifier.setRevision(subscribedRevision); - boolean executeResult = throwableAction(getMetadataReport()::saveSubscribedData, metadataIdentifier, super.getSubscribedURLs()); + boolean executeResult = throwableAction(getMetadataReport()::saveSubscribedData, metadataIdentifier, + writableMetadataService.getSubscribedURLs()); if (!executeResult) { result = false; } @@ -168,7 +176,7 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i private boolean saveServiceMetadata() { boolean result = true; - for (SortedSet urls : exportedServiceURLs.values()) { + for (SortedSet urls : writableMetadataService.exportedServiceURLs.values()) { Iterator iterator = urls.iterator(); while (iterator.hasNext()) { URL url = iterator.next(); @@ -200,7 +208,8 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i return null; } - boolean throwableAction(BiConsumer consumer, ServiceMetadataIdentifier metadataIdentifier, URL url) { + boolean throwableAction(BiConsumer consumer, + ServiceMetadataIdentifier metadataIdentifier, URL url) { try { consumer.accept(metadataIdentifier, url); } catch (Exception e) { @@ -210,7 +219,8 @@ public class RemoteWritableMetadataService extends BaseWritableMetadataService i return true; } - boolean throwableAction(BiConsumer> consumer, SubscriberMetadataIdentifier metadataIdentifier, Set urls) { + boolean throwableAction(BiConsumer> consumer, + SubscriberMetadataIdentifier metadataIdentifier, Set urls) { try { consumer.accept(metadataIdentifier, urls); } catch (Exception e) { diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java index f46bf2174e..eed9e43703 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegate.java @@ -1,6 +1,7 @@ package org.apache.dubbo.metadata.store; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.config.InmemoryConfiguration; import org.apache.dubbo.metadata.WritableMetadataService; import java.util.SortedSet; @@ -12,12 +13,12 @@ import java.util.function.BiFunction; * @since 2.7.5 */ public class RemoteWritableMetadataServiceDelegate implements WritableMetadataService { - private WritableMetadataService defaultWritableMetadataService; - private RemoteWritableMetadataService remoteWritableMetadataService; + InMemoryWritableMetadataService defaultWritableMetadataService; + RemoteWritableMetadataService remoteWritableMetadataService; public RemoteWritableMetadataServiceDelegate() { - defaultWritableMetadataService = WritableMetadataService.getDefaultExtension(); - remoteWritableMetadataService = new RemoteWritableMetadataService(); + defaultWritableMetadataService = (InMemoryWritableMetadataService) WritableMetadataService.getExtension("local"); + remoteWritableMetadataService = new RemoteWritableMetadataService(defaultWritableMetadataService); } private WritableMetadataService getDefaultWritableMetadataService() { @@ -79,9 +80,6 @@ public class RemoteWritableMetadataServiceDelegate implements WritableMetadataSe } private boolean doFunction(BiFunction func, URL url) { - boolean result = true; - result &= func.apply(defaultWritableMetadataService, url); - result &= func.apply(remoteWritableMetadataService, url); - return result; + return func.apply(defaultWritableMetadataService, url) && func.apply(remoteWritableMetadataService, url); } } diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataServiceTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataServiceTest.java new file mode 100644 index 0000000000..c032843f28 --- /dev/null +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/InMemoryWritableMetadataServiceTest.java @@ -0,0 +1,79 @@ +package org.apache.dubbo.metadata.store; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.metadata.report.MetadataReportInstance; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * 2019-08-29 + */ +public class InMemoryWritableMetadataServiceTest { + + String interfaceName = "org.apache.dubbo.metadata.store.InterfaceNameTestService2", version = "0.9.9", group = null; + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/?interface=" + interfaceName + "&version=" + + version + "&application=vicpubprovder&side=provider"); + + @BeforeEach + public void before() { + } + + @Test + public void testPublishServiceDefinition() { + InMemoryWritableMetadataService inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + inMemoryWritableMetadataService.publishServiceDefinition(url); + + String v = inMemoryWritableMetadataService.getServiceDefinition(interfaceName, version, group); + Assertions.assertNotNull(v); + } + + @Test + public void testExportURL() { + InMemoryWritableMetadataService inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test567Service?version=1.0.44&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.exportURL(url); + + Assertions.assertTrue(inMemoryWritableMetadataService.exportedServiceURLs.size() == 1); + Assertions.assertEquals(inMemoryWritableMetadataService.exportedServiceURLs.get(url.getServiceKey()).first(), url); + } + + @Test + public void testSubscribeURL() { + InMemoryWritableMetadataService inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + URL url = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test678Service?version=1.0.44&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.subscribeURL(url); + + Assertions.assertTrue(inMemoryWritableMetadataService.subscribedServiceURLs.size() == 1); + Assertions.assertEquals(inMemoryWritableMetadataService.subscribedServiceURLs.get(url.getServiceKey()).first(), url); + } + + @Test + public void testUnExportURL() { + InMemoryWritableMetadataService inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test567Service?version=1.0.44&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.exportURL(url); + + Assertions.assertTrue(inMemoryWritableMetadataService.exportedServiceURLs.size() == 1); + Assertions.assertEquals(inMemoryWritableMetadataService.exportedServiceURLs.get(url.getServiceKey()).first(), url); + + inMemoryWritableMetadataService.unexportURL(url); + Assertions.assertTrue(inMemoryWritableMetadataService.exportedServiceURLs.size() == 0); + } + + @Test + public void testUnSubscribeURL() { + InMemoryWritableMetadataService inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + URL url = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test678Service?version=1.0.44&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.subscribeURL(url); + + Assertions.assertTrue(inMemoryWritableMetadataService.subscribedServiceURLs.size() == 1); + Assertions.assertEquals(inMemoryWritableMetadataService.subscribedServiceURLs.get(url.getServiceKey()).first(), url); + + inMemoryWritableMetadataService.unsubscribeURL(url); + Assertions.assertTrue(inMemoryWritableMetadataService.subscribedServiceURLs.size() == 0); + } + +} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMeatadataServiceTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMeatadataServiceTest.java index fe2b4f6ecf..beeb1bdc23 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMeatadataServiceTest.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMeatadataServiceTest.java @@ -21,7 +21,11 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.WritableMetadataService; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; import org.apache.dubbo.metadata.report.MetadataReportInstance; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; +import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier; +import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier; import org.apache.dubbo.metadata.test.JTestMetadataReport4Test; +import org.apache.dubbo.rpc.model.ApplicationModel; import com.google.gson.Gson; import org.junit.jupiter.api.Assertions; @@ -38,24 +42,17 @@ import static org.apache.dubbo.common.constants.CommonConstants.METADATA_REMOTE; public class RemoteWritableMeatadataServiceTest { URL url = URL.valueOf("JTest://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); RemoteWritableMetadataService metadataReportService1; + InMemoryWritableMetadataService inMemoryWritableMetadataService; @BeforeEach public void before() { - metadataReportService1 = (RemoteWritableMetadataService) WritableMetadataService.getExtension(METADATA_REMOTE); + inMemoryWritableMetadataService = new InMemoryWritableMetadataService(); + metadataReportService1 = new RemoteWritableMetadataService(inMemoryWritableMetadataService); MetadataReportInstance.init(url); } - @Test - public void testInstance() { - - RemoteWritableMetadataService metadataReportService2 = (RemoteWritableMetadataService) WritableMetadataService.getExtension(METADATA_REMOTE); - Assertions.assertSame(metadataReportService1, metadataReportService2); - } - @Test public void testPublishProviderNoInterfaceName() { - - URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpubprovder&side=provider"); metadataReportService1.publishProvider(publishUrl); @@ -93,7 +90,7 @@ public class RemoteWritableMeatadataServiceTest { String value = jTestMetadataReport4Test.store.get(JTestMetadataReport4Test.getProviderKey(publishUrl)); FullServiceDefinition fullServiceDefinition = toServiceDefinition(value); - Map map = fullServiceDefinition.getParameters(); + Map map = fullServiceDefinition.getParameters(); Assertions.assertEquals(map.get("application"), "vicpubp"); Assertions.assertEquals(map.get("version"), "1.0.3"); Assertions.assertEquals(map.get("interface"), "org.apache.dubbo.metadata.store.InterfaceNameTestService"); @@ -119,6 +116,75 @@ public class RemoteWritableMeatadataServiceTest { } + @Test + public void testPublishServiceDefinition() { + URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpubprovder&side=provider"); + metadataReportService1.publishServiceDefinition(publishUrl); + + Assertions.assertTrue(metadataReportService1.getMetadataReport() instanceof JTestMetadataReport4Test); + + JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.getMetadataReport(); + Assertions.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); + + } + + @Test + public void testUnexportURL() { + + } + + @Test + public void testRefreshMetadataService() throws InterruptedException { + URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadataService?version=1.0.8&application=vicpubprovder&side=provider"); + URL publishUrl2 = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata2Service?version=1.0.5&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.exportURL(publishUrl); + inMemoryWritableMetadataService.exportURL(publishUrl2); + String exportedRevision = "9999"; + JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.getMetadataReport(); + int origSize = jTestMetadataReport4Test.store.size(); + Assertions.assertTrue(metadataReportService1.refreshMetadata(exportedRevision, "1109")); + Thread.sleep(200); + int size = jTestMetadataReport4Test.store.size(); + Assertions.assertTrue(size - origSize == 2); + Assertions.assertEquals(jTestMetadataReport4Test.store.get(getServiceMetadataIdentifier(publishUrl, exportedRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)), publishUrl.toFullString()); + Assertions.assertEquals(jTestMetadataReport4Test.store.get(getServiceMetadataIdentifier(publishUrl2, exportedRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)), publishUrl2.toFullString()); + } + + @Test + public void testRefreshMetadataSubscription() throws InterruptedException { + URL subscriberUrl1 = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata00Service?version=1.0.8&application=vicpubprovder&side=provider"); + URL subscriberUrl2 = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata09Service?version=1.0.5&application=vicpubprovder&side=provider"); + inMemoryWritableMetadataService.subscribeURL(subscriberUrl1); + inMemoryWritableMetadataService.subscribeURL(subscriberUrl2); + String exportedRevision = "9999"; + String subscriberRevision = "2099"; + String applicationName = "wriableMetadataService"; + JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.getMetadataReport(); + int origSize = jTestMetadataReport4Test.store.size(); + ApplicationModel.setApplication(applicationName); + Assertions.assertTrue(metadataReportService1.refreshMetadata(exportedRevision, subscriberRevision)); + Thread.sleep(200); + int size = jTestMetadataReport4Test.store.size(); + Assertions.assertTrue(size - origSize == 1); + String r = jTestMetadataReport4Test.store.get(getSubscriberMetadataIdentifier( + subscriberRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + Assertions.assertNotNull(r); + } + + private ServiceMetadataIdentifier getServiceMetadataIdentifier(URL publishUrl, String exportedRevision) { + ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(publishUrl); + serviceMetadataIdentifier.setRevision(exportedRevision); + serviceMetadataIdentifier.setProtocol(publishUrl.getProtocol()); + return serviceMetadataIdentifier; + } + + private SubscriberMetadataIdentifier getSubscriberMetadataIdentifier(String subscriberRevision) { + SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(); + subscriberMetadataIdentifier.setRevision(subscriberRevision); + subscriberMetadataIdentifier.setApplication(ApplicationModel.getApplication()); + return subscriberMetadataIdentifier; + } + private FullServiceDefinition toServiceDefinition(String urlQuery) { Gson gson = new Gson(); return gson.fromJson(urlQuery, FullServiceDefinition.class); diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegateTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegateTest.java new file mode 100644 index 0000000000..10b3eec8ed --- /dev/null +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/store/RemoteWritableMetadataServiceDelegateTest.java @@ -0,0 +1,183 @@ +package org.apache.dubbo.metadata.store; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.metadata.WritableMetadataService; +import org.apache.dubbo.metadata.report.MetadataReportInstance; +import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum; +import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier; +import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier; +import org.apache.dubbo.metadata.test.JTestMetadataReport4Test; +import org.apache.dubbo.rpc.model.ApplicationModel; + +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 java.util.SortedSet; + +import static org.apache.dubbo.common.constants.CommonConstants.METADATA_REMOTE; + +/** + * 2019-08-27 + */ +public class RemoteWritableMetadataServiceDelegateTest { + static URL metadataURL = URL.valueOf("JTest://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Tes33tService?version=1.0.0&application=vic"); + + RemoteWritableMetadataServiceDelegate metadataReportService; + + String interfaceName = "org.apache.dubbo.metadata.store.InterfaceNameTestService80", version = "0.6.9", group = null; + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/?interface=" + interfaceName + "&version=" + + version + "&application=vicpubprovder&side=provider"); + + @BeforeAll + public static void beforeAll() { + MetadataReportInstance.init(metadataURL); + } + + @BeforeEach + public void before() { + metadataReportService = new RemoteWritableMetadataServiceDelegate(); + } + + + @Test + public void testInstance() { + WritableMetadataService metadataReportService1 = WritableMetadataService.getExtension(METADATA_REMOTE); + WritableMetadataService metadataReportService2 = WritableMetadataService.getExtension(METADATA_REMOTE); + Assertions.assertSame(metadataReportService1, metadataReportService2); + Assertions.assertTrue(metadataReportService1 instanceof RemoteWritableMetadataServiceDelegate); + } + + @Test + public void testPublishServiceDefinition() throws InterruptedException { + String interfaceName = "org.apache.dubbo.metadata.store.InterfaceNameTestService2", version = "0.9.9", group = null; + URL tmpUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/?interface=" + interfaceName + "&version=" + + version + "&application=vicpubprovder&side=provider"); + metadataReportService.publishServiceDefinition(tmpUrl); + Thread.sleep(150); + String v = metadataReportService.getServiceDefinition(interfaceName, version, group); + Assertions.assertNotNull(v); + } + + @Test + public void testExportURL() throws InterruptedException { + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test567Service?version=1.0.44&application=vicpubprovder&side=provider"); + metadataReportService.exportURL(url); + Thread.sleep(100); + Assertions.assertTrue(getInMemoryWriableMetadataService().exportedServiceURLs.size() == 1); + Assertions.assertEquals(getInMemoryWriableMetadataService().exportedServiceURLs.get(url.getServiceKey()).first(), url); + } + + @Test + public void testSubscribeURL() throws InterruptedException { + URL url = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test0678Service?version=1.3.144&application=vicpubprovder&side=provider"); + int origSize = getInMemoryWriableMetadataService().subscribedServiceURLs.size(); + metadataReportService.subscribeURL(url); + Thread.sleep(100); + int size = getInMemoryWriableMetadataService().subscribedServiceURLs.size(); + Assertions.assertTrue(size - origSize == 1); + Assertions.assertEquals(getInMemoryWriableMetadataService().subscribedServiceURLs.get(url.getServiceKey()).first(), url); + } + + @Test + public void testUnExportURL() throws InterruptedException { + URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test0567Service?version=1.2.44&application=vicpubprovder&side=provider"); + int origSize = getInMemoryWriableMetadataService().exportedServiceURLs.size(); + metadataReportService.exportURL(url); + Thread.sleep(100); + int size = getInMemoryWriableMetadataService().exportedServiceURLs.size(); + Assertions.assertTrue(size - origSize == 1); + Assertions.assertEquals(getInMemoryWriableMetadataService().exportedServiceURLs.get(url.getServiceKey()).first(), url); + + metadataReportService.unexportURL(url); + int unexportSize = getInMemoryWriableMetadataService().exportedServiceURLs.size(); + Assertions.assertTrue(size - unexportSize == 1); + } + + @Test + public void testUnSubscribeURL() throws InterruptedException { + URL url = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.Test0678Service?version=1.5.477&application=vicpubprovder&side=provider"); + int origSize = getInMemoryWriableMetadataService().subscribedServiceURLs.size(); + metadataReportService.subscribeURL(url); + Thread.sleep(100); + int size = getInMemoryWriableMetadataService().subscribedServiceURLs.size(); + Assertions.assertTrue(size - origSize == 1); + Assertions.assertEquals(getInMemoryWriableMetadataService().subscribedServiceURLs.get(url.getServiceKey()).first(), url); + + metadataReportService.unsubscribeURL(url); + Thread.sleep(100); + Assertions.assertTrue(getInMemoryWriableMetadataService().subscribedServiceURLs.size() == 0); + } + + @Test + public void testRefreshMetadataService() throws InterruptedException { + URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadataService?version=1.6.8&application=vicpubprovder&side=provider"); + URL publishUrl2 = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata2Service?version=1.6.5&application=vicpubprovder&side=provider"); + metadataReportService.exportURL(publishUrl); + metadataReportService.exportURL(publishUrl2); + String exportedRevision = "9999"; + JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) MetadataReportInstance.getMetadataReport(true); + int origSize = jTestMetadataReport4Test.store.size(); + int num = countNum(); + Assertions.assertTrue(metadataReportService.refreshMetadata(exportedRevision, "1109")); + Thread.sleep(200); + int size = jTestMetadataReport4Test.store.size(); + Assertions.assertTrue(size - origSize == num); + Assertions.assertEquals(jTestMetadataReport4Test.store.get(getServiceMetadataIdentifier(publishUrl, exportedRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)), publishUrl.toFullString()); + Assertions.assertEquals(jTestMetadataReport4Test.store.get(getServiceMetadataIdentifier(publishUrl2, exportedRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)), publishUrl2.toFullString()); + } + + + @Test + public void testRefreshMetadataSubscription() throws InterruptedException { + URL subscriberUrl1 = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata00Service?version=2.0.8&application=vicpubprovder&side=provider"); + URL subscriberUrl2 = URL.valueOf("subscriber://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestRefreshMetadata09Service?version=2.0.5&application=vicpubprovder&side=provider"); + metadataReportService.subscribeURL(subscriberUrl1); + metadataReportService.subscribeURL(subscriberUrl2); + String exportedRevision = "9999"; + String subscriberRevision = "2099"; + String applicationName = "wriableMetadataService"; + JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) MetadataReportInstance.getMetadataReport(true); + int origSize = jTestMetadataReport4Test.store.size(); + ApplicationModel.setApplication(applicationName); + Assertions.assertTrue(metadataReportService.refreshMetadata(exportedRevision, subscriberRevision)); + Thread.sleep(200); + int size = jTestMetadataReport4Test.store.size(); + Assertions.assertTrue(size - origSize == 1); + String r = jTestMetadataReport4Test.store.get(getSubscriberMetadataIdentifier( + subscriberRevision).getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); + Assertions.assertNotNull(r); + } + + + private ServiceMetadataIdentifier getServiceMetadataIdentifier(URL publishUrl, String exportedRevision) { + ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(publishUrl); + serviceMetadataIdentifier.setRevision(exportedRevision); + serviceMetadataIdentifier.setProtocol(publishUrl.getProtocol()); + return serviceMetadataIdentifier; + } + + private SubscriberMetadataIdentifier getSubscriberMetadataIdentifier(String subscriberRevision) { + SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(); + subscriberMetadataIdentifier.setRevision(subscriberRevision); + subscriberMetadataIdentifier.setApplication(ApplicationModel.getApplication()); + return subscriberMetadataIdentifier; + } + + private InMemoryWritableMetadataService getInMemoryWriableMetadataService() { + return (InMemoryWritableMetadataService) metadataReportService.defaultWritableMetadataService; + } + + private int countNum() { + int num = 0; + for (SortedSet tmp : getInMemoryWriableMetadataService().exportedServiceURLs.values()) { + num += tmp.size(); + } + if (!getInMemoryWriableMetadataService().subscribedServiceURLs.values().isEmpty()) { + num++; + } + return num; + } +} diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java index dc1571f5d9..34ebce39c3 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java +++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/test/JTestMetadataReport4Test.java @@ -25,6 +25,7 @@ import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier; import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier; import org.apache.dubbo.metadata.report.support.AbstractMetadataReport; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -43,7 +44,7 @@ public class JTestMetadataReport4Test extends AbstractMetadataReport { super(url); } - public Map store = new ConcurrentHashMap<>(); + public volatile Map store = new ConcurrentHashMap<>(); private static String getProtocol(URL url) { @@ -64,22 +65,22 @@ public class JTestMetadataReport4Test extends AbstractMetadataReport { @Override protected void doSaveMetadata(ServiceMetadataIdentifier metadataIdentifier, URL url) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + store.put(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), url.toFullString()); } @Override protected void doRemoveMetadata(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + store.remove(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); } @Override protected List doGetExportedURLs(ServiceMetadataIdentifier metadataIdentifier) { - throw new UnsupportedOperationException("This extension does not support working as a remote metadata center."); + return Arrays.asList(store.getOrDefault(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), "")); } @Override protected void doSaveSubscriberData(SubscriberMetadataIdentifier subscriberMetadataIdentifier, String urls) { - + store.put(subscriberMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), urls); } @Override @@ -97,6 +98,6 @@ public class JTestMetadataReport4Test extends AbstractMetadataReport { @Override public String getServiceDefinition(MetadataIdentifier consumerMetadataIdentifier) { - return null; + return store.get(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)); } } From 0bd3a97bdb61bd083d7310108a16ae12288283e9 Mon Sep 17 00:00:00 2001 From: cvictory Date: Fri, 30 Aug 2019 16:25:42 +0800 Subject: [PATCH 4/7] unit test for metadata --- .../store/etcd/EtcdMetadataReport.java | 6 +- .../store/etcd/EtcdMetadataReportTest.java | 125 +++++++++++++++++- .../ZookeeperMetadataReportTest.java | 118 +++++++++++++++++ .../etcd/EtcdServiceDiscoveryTest.java | 8 ++ 4 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscoveryTest.java 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 1e97927dbb..a80c6e83dd 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 c6c984312a..a502b97e2e 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 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 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 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 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 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 6b5d3b5921..454f25ed13 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 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 0000000000..5955f7a974 --- /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 { + +} From 5dcf3017ddff65664ff430ebffdf51f61ba84142 Mon Sep 17 00:00:00 2001 From: cvictory Date: Mon, 2 Sep 2019 14:09:33 +0800 Subject: [PATCH 5/7] unit test for etcd serviceDiscovery --- .../registry/etcd/EtcdServiceDiscovery.java | 12 ++- .../etcd/EtcdServiceDiscoveryTest.java | 102 +++++++++++++++++- 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java index 1f88f7e9c2..821d80cd40 100644 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java @@ -63,9 +63,9 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener> etcdListeners = new ConcurrentHashMap<>(); - private EtcdClient etcdClient; - private EventDispatcher dispatcher; - private ServiceInstance serviceInstance; + EtcdClient etcdClient; + EventDispatcher dispatcher; + ServiceInstance serviceInstance; @Override public void onEvent(ServiceInstancesChangedEvent event) { @@ -99,7 +99,9 @@ public class EtcdServiceDiscovery implements ServiceDiscovery, EventListener + * There is no embedded server. so it works depend on etcd local server. */ +@Disabled public class EtcdServiceDiscoveryTest { + static EtcdServiceDiscovery etcdServiceDiscovery; + + @BeforeAll + public static void setUp() throws Exception { + URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.registry.RegistryService"); + etcdServiceDiscovery = new EtcdServiceDiscovery(); + Assertions.assertNull(etcdServiceDiscovery.etcdClient); + etcdServiceDiscovery.initialize(url); + } + + @AfterAll + public static void destroy() throws Exception { +// etcdServiceDiscovery.destroy(); + } + + + @Test + public void testLifecycle() throws Exception { + URL url = URL.valueOf("etcd3://127.0.0.1:2233/org.apache.dubbo.registry.RegistryService"); + EtcdServiceDiscovery etcdServiceDiscoveryTmp = new EtcdServiceDiscovery(); + Assertions.assertNull(etcdServiceDiscoveryTmp.etcdClient); + etcdServiceDiscoveryTmp.initialize(url); + Assertions.assertNotNull(etcdServiceDiscoveryTmp.etcdClient); + Assertions.assertTrue(etcdServiceDiscoveryTmp.etcdClient.isConnected()); + etcdServiceDiscoveryTmp.destroy(); + Assertions.assertFalse(etcdServiceDiscoveryTmp.etcdClient.isConnected()); + } + + @Test + public void testRegistry() throws Exception { + ServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTestService", "127.0.0.1", 8080); + Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + etcdServiceDiscovery.register(serviceInstance); + Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + } + + @Test + public void testUnRegistry() throws Exception { + ServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest2Service", "127.0.0.1", 8080); + Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + etcdServiceDiscovery.register(serviceInstance); + Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + etcdServiceDiscovery.unregister(serviceInstance); + Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + } + + @Test + public void testUpdate() throws Exception { + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest2Service", "127.0.0.1", 8080); + Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + etcdServiceDiscovery.register(serviceInstance); + Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + Assertions.assertEquals(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)), + new Gson().toJson(serviceInstance)); + serviceInstance.setPort(9999); + etcdServiceDiscovery.update(serviceInstance); + Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); + Assertions.assertEquals(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)), + new Gson().toJson(serviceInstance)); + } + + @Test + public void testGetInstances() throws Exception { + String serviceName = "EtcdTest77Service"; + Assertions.assertTrue(etcdServiceDiscovery.getInstances(serviceName).isEmpty()); + etcdServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 8080)); + etcdServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 9809)); + Assertions.assertFalse(etcdServiceDiscovery.getInstances(serviceName).isEmpty()); + List r = convertToIpPort(etcdServiceDiscovery.getInstances(serviceName)); + Assertions.assertTrue(r.contains("127.0.0.1:8080")); + Assertions.assertTrue(r.contains("127.0.0.1:9809")); + } + + private List convertToIpPort(List serviceInstances) { + List result = new ArrayList<>(); + for (ServiceInstance serviceInstance : serviceInstances) { + result.add(serviceInstance.getHost() + ":" + serviceInstance.getPort()); + } + return result; + } + } From b80a247ce28a389c7c2cd9a894afbfbbe64ed9ac Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 5 Sep 2019 15:06:48 +0800 Subject: [PATCH 6/7] unit test --- .../apache/dubbo/registry/etcd/EtcdServiceDiscoveryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 48cff97d96..6422f022ee 100644 --- 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 @@ -72,7 +72,7 @@ public class EtcdServiceDiscoveryTest { @Test public void testUpdate() throws Exception { - DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest2Service", "127.0.0.1", 8080); + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest34Service", "127.0.0.1", 8080); Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); etcdServiceDiscovery.register(serviceInstance); Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance))); From 262bc7ce2535cb446096f2375203d9ea7181248b Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 5 Sep 2019 15:12:55 +0800 Subject: [PATCH 7/7] Dubbo cloud native (#5008) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Polish apache/dubbo#4542 : [Enhancement] Adapt the Java standard Event/Listener mechanism * Polish apache/dubbo#4541 : [Feature] Add local File System DynamicConfigurationFactory‘s extension * Polish apache#4541 : Bugfix * Polish apache/dubbo#4541 : Optimization * Polish apache/dubbo#4541 : Add the compatibility for PollingWatchService on the some platforms * Polish apache/dubbo#4541 : Add delay publish without ThreadPoolExecutor * Polish apache/dubbo#4541 : Refactor the extension name * Polish apache/dubbo#4541 : Add remove ops * Polish apache/dubbo#4541 : Add testable constructor * Polish apache/dubbo#4541 : Add getConfigGroups method * Polish apache/dubbo#4610 : [Refactor] Refactor the bootstrap module * Polish apache/dubbo#4541 : Fix the nulling URL issue * Polish apache/dubbo#4622 : [Refactor] Refactor ConfigManager * Polish apache/dubbo#4622 : [Refactor] Refactor ConfigManager * Polish apache/dubbo#4622 : Support multiple configcenters * Polish apache/dubbo#4671 : ServiceNameMapping will not map the group, version and protocol * update referenceCount log (#4683) Add comments to support multiple shared connections * Polish /apache/dubbo#4687 : Remove the duplicated test code in dubbo-config-spring (#4688) * #4685 修改代码if判断false问题 if (hasException == false)修改成if (!hasException) (#4695) * Fixed Service annotation method parameters are not in effect (#4598) * keep demo simple, and switch to use zookeeper as registry center (#4705) * keep demo simple, and switch to use zookeeper as registry center * remove comment * @Reference auto-wires the instance of generic interface #4594 (#4677) * try to shorten maven output to make travis build pass (#4710) * use CountDownLatch to check zk registry if establish connection (#4589) * Minor change * Rename the extension name of WritableMetadataService * Polish apache/dubbo#4759 : [Refactor] Change the signature of methods of MetadataService #4759 * Merge remote-tracking branch 'upstream/master' into dubbo-cloud-native # Conflicts: # dubbo-all/pom.xml # dubbo-bom/pom.xml # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/RegistryConfig.java # dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java # dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java # dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/DynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/MockDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java # dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java # dubbo-configcenter/dubbo-configcenter-nacos/src/test/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfigurationTest.java # dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifier.java # dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java # dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/report/identifier/MetadataIdentifierTest.java # dubbo-metadata/dubbo-metadata-definition-protobuf/src/main/java/org/apache/dubbo/metadata/definition/protobuf/ProtobufTypeBuilder.java # dubbo-metadata/dubbo-metadata-definition-protobuf/src/test/java/org/apache/dubbo/metadata/definition/protobuf/ProtobufTypeBuilderTest.java # dubbo-metadata/pom.xml # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/AbstractConfiguratorListener.java # dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java # dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistryFactory.java # dubbo-rpc/dubbo-rpc-xml/src/main/java/org/apache/dubbo/xml/rpc/protocol/xmlrpc/XmlRpcProtocol.java * Polish apache/dubbo#3984 : Add the implementation of Page getInstances(String serviceName, int offset, int pageSize, boolean healthyOnly) * Code merge * Fix the cases * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/DubboServiceProviderBootstrap.java # dubbo-metadata/dubbo-metadata-definition-protobuf/pom.xml # dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/ServiceOrientedRegistryTest.java # dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdServiceDiscovery.java * Refactor ConfigManager * Refactor ConfigManager * Resolve the issues on ConfigManager * Refactor and add test-cases for ConfigManager * Polish apache/dubbo#4774 : [Feature] Dubbo Cloud Native - To Support in Spring * Polish apache/dubbo#4808 : [Feature] Add the registered/unregistered event mechanism ShutdownHook * Polish apache/dubbo#4807 : [Feature] Add the callback mechanism ShutdownHook #4807 * Polish apache/dubbo#4813 : [Feature] add Prioritized implementation for ServiceInstanceCustomizer * Polish apache/dubbo#4815 : [Feature] Add the ServiceLoader for Dubbo's services or components * Polish apache/dubbo#4815 : [Feature] Add the ServiceLoader for Dubbo's services or components * Polish apache/dubbo#4813 : [Feature] add Prioritized implementation for ServiceInstanceCustomizer * Polish apache/dubbo#4807 : Add sort implementation * Refactor * Refactor * Polish apache/dubbo#4845 : [Feature] Enhance the Event-Publishing feature to original ServiceDiscovery * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceDiscoveryFactory.java # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java * Polish apache/dubbo#4854 : [Feature] MetadataService supports the Dubbo protocol under auto-increased port * Polish apache/dubbo#4857 : [Enhancement] Sync the Metadata storage type into ApplicationConfig * Polish apache/dubbo#4868 : [Enhancement] Refactor ConfigChangeEvent * Polish apache/dubbo#4868 : [Enhancement] Refactor ConfigChangeEvent * Polish apache/dubbo#4873 : [Feature] Add a conditional EventListener into Event Module * Polish apache/dubbo#4875 : [Feature] Refactor ServiceInstancesChangedListener * Remove the cycle dependencies * Remove the cycle dependencies * Polish apache/dubbo#4903 : [Feature] Set source into the BeanDefinition of Dubbo Config * Polish apache/dubbo#4902 : [Feature] Dubbo Cloud Native to Spring XML scenario * Polish apache/dubbo#4713 : Initial the new module and dependencies * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4690 : AnnotatedBeanDefinitionRegistryUtils#registerBeans can't remove the duplicated bean definitions * Polish apache/dubbo#4910 : [Feature] To suppoort DubboLifecycleComponentApplicationListener in Spring XML scenario * Polish apache/dubbo#4713 : Add Service discovery implementation for Eureka #4713 * Polish apache/dubbo#4713 : Add Service registration and discovery implementation for Eureka * Polish apache/dubbo#4713 : Add Service registration and discovery implementation for Eureka * Polish apache/dubbo#4920 : [Refactor] Extract the common implementation for URLs' revision * Refactor * Polish apache/dubbo#4925 : ServiceDiscovery limits only one ServiceInstancesChangedListener each service * Polish apache/dubbo#4925 : ServiceDiscovery limits only one ServiceInstancesChangedListener each service * Remove useless classes * Bugfix & Refactor ServiceDiscoveryRegistry * Polish apache/dubbo#4937 : The calculation of Revision should add the parameters of URL * Polish apache/dubbo#4940 : NacosDynamicConfiguration supports getConfigKeys method * Polish apache/dubbo#4942 : Dubbo Cloud Native supports multiple protcols * Polish apache/dubbo#4944 : [Feature] Simplify The metadata of URL for MetadataService * Polish apache/dubbo#4947 : [Feature] Dubbo Cloud-Native supports the REST call to Non-Dubbo * Merge remote-tracking branch 'upstream/cloud-native' into dubbo-cloud-native # Conflicts: # dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataUtils.java * Refactor * Update JavaDoc * Polish apache/dubbo#4905 : Add test cases for Configuration * Polish apache/dubbo#4905 : Add test cases for AbstractDynamicConfigurationFactory * Polish apache/dubbo#4905 : Add test cases for ConfigChangeType * Polish apache/dubbo#4905 : Add test cases for AbstractDynamicConfiguration * Polish apache/dubbo#4905 : Add test cases for ConfigChangedEvent * Polish apache/dubbo#4905 : Add test cases for ConfigChangedEvent * Polish apache/dubbo#4905 : Add test cases for FileSystemDynamicConfiguration * Polish apache/dubbo#4905 : Add test cases for FileSystemDynamicConfigurationFactory * Polish apache/dubbo#4905 : Enhancement the robustness for FileSystemDynamicConfiguration * Polish apache/dubbo#4905 : Add test cases for AdaptiveClassCodeGenerator * Polish apache/dubbo#4905 : Add test cases for ExtensionLoader * Polish apache/dubbo#4905 : Add test cases for ThrowableAction, ThrowableConsumer, ThrowableFunction * Polish apache/dubbo#4905 : Add test cases for Prioritized * Polish apache/dubbo#4905 : Add test cases for ShutdownHookCallbacks * Polish apache/dubbo#4905 : Add test cases for DubboServiceLoader * Polish apache/dubbo#4981 : Add dubbo-common * Polish apache/dubbo#4907 : Add test cases for DubboServiceDestroyedEvent * Polish apache/dubbo#4907 : Add test cases for DubboShutdownHookRegisteredEvent * Polish apache/dubbo#4907 : Add test cases for DubboShutdownHookUnregisteredEvent * Polish apache/dubbo#4907 : Add test cases for ReferenceConfigDestroyedEvent * Polish apache/dubbo#4907 : Add test cases for ReferenceConfigInitializedEvent * Polish apache/dubbo#4907 : Add test cases for ServiceInstancePortCustomizer * Polish apache/dubbo#4907 : Add test cases for ConfigurableMetadataServiceExporter * Polish apache/dubbo#4907 : bugfix for @EnableDubbo * Polish apache/dubbo#4907 : add test cases for @EnableDubboLifecycle --- dubbo-all/pom.xml | 56 +++--- .../dubbo/bootstrap/DubboBootstrap.java | 22 +++ ...ookeeperDubboServiceProviderBootstrap.java | 2 +- .../AbstractDynamicConfiguration.java | 18 +- .../AbstractDynamicConfigurationFactory.java | 5 +- .../configcenter/ConfigChangedEvent.java | 34 ++-- .../file/FileSystemDynamicConfiguration.java | 4 +- .../common/lang/ShutdownHookCallbacks.java | 2 +- .../apache/dubbo/common/utils/UrlUtils.java | 30 +++- .../config/InmemoryConfigurationTest.java | 21 ++- ...stractDynamicConfigurationFactoryTest.java | 52 ++++++ .../AbstractDynamicConfigurationTest.java | 167 ++++++++++++++++++ .../configcenter/ConfigChangeTypeTest.java | 38 ++++ .../configcenter/ConfigChangedEventTest.java | 71 ++++++++ ...SystemDynamicConfigurationFactoryTest.java | 40 +++++ .../FileSystemDynamicConfigurationTest.java | 11 +- .../AdaptiveClassCodeGeneratorTest.java | 48 +++++ .../common/extension/ExtensionLoaderTest.java | 133 ++++++++------ .../common/function/ThrowableActionTest.java | 37 ++++ .../function/ThrowableConsumerTest.java | 37 ++++ .../function/ThrowableFunctionTest.java | 37 ++++ .../lang/DefaultShutdownHookCallback.java | 36 ++++ .../dubbo/common/lang/PrioritizedTest.java | 2 +- .../lang/ShutdownHookCallbacksTest.java | 32 ++++ .../common/utils/DefaultCharSequence.java | 45 +++++ .../common/utils/DubboServiceLoaderTest.java | 40 +++++ .../META-INF/services/java.lang.CharSequence | 3 + ...che.dubbo.common.lang.ShutdownHookCallback | 1 + .../adaptive/HasAdaptiveExt$Adaptive | 12 ++ .../dubbo/config/AbstractInterfaceConfig.java | 7 +- .../ServiceInstancePortCustomizer.java | 3 +- .../event/DubboServiceDestroyedEventTest.java | 58 ++++++ .../DubboShutdownHookRegisteredEventTest.java | 59 +++++++ ...ubboShutdownHookUnregisteredEventTest.java | 60 +++++++ .../ReferenceConfigDestroyedEventTest.java | 65 +++++++ .../ReferenceConfigInitializedEventTest.java | 59 +++++++ ...nfigurableMetadataServiceExporterTest.java | 2 +- .../ServiceInstancePortCustomizerTest.java | 60 +++++++ .../annotation/EnableDubboLifecycle.java | 3 +- .../annotation/EnableDubboLifecycleTest.java | 55 ++++++ .../context/annotation/MyLifecycle.java | 58 ++++++ .../org.apache.dubbo.common.context.Lifecycle | 1 + 42 files changed, 1407 insertions(+), 119 deletions(-) create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactoryTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangeTypeTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEventTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationFactoryTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGeneratorTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableActionTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableConsumerTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableFunctionTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/lang/DefaultShutdownHookCallback.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/DefaultCharSequence.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboServiceLoaderTest.java create mode 100644 dubbo-common/src/test/resources/META-INF/services/java.lang.CharSequence create mode 100644 dubbo-common/src/test/resources/META-INF/services/org.apache.dubbo.common.lang.ShutdownHookCallback create mode 100644 dubbo-common/src/test/resources/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboServiceDestroyedEventTest.java create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookRegisteredEventTest.java create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookUnregisteredEventTest.java create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigDestroyedEventTest.java create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigInitializedEventTest.java create mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizerTest.java create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycleTest.java create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/MyLifecycle.java create mode 100644 dubbo-config/dubbo-config-spring/src/test/resources/META-INF/services/org.apache.dubbo.common.context.Lifecycle diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index 93b13d0442..89de0c64b5 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -682,8 +682,15 @@ + - META-INF/dubbo/internal/org.apache.dubbo.common.compiler.Compiler + + META-INF/dubbo/internal/org.apache.dubbo.common.compiler.Compiler + + + + + META-INF/dubbo/internal/org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory @@ -691,18 +698,37 @@ META-INF/dubbo/internal/org.apache.dubbo.common.extension.ExtensionFactory + + + META-INF/dubbo/internal/org.apache.dubbo.common.infra.InfraAdapter + + + + + META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter + + + + + META-INF/dubbo/internal/org.apache.dubbo.common.status.StatusChecker + + + + + META-INF/dubbo/internal/org.apache.dubbo.common.store.DataStore + + + + + META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.ThreadPool + + + + META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization - - META-INF/dubbo/internal/org.apache.dubbo.common.status.StatusChecker - - - - META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.ThreadPool - - META-INF/dubbo/internal/org.apache.dubbo.remoting.Dispatcher @@ -784,22 +810,10 @@ META-INF/dubbo/internal/org.apache.dubbo.cache.CacheFactory - - META-INF/dubbo/internal/org.apache.dubbo.common.store.DataStore - - - - META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter - - META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand - - META-INF/dubbo/internal/org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory - - META-INF/dubbo/internal/org.apache.dubbo.metadata.report.MetadataReportFactory diff --git a/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java b/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java index 900ab1b753..21d33e2897 100644 --- a/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java +++ b/dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java @@ -27,6 +27,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ConfigCenterConfig; import org.apache.dubbo.config.ConsumerConfig; @@ -602,6 +603,11 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle { */ @Override public DubboBootstrap start() { + + if (!shouldStart()) { + return this; + } + if (!isInitialized()) { initialize(); } @@ -632,6 +638,22 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle { return this; } + /** + * Should Start current bootstrap + * + * @return If there is not any service discovery registry in the {@link ConfigManager#getRegistries()}, it will not + * start current bootstrap + */ + private boolean shouldStart() { + return configManager.getRegistries() + .stream() + .map(RegistryConfig::getAddress) + .map(URL::valueOf) + .filter(UrlUtils::isServiceDiscoveryRegistryType) + .count() > 0; + } + + private boolean hasExportedServices() { return !metadataService.getExportedURLs().isEmpty(); } diff --git a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/ZookeeperDubboServiceProviderBootstrap.java b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/ZookeeperDubboServiceProviderBootstrap.java index 87d548d913..e190b2e25a 100644 --- a/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/ZookeeperDubboServiceProviderBootstrap.java +++ b/dubbo-bootstrap/src/test/java/org/apache/dubbo/bootstrap/ZookeeperDubboServiceProviderBootstrap.java @@ -29,7 +29,7 @@ public class ZookeeperDubboServiceProviderBootstrap { .application("zookeeper-dubbo-provider") .registry(builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service")) .protocol("dubbo", builder -> builder.port(-1).name("dubbo")) - .protocol("rest", builder -> builder.port(8082).name("rest")) + .protocol("rest", builder -> builder.port(8081).name("rest")) .service("echo", builder -> builder.interfaceClass(EchoService.class).ref(new EchoServiceImpl()).protocolIds("dubbo")) .service("user", builder -> builder.interfaceClass(UserService.class).ref(new UserServiceImpl()).protocolIds("rest")) .start() diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfiguration.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfiguration.java index e692733314..40d10be528 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfiguration.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfiguration.java @@ -189,11 +189,11 @@ public abstract class AbstractDynamicConfiguration implements DynamicConfigurati } protected static int getThreadPoolSize(URL url) { - return url.getParameter(THREAD_POOL_SIZE_PARAM_NAME, DEFAULT_THREAD_POOL_SIZE); + return getParameter(url, THREAD_POOL_SIZE_PARAM_NAME, DEFAULT_THREAD_POOL_SIZE); } protected static long getThreadPoolKeepAliveTime(URL url) { - return url.getParameter(THREAD_POOL_KEEP_ALIVE_TIME_PARAM_NAME, DEFAULT_THREAD_POOL_KEEP_ALIVE_TIME); + return getParameter(url, THREAD_POOL_KEEP_ALIVE_TIME_PARAM_NAME, DEFAULT_THREAD_POOL_KEEP_ALIVE_TIME); } protected static String getParameter(URL url, String name, String defaultValue) { @@ -202,4 +202,18 @@ public abstract class AbstractDynamicConfiguration implements DynamicConfigurati } return defaultValue; } + + protected static int getParameter(URL url, String name, int defaultValue) { + if (url != null) { + return url.getParameter(name, defaultValue); + } + return defaultValue; + } + + protected static long getParameter(URL url, String name, long defaultValue) { + if (url != null) { + return url.getParameter(name, defaultValue); + } + return defaultValue; + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactory.java index 15b9145dfa..e4f6d537f8 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactory.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactory.java @@ -24,14 +24,17 @@ import java.util.concurrent.ConcurrentHashMap; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY; /** + * Abstract {@link DynamicConfigurationFactory} implementation with cache ability * + * @see DynamicConfigurationFactory + * @since 2.7.4 */ public abstract class AbstractDynamicConfigurationFactory implements DynamicConfigurationFactory { private volatile Map dynamicConfigurations = new ConcurrentHashMap<>(); @Override - public DynamicConfiguration getDynamicConfiguration(URL url) { + public final DynamicConfiguration getDynamicConfiguration(URL url) { String key = url == null ? DEFAULT_KEY : url.getAddress(); return dynamicConfigurations.computeIfAbsent(key, k -> createDynamicConfiguration(url)); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEvent.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEvent.java index 8ea32e2730..8195558cd8 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEvent.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEvent.java @@ -39,7 +39,7 @@ public class ConfigChangedEvent extends EventObject { } public ConfigChangedEvent(String key, String group, String content, ConfigChangeType changeType) { - super(key + "=" + content); + super(key + "," + group); this.key = key; this.group = group; this.content = content; @@ -62,22 +62,6 @@ public class ConfigChangedEvent extends EventObject { return changeType; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ConfigChangedEvent)) return false; - ConfigChangedEvent that = (ConfigChangedEvent) o; - return Objects.equals(getKey(), that.getKey()) && - Objects.equals(group, that.group) && - Objects.equals(getContent(), that.getContent()) && - getChangeType() == that.getChangeType(); - } - - @Override - public int hashCode() { - return Objects.hash(getKey(), group, getContent(), getChangeType()); - } - @Override public String toString() { return "ConfigChangedEvent{" + @@ -87,4 +71,20 @@ public class ConfigChangedEvent extends EventObject { ", changeType=" + changeType + "} " + super.toString(); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ConfigChangedEvent)) return false; + ConfigChangedEvent that = (ConfigChangedEvent) o; + return Objects.equals(getKey(), that.getKey()) && + Objects.equals(getGroup(), that.getGroup()) && + Objects.equals(getContent(), that.getContent()) && + getChangeType() == that.getChangeType(); + } + + @Override + public int hashCode() { + return Objects.hash(getKey(), getGroup(), getContent(), getChangeType()); + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java index bfd7062aad..284da64a16 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfiguration.java @@ -582,13 +582,13 @@ public class FileSystemDynamicConfiguration extends AbstractDynamicConfiguration } protected static File initDirectory(URL url) { - String directoryPath = url.getParameter(CONFIG_CENTER_DIR_PARAM_NAME, url.getPath()); + String directoryPath = getParameter(url, CONFIG_CENTER_DIR_PARAM_NAME, url == null ? null : url.getPath()); File rootDirectory = null; if (!StringUtils.isBlank(directoryPath)) { rootDirectory = new File("/" + directoryPath); } - if (!rootDirectory.exists()) { // If the directory does not exist + if (directoryPath == null || !rootDirectory.exists()) { // If the directory does not exist rootDirectory = new File(DEFAULT_CONFIG_CENTER_DIR_PATH); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/lang/ShutdownHookCallbacks.java b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/ShutdownHookCallbacks.java index 70fb7749ea..7aabb77fd5 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/lang/ShutdownHookCallbacks.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/ShutdownHookCallbacks.java @@ -35,7 +35,7 @@ public class ShutdownHookCallbacks { private final List callbacks = new LinkedList<>(); - public ShutdownHookCallbacks() { + ShutdownHookCallbacks() { loadCallbacks(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java index 7a2f99cae8..c795ffda64 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java @@ -27,6 +27,7 @@ import java.util.Set; import java.util.function.Predicate; import java.util.stream.Collectors; +import static java.util.Collections.emptyMap; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.CLASSIFIER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; @@ -50,9 +51,11 @@ import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL import static org.apache.dubbo.common.constants.RegistryConstants.OVERRIDE_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; +import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_PROTOCOL; +import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE; public class UrlUtils { @@ -490,6 +493,31 @@ public class UrlUtils { return REGISTRY_PROTOCOL.equals(url.getProtocol()) || SERVICE_REGISTRY_PROTOCOL.equalsIgnoreCase(url.getProtocol()); } + /** + * The specified {@link URL} is service discovery registry type or not + * + * @param url the {@link URL} connects to the registry + * @return If it is, return true, or false + * @since 2.7.4 + */ + public static boolean isServiceDiscoveryRegistryType(URL url) { + return isServiceDiscoveryRegistryType(url == null ? emptyMap() : url.getParameters()); + } + + /** + * The specified parameters of {@link URL} is service discovery registry type or not + * + * @param parameters the parameters of {@link URL} that connects to the registry + * @return If it is, return true, or false + * @since 2.7.4 + */ + public static boolean isServiceDiscoveryRegistryType(Map parameters) { + if (parameters == null || parameters.isEmpty()) { + return false; + } + return SERVICE_REGISTRY_TYPE.equals(parameters.get(REGISTRY_TYPE_KEY)); + } + /** * Check if the given value matches the given pattern. The pattern supports wildcard "*". * @@ -507,7 +535,7 @@ public class UrlUtils { /** * @param serviceKey, {group}/{interfaceName}:{version} - * @return [group, interfaceName, version] + * @return [group, interfaceName, version] */ public static String[] parseServiceKey(String serviceKey) { String[] arr = new String[3]; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java index bdb371a1dc..9cdfcdd76c 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/InmemoryConfigurationTest.java @@ -29,7 +29,7 @@ import java.util.Map; */ class InmemoryConfigurationTest { - private static InmemoryConfiguration memConfig; + private InmemoryConfiguration memConfig; private static final String MOCK_KEY = "mockKey"; private static final String MOCK_VALUE = "mockValue"; private static final String MOCK_ONE_KEY = "one"; @@ -59,7 +59,6 @@ class InmemoryConfigurationTest { Assertions.assertEquals(MOCK_VALUE, memConfig.getInternalProperty(MOCK_KEY)); Assertions.assertEquals(MOCK_VALUE, memConfig.getString(MOCK_KEY, MOCK_VALUE)); Assertions.assertEquals(MOCK_VALUE, memConfig.getProperty(MOCK_KEY, MOCK_VALUE)); - } /** @@ -84,11 +83,27 @@ class InmemoryConfigurationTest { } + @Test + public void testGetInt() { + memConfig.addProperty("a", "1"); + Assertions.assertEquals(1, memConfig.getInt("a")); + Assertions.assertEquals(Integer.valueOf(1), memConfig.getInteger("a", 2)); + Assertions.assertEquals(2, memConfig.getInt("b", 2)); + } + + @Test + public void getBoolean() { + memConfig.addProperty("a", Boolean.TRUE.toString()); + Assertions.assertTrue(memConfig.getBoolean("a")); + Assertions.assertFalse(memConfig.getBoolean("b", false)); + Assertions.assertTrue(memConfig.getBoolean("b", Boolean.TRUE)); + } + /** * Clean. */ @AfterEach - public void clean(){ + public void clean() { } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactoryTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactoryTest.java new file mode 100644 index 0000000000..149d1986e0 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationFactoryTest.java @@ -0,0 +1,52 @@ +/* + * 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.common.config.configcenter; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.config.configcenter.nop.NopDynamicConfiguration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link AbstractDynamicConfigurationFactory} Test + * + * @see AbstractDynamicConfigurationFactory + * @since 2.7.4 + */ +public class AbstractDynamicConfigurationFactoryTest { + + private AbstractDynamicConfigurationFactory factory; + + @BeforeEach + public void init() { + factory = new AbstractDynamicConfigurationFactory() { + @Override + protected DynamicConfiguration createDynamicConfiguration(URL url) { + return new NopDynamicConfiguration(url); + } + }; + } + + @Test + public void testGetDynamicConfiguration() { + URL url = URL.valueOf("nop://127.0.0.1"); + assertEquals(factory.getDynamicConfiguration(url), factory.getDynamicConfiguration(url)); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationTest.java new file mode 100644 index 0000000000..42a15f9e1d --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/AbstractDynamicConfigurationTest.java @@ -0,0 +1,167 @@ +/* + * 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.common.config.configcenter; + +import org.apache.dubbo.common.URL; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.DEFAULT_THREAD_POOL_KEEP_ALIVE_TIME; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.DEFAULT_THREAD_POOL_PREFIX; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.DEFAULT_THREAD_POOL_SIZE; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.PARAM_NAME_PREFIX; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.THREAD_POOL_KEEP_ALIVE_TIME_PARAM_NAME; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.THREAD_POOL_PREFIX_PARAM_NAME; +import static org.apache.dubbo.common.config.configcenter.AbstractDynamicConfiguration.THREAD_POOL_SIZE_PARAM_NAME; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * {@link AbstractDynamicConfiguration} Test + * + * @since 2.7.4 + */ +public class AbstractDynamicConfigurationTest { + + private AbstractDynamicConfiguration configuration; + + @BeforeEach + public void init() { + configuration = new AbstractDynamicConfiguration() { + @Override + protected String doGetConfig(String key, String group) throws Exception { + return null; + } + + @Override + protected void doClose() throws Exception { + + } + }; + } + + @Test + public void testConstants() { + assertEquals("dubbo.config-center.", PARAM_NAME_PREFIX); + assertEquals("dubbo.config-center.workers", DEFAULT_THREAD_POOL_PREFIX); + assertEquals("dubbo.config-center.thread-pool.prefix", THREAD_POOL_PREFIX_PARAM_NAME); + assertEquals("dubbo.config-center.thread-pool.size", THREAD_POOL_SIZE_PARAM_NAME); + assertEquals("dubbo.config-center.thread-pool.keep-alive-time", THREAD_POOL_KEEP_ALIVE_TIME_PARAM_NAME); + assertEquals(1, DEFAULT_THREAD_POOL_SIZE); + assertEquals(60 * 1000, DEFAULT_THREAD_POOL_KEEP_ALIVE_TIME); + } + + @Test + public void testConstructor() { + URL url = URL.valueOf("default://") + .addParameter(THREAD_POOL_PREFIX_PARAM_NAME, "test") + .addParameter(THREAD_POOL_SIZE_PARAM_NAME, 10) + .addParameter(THREAD_POOL_KEEP_ALIVE_TIME_PARAM_NAME, 100); + + AbstractDynamicConfiguration configuration = new AbstractDynamicConfiguration(url) { + + @Override + protected String doGetConfig(String key, String group) throws Exception { + return null; + } + + @Override + protected void doClose() throws Exception { + + } + }; + + ThreadPoolExecutor threadPoolExecutor = configuration.getWorkersThreadPool(); + ThreadFactory threadFactory = threadPoolExecutor.getThreadFactory(); + + Thread thread = threadFactory.newThread(() -> { + }); + + assertEquals(10, threadPoolExecutor.getCorePoolSize()); + assertEquals(10, threadPoolExecutor.getMaximumPoolSize()); + assertEquals(100, threadPoolExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS)); + assertEquals("test-thread-1", thread.getName()); + } + + @Test + public void testPublishConfig() { + assertThrows(UnsupportedOperationException.class, () -> configuration.publishConfig(null, null), "No support"); + assertThrows(UnsupportedOperationException.class, () -> configuration.publishConfig(null, null, null), "No support"); + } + + @Test + public void testRemoveConfig() { + assertThrows(UnsupportedOperationException.class, () -> configuration.removeConfig(null), "No support"); + assertThrows(UnsupportedOperationException.class, () -> configuration.removeConfig(null, null), "No support"); + } + + @Test + public void testGetConfigGroups() { + assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigGroups(), "No support"); + } + + @Test + public void testGetConfigKeys() { + assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigKeys(null), "No support"); + } + + @Test + public void testGetConfigs() { + assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigs(null), "No support"); + assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigs(null, 1000), "No support"); + } + + @Test + public void testGetConfig() { + assertNull(configuration.getConfig(null, null)); + assertNull(configuration.getConfig(null, null, 200)); + } + + @Test + public void testGetInternalProperty() { + assertNull(configuration.getInternalProperty(null)); + } + + @Test + public void testGetProperties() { + assertNull(configuration.getProperties(null, null)); + assertNull(configuration.getProperties(null, null, 100L)); + } + + @Test + public void testAddListener() { + configuration.addListener(null, null); + configuration.addListener(null, null, null); + } + + @Test + public void testRemoveListener() { + configuration.removeListener(null, null); + configuration.removeListener(null, null, null); + } + + @Test + public void testClose() throws Exception { + configuration.close(); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangeTypeTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangeTypeTest.java new file mode 100644 index 0000000000..8095d59732 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangeTypeTest.java @@ -0,0 +1,38 @@ +/* + * 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.common.config.configcenter; + +import org.junit.jupiter.api.Test; + +import static org.apache.dubbo.common.config.configcenter.ConfigChangeType.ADDED; +import static org.apache.dubbo.common.config.configcenter.ConfigChangeType.DELETED; +import static org.apache.dubbo.common.config.configcenter.ConfigChangeType.MODIFIED; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +/** + * {@link ConfigChangeType} Test + * + * @see ConfigChangeType + * @since 2.7.4 + */ +public class ConfigChangeTypeTest { + + @Test + public void testMembers() { + assertArrayEquals(new ConfigChangeType[]{ADDED, MODIFIED, DELETED}, ConfigChangeType.values()); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEventTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEventTest.java new file mode 100644 index 0000000000..7de409aa08 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/ConfigChangedEventTest.java @@ -0,0 +1,71 @@ +/* + * 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.common.config.configcenter; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * {@link ConfigChangedEvent} Test + * + * @since 2.7.4 + */ +public class ConfigChangedEventTest { + + private static final String key = "k"; + + private static final String group = "g"; + + private static final String content = "c"; + + @Test + public void testGetter() { + + ConfigChangedEvent event = new ConfigChangedEvent(key, group, content); + + assertEquals(key, event.getKey()); + assertEquals(group, event.getGroup()); + assertEquals(content, event.getContent()); + assertEquals(ConfigChangeType.MODIFIED, event.getChangeType()); + assertEquals("k,g", event.getSource()); + + event = new ConfigChangedEvent(key, group, content, ConfigChangeType.ADDED); + + assertEquals(key, event.getKey()); + assertEquals(group, event.getGroup()); + assertEquals(content, event.getContent()); + assertEquals(ConfigChangeType.ADDED, event.getChangeType()); + assertEquals("k,g", event.getSource()); + } + + @Test + public void testEqualsAndHashCode() { + for (ConfigChangeType type : ConfigChangeType.values()) { + assertEquals(new ConfigChangedEvent(key, group, content, type), new ConfigChangedEvent(key, group, content, type)); + assertEquals(new ConfigChangedEvent(key, group, content, type).hashCode(), new ConfigChangedEvent(key, group, content, type).hashCode()); + assertEquals(new ConfigChangedEvent(key, group, content, type).toString(), new ConfigChangedEvent(key, group, content, type).toString()); + } + } + + @Test + public void testToString() { + ConfigChangedEvent event = new ConfigChangedEvent(key, group, content); + assertNotNull(event.toString()); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationFactoryTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationFactoryTest.java new file mode 100644 index 0000000000..ccd368d257 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationFactoryTest.java @@ -0,0 +1,40 @@ +/* + * 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.common.config.configcenter.file; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link FileSystemDynamicConfigurationFactory} Test + * + * @since 2.7.4 + */ +public class FileSystemDynamicConfigurationFactoryTest { + + @Test + public void testGetFactory() { + DynamicConfigurationFactory factory = DynamicConfigurationFactory.getDynamicConfigurationFactory("not-exists"); + assertEquals(factory, DynamicConfigurationFactory.getDynamicConfigurationFactory("file")); + assertEquals(factory.getDynamicConfiguration(URL.valueOf("dummy")), factory.getDynamicConfiguration(URL.valueOf("dummy"))); + assertEquals(FileSystemDynamicConfiguration.class, factory.getDynamicConfiguration(URL.valueOf("dummy")).getClass()); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationTest.java index 17ead90b00..7140136350 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/configcenter/file/FileSystemDynamicConfigurationTest.java @@ -19,6 +19,7 @@ package org.apache.dubbo.common.config.configcenter.file; import org.apache.dubbo.common.URL; import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -49,12 +50,18 @@ public class FileSystemDynamicConfigurationTest { @BeforeEach public void init() { - String classPath = getClassPath(); - URL url = valueOf("dubbo://127.0.0.1:20880").addParameter(CONFIG_CENTER_DIR_PARAM_NAME, classPath + File.separator + "config-center"); + File rootDirectory = new File(getClassPath(), "config-center"); + rootDirectory.mkdirs(); + URL url = valueOf("dubbo://127.0.0.1:20880").addParameter(CONFIG_CENTER_DIR_PARAM_NAME, rootDirectory.getAbsolutePath()); configuration = new FileSystemDynamicConfiguration(url); deleteQuietly(configuration.getRootDirectory()); } + @AfterEach + public void destroy() throws Exception { + configuration.close(); + } + private String getClassPath() { return getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGeneratorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGeneratorTest.java new file mode 100644 index 0000000000..911e34b300 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGeneratorTest.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.common.extension; + +import org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt; +import org.apache.dubbo.common.utils.IOUtils; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link AdaptiveClassCodeGenerator} Test + * + * @since 2.7.4 + */ +public class AdaptiveClassCodeGeneratorTest { + + @Test + public void testGenerate() throws IOException { + AdaptiveClassCodeGenerator generator = new AdaptiveClassCodeGenerator(HasAdaptiveExt.class, "adaptive"); + String value = generator.generate(); + URL url = getClass().getResource("/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive"); + try (InputStream inputStream = url.openStream()) { + String content = IOUtils.read(new InputStreamReader(inputStream, "UTF-8")); + assertEquals(content, value); + } + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java index ffe542a659..393ae3adf4 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java @@ -53,11 +53,13 @@ import org.apache.dubbo.common.extension.injection.impl.InjectExtImpl; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.instanceOf; @@ -73,7 +75,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtensionLoader_Null() throws Exception { try { - ExtensionLoader.getExtensionLoader(null); + getExtensionLoader(null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), @@ -84,7 +86,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtensionLoader_NotInterface() throws Exception { try { - ExtensionLoader.getExtensionLoader(ExtensionLoaderTest.class); + getExtensionLoader(ExtensionLoaderTest.class); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), @@ -95,7 +97,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtensionLoader_NotSpiAnnotation() throws Exception { try { - ExtensionLoader.getExtensionLoader(NoSpiExt.class); + getExtensionLoader(NoSpiExt.class); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), @@ -107,34 +109,34 @@ public class ExtensionLoaderTest { @Test public void test_getDefaultExtension() throws Exception { - SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getDefaultExtension(); + SimpleExt ext = getExtensionLoader(SimpleExt.class).getDefaultExtension(); assertThat(ext, instanceOf(SimpleExtImpl1.class)); - String name = ExtensionLoader.getExtensionLoader(SimpleExt.class).getDefaultExtensionName(); + String name = getExtensionLoader(SimpleExt.class).getDefaultExtensionName(); assertEquals("impl1", name); } @Test public void test_getDefaultExtension_NULL() throws Exception { - Ext2 ext = ExtensionLoader.getExtensionLoader(Ext2.class).getDefaultExtension(); + Ext2 ext = getExtensionLoader(Ext2.class).getDefaultExtension(); assertNull(ext); - String name = ExtensionLoader.getExtensionLoader(Ext2.class).getDefaultExtensionName(); + String name = getExtensionLoader(Ext2.class).getDefaultExtensionName(); assertNull(name); } @Test public void test_getExtension() throws Exception { - assertTrue(ExtensionLoader.getExtensionLoader(SimpleExt.class).getExtension("impl1") instanceof SimpleExtImpl1); - assertTrue(ExtensionLoader.getExtensionLoader(SimpleExt.class).getExtension("impl2") instanceof SimpleExtImpl2); + assertTrue(getExtensionLoader(SimpleExt.class).getExtension("impl1") instanceof SimpleExtImpl1); + assertTrue(getExtensionLoader(SimpleExt.class).getExtension("impl2") instanceof SimpleExtImpl2); } @Test public void test_getExtension_WithWrapper() throws Exception { - WrappedExt impl1 = ExtensionLoader.getExtensionLoader(WrappedExt.class).getExtension("impl1"); + WrappedExt impl1 = getExtensionLoader(WrappedExt.class).getExtension("impl1"); assertThat(impl1, anyOf(instanceOf(Ext5Wrapper1.class), instanceOf(Ext5Wrapper2.class))); - WrappedExt impl2 = ExtensionLoader.getExtensionLoader(WrappedExt.class).getExtension("impl2"); + WrappedExt impl2 = getExtensionLoader(WrappedExt.class).getExtension("impl2"); assertThat(impl2, anyOf(instanceOf(Ext5Wrapper1.class), instanceOf(Ext5Wrapper2.class))); @@ -150,7 +152,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtension_ExceptionNoExtension() throws Exception { try { - ExtensionLoader.getExtensionLoader(SimpleExt.class).getExtension("XXX"); + getExtensionLoader(SimpleExt.class).getExtension("XXX"); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("No such extension org.apache.dubbo.common.extension.ext1.SimpleExt by name XXX")); @@ -160,7 +162,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtension_ExceptionNoExtension_WrapperNotAffactName() throws Exception { try { - ExtensionLoader.getExtensionLoader(WrappedExt.class).getExtension("XXX"); + getExtensionLoader(WrappedExt.class).getExtension("XXX"); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("No such extension org.apache.dubbo.common.extension.ext6_wrap.WrappedExt by name XXX")); @@ -170,7 +172,7 @@ public class ExtensionLoaderTest { @Test public void test_getExtension_ExceptionNullArg() throws Exception { try { - ExtensionLoader.getExtensionLoader(SimpleExt.class).getExtension(null); + getExtensionLoader(SimpleExt.class).getExtension(null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("Extension name == null")); @@ -179,12 +181,12 @@ public class ExtensionLoaderTest { @Test public void test_hasExtension() throws Exception { - assertTrue(ExtensionLoader.getExtensionLoader(SimpleExt.class).hasExtension("impl1")); - assertFalse(ExtensionLoader.getExtensionLoader(SimpleExt.class).hasExtension("impl1,impl2")); - assertFalse(ExtensionLoader.getExtensionLoader(SimpleExt.class).hasExtension("xxx")); + assertTrue(getExtensionLoader(SimpleExt.class).hasExtension("impl1")); + assertFalse(getExtensionLoader(SimpleExt.class).hasExtension("impl1,impl2")); + assertFalse(getExtensionLoader(SimpleExt.class).hasExtension("xxx")); try { - ExtensionLoader.getExtensionLoader(SimpleExt.class).hasExtension(null); + getExtensionLoader(SimpleExt.class).hasExtension(null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("Extension name == null")); @@ -193,14 +195,14 @@ public class ExtensionLoaderTest { @Test public void test_hasExtension_wrapperIsNotExt() throws Exception { - assertTrue(ExtensionLoader.getExtensionLoader(WrappedExt.class).hasExtension("impl1")); - assertFalse(ExtensionLoader.getExtensionLoader(WrappedExt.class).hasExtension("impl1,impl2")); - assertFalse(ExtensionLoader.getExtensionLoader(WrappedExt.class).hasExtension("xxx")); + assertTrue(getExtensionLoader(WrappedExt.class).hasExtension("impl1")); + assertFalse(getExtensionLoader(WrappedExt.class).hasExtension("impl1,impl2")); + assertFalse(getExtensionLoader(WrappedExt.class).hasExtension("xxx")); - assertFalse(ExtensionLoader.getExtensionLoader(WrappedExt.class).hasExtension("wrapper1")); + assertFalse(getExtensionLoader(WrappedExt.class).hasExtension("wrapper1")); try { - ExtensionLoader.getExtensionLoader(WrappedExt.class).hasExtension(null); + getExtensionLoader(WrappedExt.class).hasExtension(null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("Extension name == null")); @@ -209,7 +211,7 @@ public class ExtensionLoaderTest { @Test public void test_getSupportedExtensions() throws Exception { - Set exts = ExtensionLoader.getExtensionLoader(SimpleExt.class).getSupportedExtensions(); + Set exts = getExtensionLoader(SimpleExt.class).getSupportedExtensions(); Set expected = new HashSet(); expected.add("impl1"); @@ -221,7 +223,7 @@ public class ExtensionLoaderTest { @Test public void test_getSupportedExtensions_wrapperIsNotExt() throws Exception { - Set exts = ExtensionLoader.getExtensionLoader(WrappedExt.class).getSupportedExtensions(); + Set exts = getExtensionLoader(WrappedExt.class).getSupportedExtensions(); Set expected = new HashSet(); expected.add("impl1"); @@ -233,35 +235,35 @@ public class ExtensionLoaderTest { @Test public void test_AddExtension() throws Exception { try { - ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("Manual1"); + getExtensionLoader(AddExt1.class).getExtension("Manual1"); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("No such extension org.apache.dubbo.common.extension.ext8_add.AddExt1 by name Manual")); } - ExtensionLoader.getExtensionLoader(AddExt1.class).addExtension("Manual1", AddExt1_ManualAdd1.class); - AddExt1 ext = ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("Manual1"); + getExtensionLoader(AddExt1.class).addExtension("Manual1", AddExt1_ManualAdd1.class); + AddExt1 ext = getExtensionLoader(AddExt1.class).getExtension("Manual1"); assertThat(ext, instanceOf(AddExt1_ManualAdd1.class)); - assertEquals("Manual1", ExtensionLoader.getExtensionLoader(AddExt1.class).getExtensionName(AddExt1_ManualAdd1.class)); + assertEquals("Manual1", getExtensionLoader(AddExt1.class).getExtensionName(AddExt1_ManualAdd1.class)); } @Test public void test_AddExtension_NoExtend() throws Exception { // ExtensionLoader.getExtensionLoader(Ext9Empty.class).getSupportedExtensions(); - ExtensionLoader.getExtensionLoader(Ext9Empty.class).addExtension("ext9", Ext9EmptyImpl.class); - Ext9Empty ext = ExtensionLoader.getExtensionLoader(Ext9Empty.class).getExtension("ext9"); + getExtensionLoader(Ext9Empty.class).addExtension("ext9", Ext9EmptyImpl.class); + Ext9Empty ext = getExtensionLoader(Ext9Empty.class).getExtension("ext9"); assertThat(ext, instanceOf(Ext9Empty.class)); - assertEquals("ext9", ExtensionLoader.getExtensionLoader(Ext9Empty.class).getExtensionName(Ext9EmptyImpl.class)); + assertEquals("ext9", getExtensionLoader(Ext9Empty.class).getExtensionName(Ext9EmptyImpl.class)); } @Test public void test_AddExtension_ExceptionWhenExistedExtension() throws Exception { - SimpleExt ext = ExtensionLoader.getExtensionLoader(SimpleExt.class).getExtension("impl1"); + SimpleExt ext = getExtensionLoader(SimpleExt.class).getExtension("impl1"); try { - ExtensionLoader.getExtensionLoader(AddExt1.class).addExtension("impl1", AddExt1_ManualAdd1.class); + getExtensionLoader(AddExt1.class).addExtension("impl1", AddExt1_ManualAdd1.class); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("Extension name impl1 already exists (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)!")); @@ -270,7 +272,7 @@ public class ExtensionLoaderTest { @Test public void test_AddExtension_Adaptive() throws Exception { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(AddExt2.class); + ExtensionLoader loader = getExtensionLoader(AddExt2.class); loader.addExtension(null, AddExt2_ManualAdaptive.class); AddExt2 adaptive = loader.getAdaptiveExtension(); @@ -279,7 +281,7 @@ public class ExtensionLoaderTest { @Test public void test_AddExtension_Adaptive_ExceptionWhenExistedAdaptive() throws Exception { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(AddExt1.class); + ExtensionLoader loader = getExtensionLoader(AddExt1.class); loader.getAdaptiveExtension(); @@ -294,30 +296,30 @@ public class ExtensionLoaderTest { @Test public void test_replaceExtension() throws Exception { try { - ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("Manual2"); + getExtensionLoader(AddExt1.class).getExtension("Manual2"); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("No such extension org.apache.dubbo.common.extension.ext8_add.AddExt1 by name Manual")); } { - AddExt1 ext = ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("impl1"); + AddExt1 ext = getExtensionLoader(AddExt1.class).getExtension("impl1"); assertThat(ext, instanceOf(AddExt1Impl1.class)); - assertEquals("impl1", ExtensionLoader.getExtensionLoader(AddExt1.class).getExtensionName(AddExt1Impl1.class)); + assertEquals("impl1", getExtensionLoader(AddExt1.class).getExtensionName(AddExt1Impl1.class)); } { - ExtensionLoader.getExtensionLoader(AddExt1.class).replaceExtension("impl1", AddExt1_ManualAdd2.class); - AddExt1 ext = ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("impl1"); + getExtensionLoader(AddExt1.class).replaceExtension("impl1", AddExt1_ManualAdd2.class); + AddExt1 ext = getExtensionLoader(AddExt1.class).getExtension("impl1"); assertThat(ext, instanceOf(AddExt1_ManualAdd2.class)); - assertEquals("impl1", ExtensionLoader.getExtensionLoader(AddExt1.class).getExtensionName(AddExt1_ManualAdd2.class)); + assertEquals("impl1", getExtensionLoader(AddExt1.class).getExtensionName(AddExt1_ManualAdd2.class)); } } @Test public void test_replaceExtension_Adaptive() throws Exception { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(AddExt3.class); + ExtensionLoader loader = getExtensionLoader(AddExt3.class); AddExt3 adaptive = loader.getAdaptiveExtension(); assertFalse(adaptive instanceof AddExt3_ManualAdaptive); @@ -330,10 +332,10 @@ public class ExtensionLoaderTest { @Test public void test_replaceExtension_ExceptionWhenNotExistedExtension() throws Exception { - AddExt1 ext = ExtensionLoader.getExtensionLoader(AddExt1.class).getExtension("impl1"); + AddExt1 ext = getExtensionLoader(AddExt1.class).getExtension("impl1"); try { - ExtensionLoader.getExtensionLoader(AddExt1.class).replaceExtension("NotExistedExtension", AddExt1_ManualAdd1.class); + getExtensionLoader(AddExt1.class).replaceExtension("NotExistedExtension", AddExt1_ManualAdd1.class); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("Extension name NotExistedExtension doesn't exist (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)")); @@ -342,7 +344,7 @@ public class ExtensionLoaderTest { @Test public void test_replaceExtension_Adaptive_ExceptionWhenNotExistedExtension() throws Exception { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(AddExt4.class); + ExtensionLoader loader = getExtensionLoader(AddExt4.class); try { loader.replaceExtension(null, AddExt4_ManualAdaptive.class); @@ -354,7 +356,7 @@ public class ExtensionLoaderTest { @Test public void test_InitError() throws Exception { - ExtensionLoader loader = ExtensionLoader.getExtensionLoader(InitErrorExt.class); + ExtensionLoader loader = getExtensionLoader(InitErrorExt.class); loader.getExtension("ok"); @@ -371,21 +373,21 @@ public class ExtensionLoaderTest { public void testLoadActivateExtension() throws Exception { // test default URL url = URL.valueOf("test://localhost/test"); - List list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + List list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "default_group"); Assertions.assertEquals(1, list.size()); Assertions.assertSame(list.get(0).getClass(), ActivateExt1Impl1.class); // test group url = url.addParameter(GROUP_KEY, "group1"); - list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "group1"); Assertions.assertEquals(1, list.size()); Assertions.assertSame(list.get(0).getClass(), GroupActivateExtImpl.class); // test old @Activate group url = url.addParameter(GROUP_KEY, "old_group"); - list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "old_group"); Assertions.assertEquals(2, list.size()); Assertions.assertTrue(list.get(0).getClass() == OldActivateExt1Impl2.class @@ -395,7 +397,7 @@ public class ExtensionLoaderTest { url = url.removeParameter(GROUP_KEY); url = url.addParameter(GROUP_KEY, "value"); url = url.addParameter("value", "value"); - list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "value"); Assertions.assertEquals(1, list.size()); Assertions.assertSame(list.get(0).getClass(), ValueActivateExtImpl.class); @@ -403,7 +405,7 @@ public class ExtensionLoaderTest { // test order url = URL.valueOf("test://localhost/test"); url = url.addParameter(GROUP_KEY, "order"); - list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "order"); Assertions.assertEquals(2, list.size()); Assertions.assertSame(list.get(0).getClass(), OrderActivateExtImpl1.class); @@ -414,14 +416,14 @@ public class ExtensionLoaderTest { public void testLoadDefaultActivateExtension() throws Exception { // test default URL url = URL.valueOf("test://localhost/test?ext=order1,default"); - List list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + List list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, "ext", "default_group"); Assertions.assertEquals(2, list.size()); Assertions.assertSame(list.get(0).getClass(), OrderActivateExtImpl1.class); Assertions.assertSame(list.get(1).getClass(), ActivateExt1Impl1.class); url = URL.valueOf("test://localhost/test?ext=default,order1"); - list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) + list = getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, "ext", "default_group"); Assertions.assertEquals(2, list.size()); Assertions.assertSame(list.get(0).getClass(), ActivateExt1Impl1.class); @@ -431,7 +433,7 @@ public class ExtensionLoaderTest { @Test public void testInjectExtension() { // test default - InjectExt injectExt = ExtensionLoader.getExtensionLoader(InjectExt.class).getExtension("injection"); + InjectExt injectExt = getExtensionLoader(InjectExt.class).getExtension("injection"); InjectExtImpl injectExtImpl = (InjectExtImpl) injectExt; Assertions.assertNotNull(injectExtImpl.getSimpleExt()); Assertions.assertNull(injectExtImpl.getSimpleExt1()); @@ -440,13 +442,28 @@ public class ExtensionLoaderTest { @Test void testMultiNames() { - Ext10MultiNames ext10MultiNames = ExtensionLoader.getExtensionLoader(Ext10MultiNames.class).getExtension("impl"); + Ext10MultiNames ext10MultiNames = getExtensionLoader(Ext10MultiNames.class).getExtension("impl"); Assertions.assertNotNull(ext10MultiNames); - ext10MultiNames = ExtensionLoader.getExtensionLoader(Ext10MultiNames.class).getExtension("implMultiName"); + ext10MultiNames = getExtensionLoader(Ext10MultiNames.class).getExtension("implMultiName"); Assertions.assertNotNull(ext10MultiNames); Assertions.assertThrows( IllegalStateException.class, - () -> ExtensionLoader.getExtensionLoader(Ext10MultiNames.class).getExtension("impl,implMultiName") + () -> getExtensionLoader(Ext10MultiNames.class).getExtension("impl,implMultiName") ); } + + @Test + public void testGetOrDefaultExtension() { + ExtensionLoader loader = getExtensionLoader(InjectExt.class); + InjectExt injectExt = loader.getOrDefaultExtension("non-exists"); + assertEquals(InjectExtImpl.class, injectExt.getClass()); + assertEquals(InjectExtImpl.class, loader.getOrDefaultExtension("injection").getClass()); + } + + @Test + public void testGetSupported() { + ExtensionLoader loader = getExtensionLoader(InjectExt.class); + assertEquals(1, loader.getSupportedExtensions().size()); + assertEquals(Collections.singleton("injection"), loader.getSupportedExtensions()); + } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableActionTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableActionTest.java new file mode 100644 index 0000000000..977b730cbe --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableActionTest.java @@ -0,0 +1,37 @@ +/* + * 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.common.function; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.apache.dubbo.common.function.ThrowableAction.execute; + +/** + * {@link ThrowableAction} Test + * + * @since 2.7.4 + */ +public class ThrowableActionTest { + + @Test + public void testExecute() { + Assertions.assertThrows(RuntimeException.class, () -> execute(() -> { + throw new Exception("Test"); + }), "Test"); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableConsumerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableConsumerTest.java new file mode 100644 index 0000000000..475974c519 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableConsumerTest.java @@ -0,0 +1,37 @@ +/* + * 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.common.function; + +import org.junit.jupiter.api.Test; + +import static org.apache.dubbo.common.function.ThrowableConsumer.execute; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * {@link ThrowableConsumer} Test + * + * @since 2.7.4 + */ +public class ThrowableConsumerTest { + + @Test + public void testExecute() { + assertThrows(RuntimeException.class, () -> execute("Hello,World", m -> { + throw new Exception(m); + }), "Hello,World"); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableFunctionTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableFunctionTest.java new file mode 100644 index 0000000000..abd2bee651 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/function/ThrowableFunctionTest.java @@ -0,0 +1,37 @@ +/* + * 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.common.function; + +import org.junit.jupiter.api.Test; + +import static org.apache.dubbo.common.function.ThrowableConsumer.execute; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * {@link ThrowableFunction} Test + * + * @since 2.7.4 + */ +public class ThrowableFunctionTest { + + @Test + public void testExecute() { + assertThrows(RuntimeException.class, () -> execute("Hello,World", m -> { + throw new Exception(m); + }), "Hello,World"); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/lang/DefaultShutdownHookCallback.java b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/DefaultShutdownHookCallback.java new file mode 100644 index 0000000000..35256c1bb1 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/DefaultShutdownHookCallback.java @@ -0,0 +1,36 @@ +/* + * 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.common.lang; + +/** + * Default {@link ShutdownHookCallback} + * + * @since 2.7.4 + */ +public class DefaultShutdownHookCallback implements ShutdownHookCallback { + + private boolean executed = false; + + @Override + public void callback() throws Throwable { + executed = true; + } + + public boolean isExecuted() { + return executed; + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/lang/PrioritizedTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/PrioritizedTest.java index 3ee027f25b..3bb57535c7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/lang/PrioritizedTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/PrioritizedTest.java @@ -41,7 +41,7 @@ public class PrioritizedTest { @Test public void testGetPriority() { - assertEquals(Prioritized.MIN_PRIORITY, new Prioritized() { + assertEquals(Prioritized.NORMAL_PRIORITY, new Prioritized() { }.getPriority()); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/lang/ShutdownHookCallbacksTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/ShutdownHookCallbacksTest.java index 9a1ea895f7..a2ab0b4621 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/lang/ShutdownHookCallbacksTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/lang/ShutdownHookCallbacksTest.java @@ -16,10 +16,42 @@ */ package org.apache.dubbo.common.lang; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * {@link ShutdownHookCallbacks} * * @since 2.7.4 */ public class ShutdownHookCallbacksTest { + + private ShutdownHookCallbacks callbacks; + + @BeforeEach + public void init() { + callbacks = new ShutdownHookCallbacks(); + } + + @Test + public void testSingleton() { + assertNotNull(callbacks); + } + + @Test + public void testCallback() { + callbacks.callback(); + DefaultShutdownHookCallback callback = (DefaultShutdownHookCallback) callbacks.getCallbacks().iterator().next(); + assertTrue(callback.isExecuted()); + } + + @AfterEach + public void destroy() { + callbacks.clear(); + assertTrue(callbacks.getCallbacks().isEmpty()); + } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DefaultCharSequence.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DefaultCharSequence.java new file mode 100644 index 0000000000..e00d4acbca --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DefaultCharSequence.java @@ -0,0 +1,45 @@ +/* + * 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.common.utils; + +import org.apache.dubbo.common.lang.Prioritized; + +/** + * Default {@link CharSequence} + * + * @since 2.7.4 + */ +public class DefaultCharSequence implements CharSequence, Prioritized { + @Override + public int length() { + return 0; + } + + @Override + public char charAt(int index) { + return 0; + } + + @Override + public CharSequence subSequence(int start, int end) { + return null; + } + + public int getPriority() { + return MAX_PRIORITY; + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboServiceLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboServiceLoaderTest.java new file mode 100644 index 0000000000..3456d2432b --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboServiceLoaderTest.java @@ -0,0 +1,40 @@ +/* + * 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.common.utils; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.apache.dubbo.common.utils.DubboServiceLoader.loadServices; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link DubboServiceLoader} Test + * + * @since 2.7.4 + */ +public class DubboServiceLoaderTest { + + @Test + public void testLoad() { + List charSequences = loadServices(CharSequence.class); + assertEquals(DefaultCharSequence.class, charSequences.get(0).getClass()); + assertEquals(String.class, charSequences.get(1).getClass()); + assertEquals(StringBuilder.class, charSequences.get(2).getClass()); + } +} diff --git a/dubbo-common/src/test/resources/META-INF/services/java.lang.CharSequence b/dubbo-common/src/test/resources/META-INF/services/java.lang.CharSequence new file mode 100644 index 0000000000..0fb689757b --- /dev/null +++ b/dubbo-common/src/test/resources/META-INF/services/java.lang.CharSequence @@ -0,0 +1,3 @@ +java.lang.String +java.lang.StringBuilder +org.apache.dubbo.common.utils.DefaultCharSequence \ No newline at end of file diff --git a/dubbo-common/src/test/resources/META-INF/services/org.apache.dubbo.common.lang.ShutdownHookCallback b/dubbo-common/src/test/resources/META-INF/services/org.apache.dubbo.common.lang.ShutdownHookCallback new file mode 100644 index 0000000000..a09ea1e981 --- /dev/null +++ b/dubbo-common/src/test/resources/META-INF/services/org.apache.dubbo.common.lang.ShutdownHookCallback @@ -0,0 +1 @@ +org.apache.dubbo.common.lang.DefaultShutdownHookCallback \ No newline at end of file diff --git a/dubbo-common/src/test/resources/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive b/dubbo-common/src/test/resources/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive new file mode 100644 index 0000000000..1ee9dc92de --- /dev/null +++ b/dubbo-common/src/test/resources/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive @@ -0,0 +1,12 @@ +package org.apache.dubbo.common.extension.adaptive; +import org.apache.dubbo.common.extension.ExtensionLoader; +public class HasAdaptiveExt$Adaptive implements org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt { +public java.lang.String echo(org.apache.dubbo.common.URL arg0, java.lang.String arg1) { +if (arg0 == null) throw new IllegalArgumentException("url == null"); +org.apache.dubbo.common.URL url = arg0; +String extName = url.getParameter("has.adaptive.ext", "adaptive"); +if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt) name from url (" + url.toString() + ") use keys([has.adaptive.ext])"); +org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt extension = (org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt)ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.extension.adaptive.HasAdaptiveExt.class).getExtension(extName); +return extension.echo(arg0, arg1); +} +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index 257cf5d4a5..e0bb4671e1 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -59,10 +59,9 @@ import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_SE import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; -import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_PROTOCOL; -import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE; import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; +import static org.apache.dubbo.common.utils.UrlUtils.isServiceDiscoveryRegistryType; import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; import static org.apache.dubbo.config.Constants.LAYER_KEY; import static org.apache.dubbo.config.Constants.LISTENER_KEY; @@ -308,9 +307,7 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig { } private String extractRegistryType(URL url) { - boolean isServiceRegistry = url.getParameter(REGISTRY_TYPE_KEY) != null - && SERVICE_REGISTRY_TYPE.equals(url.getParameter(REGISTRY_TYPE_KEY)); - return isServiceRegistry ? SERVICE_REGISTRY_PROTOCOL : REGISTRY_PROTOCOL; + return isServiceDiscoveryRegistryType(url) ? SERVICE_REGISTRY_PROTOCOL : REGISTRY_PROTOCOL; } /** diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizer.java index 25c4d20353..bac27b6c59 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizer.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizer.java @@ -31,8 +31,7 @@ public class ServiceInstancePortCustomizer implements ServiceInstanceCustomizer @Override public void customize(ServiceInstance serviceInstance) { - if (serviceInstance.getPort() != null - || serviceInstance.getPort().intValue() < 1) { + if (serviceInstance.getPort() != null) { return; } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboServiceDestroyedEventTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboServiceDestroyedEventTest.java new file mode 100644 index 0000000000..03b7cddac6 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboServiceDestroyedEventTest.java @@ -0,0 +1,58 @@ +/* + * 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.config.event; + +import org.apache.dubbo.config.DubboShutdownHook; +import org.apache.dubbo.event.EventDispatcher; +import org.apache.dubbo.event.EventListener; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link DubboServiceDestroyedEvent} Test + * + * @since 2.7.4 + */ +public class DubboServiceDestroyedEventTest implements EventListener { + + private DubboServiceDestroyedEvent event; + + private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); + + private DubboShutdownHook dubboShutdownHook; + + @BeforeEach + public void init() { + eventDispatcher.removeAllEventListeners(); + eventDispatcher.addEventListener(this); + dubboShutdownHook = DubboShutdownHook.getDubboShutdownHook(); + } + + @Test + public void testOnEvent() { + dubboShutdownHook.doDestroy(); + assertEquals(dubboShutdownHook, event.getSource()); + } + + @Override + public void onEvent(DubboServiceDestroyedEvent event) { + this.event = event; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookRegisteredEventTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookRegisteredEventTest.java new file mode 100644 index 0000000000..ec2a850311 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookRegisteredEventTest.java @@ -0,0 +1,59 @@ +/* + * 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.config.event; + +import org.apache.dubbo.config.DubboShutdownHook; +import org.apache.dubbo.event.EventDispatcher; +import org.apache.dubbo.event.EventListener; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link DubboShutdownHookRegisteredEvent} Test + * + * @since 2.7.4 + */ +public class DubboShutdownHookRegisteredEventTest implements EventListener { + + private DubboShutdownHookRegisteredEvent event; + + private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); + + private DubboShutdownHook dubboShutdownHook; + + @BeforeEach + public void init() { + eventDispatcher.removeAllEventListeners(); + eventDispatcher.addEventListener(this); + dubboShutdownHook = DubboShutdownHook.getDubboShutdownHook(); + } + + @Test + public void testOnEvent() { + dubboShutdownHook.register(); + assertEquals(dubboShutdownHook, event.getSource()); + assertEquals(dubboShutdownHook, event.getDubboShutdownHook()); + } + + @Override + public void onEvent(DubboShutdownHookRegisteredEvent event) { + this.event = event; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookUnregisteredEventTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookUnregisteredEventTest.java new file mode 100644 index 0000000000..d4acfed787 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/DubboShutdownHookUnregisteredEventTest.java @@ -0,0 +1,60 @@ +/* + * 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.config.event; + +import org.apache.dubbo.config.DubboShutdownHook; +import org.apache.dubbo.event.EventDispatcher; +import org.apache.dubbo.event.EventListener; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link DubboShutdownHookUnregisteredEvent} Test + * + * @since 2.7.4 + */ +public class DubboShutdownHookUnregisteredEventTest implements EventListener { + + private DubboShutdownHookUnregisteredEvent event; + + private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); + + private DubboShutdownHook dubboShutdownHook; + + @BeforeEach + public void init() { + eventDispatcher.removeAllEventListeners(); + eventDispatcher.addEventListener(this); + dubboShutdownHook = DubboShutdownHook.getDubboShutdownHook(); + } + + @Test + public void testOnEvent() { + dubboShutdownHook.register(); + dubboShutdownHook.unregister(); + assertEquals(dubboShutdownHook, event.getSource()); + assertEquals(dubboShutdownHook, event.getDubboShutdownHook()); + } + + @Override + public void onEvent(DubboShutdownHookUnregisteredEvent event) { + this.event = event; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigDestroyedEventTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigDestroyedEventTest.java new file mode 100644 index 0000000000..3ca106241a --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigDestroyedEventTest.java @@ -0,0 +1,65 @@ +/* + * 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.config.event; + +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.event.EventDispatcher; +import org.apache.dubbo.event.EventListener; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.util.ReflectionUtils.findField; +import static org.springframework.util.ReflectionUtils.setField; + +/** + * {@link ReferenceConfigDestroyedEvent} Test + * + * @since 2.7.4 + */ +public class ReferenceConfigDestroyedEventTest implements EventListener { + + private ReferenceConfigDestroyedEvent event; + + private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); + + private ReferenceConfig referenceConfig; + + @BeforeEach + public void init() throws Exception { + eventDispatcher.removeAllEventListeners(); + eventDispatcher.addEventListener(this); + referenceConfig = new ReferenceConfig(); + Field field = findField(referenceConfig.getClass(), "ref"); + field.setAccessible(true); + setField(field, referenceConfig, new Object()); + } + + @Test + public void testOnEvent() { + referenceConfig.destroy(); + assertEquals(referenceConfig, event.getSource()); + } + + @Override + public void onEvent(ReferenceConfigDestroyedEvent event) { + this.event = event; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigInitializedEventTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigInitializedEventTest.java new file mode 100644 index 0000000000..77bc6b5dee --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/event/ReferenceConfigInitializedEventTest.java @@ -0,0 +1,59 @@ +/* + * 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.config.event; + +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.event.EventDispatcher; +import org.apache.dubbo.event.EventListener; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link ReferenceConfigInitializedEvent} Test + * + * @since 2.7.4 + */ +public class ReferenceConfigInitializedEventTest implements EventListener { + + private ReferenceConfigInitializedEvent event; + + private EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); + + private ReferenceConfig referenceConfig; + + @BeforeEach + public void init() throws Exception { + eventDispatcher.removeAllEventListeners(); + eventDispatcher.addEventListener(this); + referenceConfig = new ReferenceConfig(); + } + + @Test + public void testOnEvent() { + eventDispatcher.dispatch(new ReferenceConfigInitializedEvent(referenceConfig, null)); + assertEquals(referenceConfig, event.getSource()); + assertEquals(referenceConfig, event.getReferenceConfig()); + } + + @Override + public void onEvent(ReferenceConfigInitializedEvent event) { + this.event = event; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java index 6cf5aba277..d66ce0b628 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ConfigurableMetadataServiceExporterTest.java @@ -81,7 +81,7 @@ public class ConfigurableMetadataServiceExporterTest { assertEquals(MetadataService.class.getName(), url.getServiceInterface()); assertEquals("test", url.getParameter(GROUP_KEY)); assertEquals(MetadataService.VERSION, url.getParameter(VERSION_KEY)); - assertEquals("mockprotocol", url.getProtocol()); + assertEquals("dubbo", url.getProtocol()); exporter.unexport(); } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizerTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizerTest.java new file mode 100644 index 0000000000..1abdca9c4b --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/metadata/ServiceInstancePortCustomizerTest.java @@ -0,0 +1,60 @@ +/* + * 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.config.metadata; + +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.context.ConfigManager; +import org.apache.dubbo.registry.client.DefaultServiceInstance; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link ServiceInstancePortCustomizer} Test + * + * @since 2.7.4 + */ +public class ServiceInstancePortCustomizerTest { + + private ServiceInstancePortCustomizer customizer; + + private DefaultServiceInstance serviceInstance; + + @BeforeEach + + public void init() { + customizer = new ServiceInstancePortCustomizer(); + serviceInstance = new DefaultServiceInstance(); + ConfigManager.getInstance() + .addProtocol(new ProtocolConfig("rest", 9090)); + } + + @Test + public void testCustomizeWithoutSet() { + serviceInstance.setPort(8080); + customizer.customize(serviceInstance); + assertEquals(8080, serviceInstance.getPort()); + } + + @Test + public void testCustomize() { + customizer.customize(serviceInstance); + assertEquals(9090, serviceInstance.getPort()); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java index 74ca3f3c6a..1219343211 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java @@ -16,7 +16,8 @@ */ package org.apache.dubbo.config.spring.context.annotation; -import org.springframework.context.Lifecycle; +import org.apache.dubbo.common.context.Lifecycle; + import org.springframework.context.annotation.Import; import java.lang.annotation.Documented; diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycleTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycleTest.java new file mode 100644 index 0000000000..70a6daf6da --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycleTest.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.config.spring.context.annotation; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * {@link EnableDubboLifecycle} Test + * + * @since 2.7.4 + */ +@EnableDubboLifecycle +public class EnableDubboLifecycleTest { + + private AnnotationConfigApplicationContext context; + + static boolean initialized = false; + + static boolean started = false; + + @BeforeEach + public void init() { + context = new AnnotationConfigApplicationContext(EnableDubboLifecycleTest.class); + } + + @AfterEach + public void destroy() { + context.close(); + } + + @Test + public void test() { + assertTrue(initialized); + assertTrue(started); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/MyLifecycle.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/MyLifecycle.java new file mode 100644 index 0000000000..1efaee83b9 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/MyLifecycle.java @@ -0,0 +1,58 @@ +/* + * 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.config.spring.context.annotation; + +import org.apache.dubbo.common.context.Lifecycle; + +/** + * {@link Lifecycle} + */ +public class MyLifecycle implements Lifecycle { + + @Override + public Lifecycle initialize() throws IllegalStateException { + EnableDubboLifecycleTest.initialized = true; + return this; + } + + @Override + public boolean isInitialized() { + return true; + } + + @Override + public Lifecycle start() throws IllegalStateException { + initialize(); + EnableDubboLifecycleTest.started = true; + return this; + } + + @Override + public boolean isStarted() { + return true; + } + + @Override + public Lifecycle stop() throws IllegalStateException { + return this; + } + + @Override + public void destroy() throws IllegalStateException { + + } +} diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/services/org.apache.dubbo.common.context.Lifecycle b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/services/org.apache.dubbo.common.context.Lifecycle new file mode 100644 index 0000000000..c1e129692a --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/services/org.apache.dubbo.common.context.Lifecycle @@ -0,0 +1 @@ +org.apache.dubbo.config.spring.context.annotation.MyLifecycle \ No newline at end of file