diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java index df3a47ce74..98a3c2f3e1 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java @@ -386,7 +386,7 @@ public class AbstractClusterInvokerTest { public void testSelect_multiInvokers(String lbname) throws Exception { - int min = 1000, max = 5000; + int min = 100, max = 500; Double d = (Math.random() * (max - min + 1) + min); int runs = d.intValue(); Assertions.assertTrue(runs > min); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index b0fc95e6ac..b6ec83e630 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -29,6 +29,8 @@ import org.apache.dubbo.config.api.DemoService; import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.provider.impl.DemoServiceImpl; import org.apache.dubbo.registry.client.migration.MigrationInvoker; +import org.apache.dubbo.registrycenter.RegistryCenter; +import org.apache.dubbo.registrycenter.ZookeeperSingleRegistryCenter; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.listener.ListenerInvokerWrapper; @@ -39,7 +41,6 @@ import org.apache.dubbo.rpc.model.ServiceMetadata; import org.apache.dubbo.rpc.protocol.injvm.InjvmInvoker; import org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol; -import org.apache.curator.test.TestingServer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -104,16 +105,16 @@ import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL; import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; public class ReferenceConfigTest { - private TestingServer zkServer; private String zkUrl; private String registryUrl; + private RegistryCenter registryCenter; @BeforeEach public void setUp() throws Exception { DubboBootstrap.reset(); int zkServerPort = NetUtils.getAvailablePort(NetUtils.getRandomPort()); - this.zkServer = new TestingServer(zkServerPort, true); - this.zkServer.start(); + registryCenter = new ZookeeperSingleRegistryCenter(zkServerPort); + registryCenter.startup(); this.zkUrl = "zookeeper://localhost:" + zkServerPort; this.registryUrl = "registry://localhost:" + zkServerPort + "?registry=zookeeper"; @@ -126,7 +127,7 @@ public class ReferenceConfigTest { @AfterEach public void tearDown() throws IOException { DubboBootstrap.reset(); - zkServer.stop(); + registryCenter.shutdown(); Mockito.framework().clearInlineMocks(); } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/registrycenter/AbstractRegistryCenter.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/registrycenter/AbstractRegistryCenter.java index 3846c949b9..3ef9e58034 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/registrycenter/AbstractRegistryCenter.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/registrycenter/AbstractRegistryCenter.java @@ -60,7 +60,7 @@ abstract class AbstractRegistryCenter implements RegistryCenter { /** * The default value is -1. */ - private static final int DEFAULT_MAX_CLIENT_CNXNS = -1; + private static final int DEFAULT_MAX_CLIENT_CNXNS = 200; /** * The minimum session timeout. diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer/PropertyConfigurerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer/PropertyConfigurerTest.java index 37a677d49a..c2ba2c497b 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer/PropertyConfigurerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer/PropertyConfigurerTest.java @@ -61,11 +61,6 @@ public class PropertyConfigurerTest { // reset ConfigManager of provider context ApplicationModel.defaultModel().getApplicationConfigManager().destroy(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - // Resolve placeholder by PropertyPlaceholderConfigurer in dubbo-consumer.xml, without import property source. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); context.start(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer2/PropertySourcesConfigurerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer2/PropertySourcesConfigurerTest.java index 8054311e6e..08c3a04923 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer2/PropertySourcesConfigurerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer2/PropertySourcesConfigurerTest.java @@ -59,11 +59,6 @@ public class PropertySourcesConfigurerTest { try { providerContext.start(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - // reset ConfigManager of provider context ApplicationModel.defaultModel().getApplicationConfigManager().destroy(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer3/PropertySourcesInJavaConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer3/PropertySourcesInJavaConfigTest.java index afdbf250c6..bc28201f92 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer3/PropertySourcesInJavaConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/propertyconfigurer/consumer3/PropertySourcesInJavaConfigTest.java @@ -72,11 +72,6 @@ public class PropertySourcesInJavaConfigTest { try { providerContext.start(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - // reset ConfigManager of provider context ApplicationModel.defaultModel().getApplicationConfigManager().destroy(); //ApplicationModel.defaultModel().getApplicationServiceRepository().setProviderUrlsWithoutGroup(tmp); @@ -109,11 +104,6 @@ public class PropertySourcesInJavaConfigTest { try { providerContext.start(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - // reset ConfigManager of provider context ApplicationModel.defaultModel().getApplicationConfigManager().destroy(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcall/LocalCallTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcall/LocalCallTest.java index 4f5424a7c0..25fee33fea 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcall/LocalCallTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcall/LocalCallTest.java @@ -61,9 +61,6 @@ public class LocalCallTest { //@Qualifier("localHelloService") private HelloService localHelloService; - @Autowired - private ApplicationContext applicationContext; - @Test public void testLocalCall() { // see also: org.apache.dubbo.rpc.protocol.injvm.InjvmInvoker.doInvoke diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcallam/LocalCallMultipleReferenceAnnotationsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcallam/LocalCallMultipleReferenceAnnotationsTest.java index 241ed75f54..2da37326f0 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcallam/LocalCallMultipleReferenceAnnotationsTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/localcallam/LocalCallMultipleReferenceAnnotationsTest.java @@ -22,6 +22,7 @@ import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.api.HelloService; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; +import org.apache.dubbo.config.spring.registrycenter.RegistryCenter; import org.apache.dubbo.config.spring.registrycenter.ZookeeperSingleRegistryCenter; import org.apache.dubbo.rpc.RpcContext; import org.junit.jupiter.api.AfterAll; @@ -48,7 +49,7 @@ import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) public class LocalCallMultipleReferenceAnnotationsTest { - private static ZookeeperSingleRegistryCenter registryCenter; + private static RegistryCenter registryCenter; @BeforeAll public static void setUp() { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java index b898c93135..c4ae5caf64 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/registrycenter/AbstractRegistryCenter.java @@ -53,14 +53,14 @@ abstract class AbstractRegistryCenter implements RegistryCenter { private static final int DEFAULT_SERVER_ID = -1; /** - * The default tick time is 10000 + * The default tick time is 5000 */ - private static final int DEFAULT_TICK_TIME = 100 * 1000; + private static final int DEFAULT_TICK_TIME = 5 * 1000; /** - * The default value is -1. + * The default value is 60. */ - private static final int DEFAULT_MAX_CLIENT_CNXNS = -1; + private static final int DEFAULT_MAX_CLIENT_CNXNS = 200; /** * The minimum session timeout. diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceTest.java index 57c0b83c25..988919c460 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceTest.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.config.spring.schema; -import org.apache.dubbo.common.utils.ClassUtils; -import org.apache.dubbo.config.ReferenceConfigBase; import org.apache.dubbo.config.ServiceConfigBase; import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.context.ConfigManager; @@ -27,7 +25,6 @@ import org.apache.dubbo.config.spring.registrycenter.ZookeeperSingleRegistryCent import org.apache.dubbo.config.spring.registrycenter.RegistryCenter; import org.apache.dubbo.rpc.service.GenericService; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; @@ -59,22 +56,14 @@ public class GenericServiceTest { @AfterAll public static void afterAll() { - singleRegistryCenter.shutdown(); - } - - @AfterEach - public void tearDown() { DubboBootstrap.reset(); + singleRegistryCenter.shutdown(); } @Autowired @Qualifier("demoServiceRef") private GenericService demoServiceRef; - @Autowired - @Qualifier("genericServiceWithoutInterfaceRef") - private GenericService genericServiceWithoutInterfaceRef; - @Autowired @Qualifier("demoService") private ServiceBean serviceBean; @@ -92,15 +81,5 @@ public class GenericServiceTest { Object result = demoServiceRef.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"dubbo"}); Assertions.assertEquals("Welcome dubbo", result); - - // Test generic service without interface class locally - result = genericServiceWithoutInterfaceRef.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"generic"}); - Assertions.assertEquals("Welcome generic", result); - - ReferenceConfigBase reference = configManager.getReference("genericServiceWithoutInterfaceRef"); - Assertions.assertNull(reference.getServiceInterfaceClass()); - Assertions.assertEquals("org.apache.dubbo.config.spring.api.LocalMissClass", reference.getInterface()); - Assertions.assertThrows(ClassNotFoundException.class, () -> ClassUtils.forName(reference.getInterface())); - } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceWithoutInterfaceTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceWithoutInterfaceTest.java new file mode 100644 index 0000000000..f45fa00198 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/GenericServiceWithoutInterfaceTest.java @@ -0,0 +1,76 @@ +/* + * 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.schema; + +import org.apache.dubbo.common.utils.ClassUtils; +import org.apache.dubbo.config.ReferenceConfigBase; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.spring.registrycenter.RegistryCenter; +import org.apache.dubbo.config.spring.registrycenter.ZookeeperSingleRegistryCenter; +import org.apache.dubbo.rpc.service.GenericService; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.ImportResource; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = GenericServiceWithoutInterfaceTest.class) +@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) +@ImportResource(locations = "classpath:/META-INF/spring/dubbo-generic-consumer-without-interface.xml") +public class GenericServiceWithoutInterfaceTest { + + private static RegistryCenter singleRegistryCenter; + + @BeforeAll + public static void beforeAll() { + singleRegistryCenter = new ZookeeperSingleRegistryCenter(); + singleRegistryCenter.startup(); + DubboBootstrap.reset(); + } + + @AfterAll + public static void afterAll() { + DubboBootstrap.reset(); + singleRegistryCenter.shutdown(); + } + + @Autowired + @Qualifier("genericServiceWithoutInterfaceRef") + private GenericService genericServiceWithoutInterfaceRef; + + @Test + public void testGenericWithoutInterface() { + + // Test generic service without interface class locally + Object result = genericServiceWithoutInterfaceRef.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"generic"}); + Assertions.assertEquals("Welcome generic", result); + + ReferenceConfigBase reference = DubboBootstrap.getInstance().getConfigManager().getReference("genericServiceWithoutInterfaceRef"); + Assertions.assertNull(reference.getServiceInterfaceClass()); + Assertions.assertEquals("org.apache.dubbo.config.spring.api.LocalMissClass", reference.getInterface()); + Assertions.assertThrows(ClassNotFoundException.class, () -> ClassUtils.forName(reference.getInterface())); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer-without-interface.xml b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer-without-interface.xml new file mode 100644 index 0000000000..002f8f69e7 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer-without-interface.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer.xml b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer.xml index fd5dde5f08..e426e22a7c 100644 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer.xml +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-generic-consumer.xml @@ -33,9 +33,4 @@ - - - - diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index a62797bc94..3594718189 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -100,7 +100,7 @@ 4.4.6 1.2.70 3.4.13 - 4.0.1 + 4.1.0 2.12.0 3.6.0 1.4.2 diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/PortTelnetTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/PortTelnetTest.java index 2a268d942b..dbbc476b83 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/PortTelnetTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/PortTelnetTest.java @@ -73,7 +73,7 @@ public class PortTelnetTest { public void testListClient() throws Exception { ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:" + availablePort + "/demo"); ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:" + availablePort + "/demo"); - Thread.sleep(5000); + Thread.sleep(100); String result = port.execute(mockCommandContext, new String[]{"-l", availablePort + ""}); String client1Addr = client1.getLocalAddress().toString(); String client2Addr = client2.getLocalAddress().toString(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java index 0779bb15b3..99a8da28d2 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java @@ -190,7 +190,7 @@ public class DubboProtocolTest { service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", 3000L))); long start = System.currentTimeMillis(); - for (int i = 0; i < 1000; i++) + for (int i = 0; i < 100; i++) service.getSize(new String[]{"", "", ""}); System.out.println("take:" + (System.currentTimeMillis() - start)); } diff --git a/dubbo-test/dubbo-test-common/pom.xml b/dubbo-test/dubbo-test-common/pom.xml index 9c529519ef..0cbfa346a9 100644 --- a/dubbo-test/dubbo-test-common/pom.xml +++ b/dubbo-test/dubbo-test-common/pom.xml @@ -57,5 +57,16 @@ org.apache.dubbo dubbo-rpc-rest + + org.apache.curator + curator-test + compile + + + com.google.guava + guava + + + diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/EmbeddedZooKeeper.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/EmbeddedZooKeeper.java index e91755a252..c96f06f2a1 100644 --- a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/EmbeddedZooKeeper.java +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/EmbeddedZooKeeper.java @@ -39,7 +39,11 @@ import java.util.UUID; * @author Patrick Peralta * @author Mark Fisher * @author David Turanski + * @deprecated + * @see org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter + * @see org.apache.dubbo.test.common.registrycenter.ZookeeperMultipleRegistryCenter */ +@Deprecated public class EmbeddedZooKeeper { /** diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/ZooKeeperServer.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/ZooKeeperServer.java index 115510a786..846c2acc7b 100644 --- a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/ZooKeeperServer.java +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/ZooKeeperServer.java @@ -16,6 +16,15 @@ */ package org.apache.dubbo.test.common; +/** + * Using this class as registry center is not very well because of time-consuming. + *

The alternative is to use {@link org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter}

or + * {@link org.apache.dubbo.test.common.registrycenter.ZookeeperMultipleRegistryCenter} + * @deprecated + * @see org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter + * @see org.apache.dubbo.test.common.registrycenter.ZookeeperMultipleRegistryCenter + */ +@Deprecated public class ZooKeeperServer { private static EmbeddedZooKeeper zookeeper1; diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/AbstractRegistryCenter.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/AbstractRegistryCenter.java new file mode 100644 index 0000000000..cbf637ac86 --- /dev/null +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/AbstractRegistryCenter.java @@ -0,0 +1,107 @@ +/* + * 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.test.common.registrycenter; + +import org.apache.curator.test.InstanceSpec; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * The abstraction of {@link RegistryCenter} implements the basic methods. + */ +abstract class AbstractRegistryCenter implements RegistryCenter { + + /** + * The default data directory is null + */ + private static final File DEFAULT_DATA_DIRECTORY = null; + + /** + * The default election port is -1. + */ + private static final int DEFAULT_ELECTION_PORT = -1; + + /** + * The default quorum port is -1. + */ + private static final int DEFAULT_QUORUM_PORT = -1; + + /** + * The default value is true. + */ + private static final boolean DEFAULT_DELETE_DATA_DIRECTORY_ON_CLOSE = true; + + /** + * The default service id is -1. + */ + private static final int DEFAULT_SERVER_ID = -1; + + /** + * The default tick time is 5000 + */ + private static final int DEFAULT_TICK_TIME = 5 * 1000; + + /** + * The default value is 60. + */ + private static final int DEFAULT_MAX_CLIENT_CNXNS = 200; + + /** + * The minimum session timeout. + */ + private static final int DEFAULT_MINIMUM_SESSION_TIMEOUT = DEFAULT_TICK_TIME * 2; + + /** + * The maximum session timeout. + */ + private static final int DEFAULT_MAXIMUM_SESSION_TIMEOUT = 60 * 1000; + + /** + * The default customer properties. + */ + private static final Map DEFAULT_CUSTOM_PROPERTIES = new HashMap<>(2); + + /** + * The default hostname. + */ + private static final String DEFAULT_HOSTNAME = "127.0.0.1"; + + static { + DEFAULT_CUSTOM_PROPERTIES.put("minSessionTimeout", DEFAULT_MINIMUM_SESSION_TIMEOUT); + DEFAULT_CUSTOM_PROPERTIES.put("maxSessionTimeout", DEFAULT_MAXIMUM_SESSION_TIMEOUT); + } + + /** + * Create an {@link InstanceSpec} instance to initialize {@link org.apache.curator.test.TestingServer} + * + * @param port the zookeeper server's port. + */ + protected InstanceSpec createInstanceSpec(int port) { + return new InstanceSpec(DEFAULT_DATA_DIRECTORY, + port, + DEFAULT_ELECTION_PORT, + DEFAULT_QUORUM_PORT, + DEFAULT_DELETE_DATA_DIRECTORY_ON_CLOSE, + DEFAULT_SERVER_ID, + DEFAULT_TICK_TIME, + DEFAULT_MAX_CLIENT_CNXNS, + DEFAULT_CUSTOM_PROPERTIES, + DEFAULT_HOSTNAME); + } +} diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/RegistryCenter.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/RegistryCenter.java new file mode 100644 index 0000000000..8e9c569b55 --- /dev/null +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/RegistryCenter.java @@ -0,0 +1,70 @@ +/* + * 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.test.common.registrycenter; + +import org.apache.dubbo.rpc.RpcException; + +import java.util.List; + +/** + * The mock registry center. + */ +public interface RegistryCenter { + + /** + * Start the registry center. + * + * @throws RpcException when an exception occurred + */ + void startup() throws RpcException; + + /** + * Returns the registry center instance. + *

This method can be called only after {@link #startup()} is called

+ * + * @throws RpcException when an exception occurred + */ + List getRegistryCenterInstance() throws RpcException; + + /** + * Stop the registry center. + * + * @throws RpcException when an exception occurred + */ + void shutdown() throws RpcException; + + /** + * The registry center instance. + */ + interface Instance { + + /** + * Returns the registry center type. + */ + String getType(); + + /** + * Returns the hostname of registry center. + */ + String getHostname(); + + /** + * Returns the port of registry center. + */ + int getPort(); + } +} diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperMultipleRegistryCenter.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperMultipleRegistryCenter.java new file mode 100644 index 0000000000..761558ad4e --- /dev/null +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperMultipleRegistryCenter.java @@ -0,0 +1,43 @@ +/* + * 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.test.common.registrycenter; + +/** + * The default zookeeper multiple registry center. + */ +public class ZookeeperMultipleRegistryCenter extends ZookeeperRegistryCenter { + + /** + * Initialize {@link ZookeeperMultipleRegistryCenter} instance. + * + * @param port1 the zookeeper server's port. + * @param port2 the zookeeper server's port. + */ + public ZookeeperMultipleRegistryCenter(int port1, int port2) { + super(port1, port2); + } + + /** + * Initialize {@link ZookeeperMultipleRegistryCenter} instance. + */ + public ZookeeperMultipleRegistryCenter() { + this(DEFAULT_PORT1, DEFAULT_PORT2); + } + + private static final int DEFAULT_PORT1 = 2181; + private static final int DEFAULT_PORT2 = 2182; +} diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperRegistryCenter.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperRegistryCenter.java new file mode 100644 index 0000000000..4b2e3b66b5 --- /dev/null +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperRegistryCenter.java @@ -0,0 +1,142 @@ +/* + * 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.test.common.registrycenter; + +import org.apache.curator.test.InstanceSpec; +import org.apache.curator.test.TestingServer; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.rpc.RpcException; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * The default implementation of registry center can support single and multiple registry center. + *

Each port represents an instance. You can provide multiple ports to build multiple registry center, + * if you want to create multiple registry center + */ +class ZookeeperRegistryCenter extends AbstractRegistryCenter { + + /** + * Initialize the default registry center. + * + * @param ports the registry center's ports. + */ + public ZookeeperRegistryCenter(int... ports) { + this.ports = ports; + this.instanceSpecs = new ArrayList<>(this.ports.length); + this.zookeeperServers = new ArrayList<>(this.ports.length); + } + + private static final Logger logger = LoggerFactory.getLogger(ZookeeperRegistryCenter.class); + + /** + * The type of the registry center. + */ + private static final String DEFAULT_REGISTRY_CENTER_TYPE = "zookeeper"; + + private int[] ports; + + private List instanceSpecs; + + private List zookeeperServers; + + private AtomicBoolean started = new AtomicBoolean(false); + + /** + * {@inheritDoc} + */ + @Override + public void startup() throws RpcException { + try { + if (started.compareAndSet(false, true)) { + logger.info("The ZookeeperRegistryCenter is starting..."); + for (int port : this.ports) { + InstanceSpec instanceSpec = this.createInstanceSpec(port); + this.instanceSpecs.add(instanceSpec); + this.zookeeperServers.add(new TestingServer(instanceSpec, true)); + } + logger.info("The ZookeeperRegistryCenter is started successfully"); + } + } catch (Exception exception) { + started.set(false); + throw new RpcException("Failed to initialize ZookeeperRegistryCenter instance", exception); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List getRegistryCenterInstance() throws RpcException { + this.startup(); + List instances = new ArrayList<>(this.instanceSpecs.size()); + for (InstanceSpec instanceSpec : this.instanceSpecs) { + instances.add(new Instance() { + @Override + public String getType() { + return DEFAULT_REGISTRY_CENTER_TYPE; + } + + @Override + public String getHostname() { + return instanceSpec.getHostname(); + } + + @Override + public int getPort() { + return instanceSpec.getPort(); + } + }); + } + return instances; + } + + /** + * {@inheritDoc} + */ + @Override + public void shutdown() throws RpcException { + logger.info("The ZookeeperRegistryCenter is stopping..."); + List exceptions = new ArrayList<>(this.zookeeperServers.size()); + for (TestingServer testingServer : this.zookeeperServers) { + try { + testingServer.close(); + logger.info(String.format("The zookeeper instance of %s is shutdown successfully", + testingServer.getConnectString())); + } catch (IOException exception) { + RpcException rpcException = new RpcException(String.format("Failed to close zookeeper instance of %s", + testingServer.getConnectString()), + exception); + exceptions.add(rpcException); + logger.error(rpcException); + } + } + this.instanceSpecs.clear(); + this.zookeeperServers.clear(); + if (!exceptions.isEmpty()) { + logger.info("The ZookeeperRegistryCenter failed to close."); + // throw any one of exceptions + throw exceptions.get(0); + } else { + logger.info("The ZookeeperRegistryCenter close successfully."); + } + } +} diff --git a/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperSingleRegistryCenter.java b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperSingleRegistryCenter.java new file mode 100644 index 0000000000..9f0e0100a3 --- /dev/null +++ b/dubbo-test/dubbo-test-common/src/main/java/org/apache/dubbo/test/common/registrycenter/ZookeeperSingleRegistryCenter.java @@ -0,0 +1,44 @@ +/* + * 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.test.common.registrycenter; + +/** + * The default zookeeper single registry center. + */ +public class ZookeeperSingleRegistryCenter extends ZookeeperRegistryCenter { + + /** + * Initialize {@link ZookeeperSingleRegistryCenter} instance. + */ + public ZookeeperSingleRegistryCenter() { + this(DEFAULT_PORT); + } + + /** + * Initialize {@link RegistryCenter} instance. + * + * @param port the zookeeper server's port. + */ + public ZookeeperSingleRegistryCenter(int port) { + super(port); + } + + /** + * The zookeeper server's default port. + */ + private static final int DEFAULT_PORT = 2181; +} diff --git a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringAnnotationBeanTest.java b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringAnnotationBeanTest.java index 9398865643..e06b8f3d4e 100644 --- a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringAnnotationBeanTest.java +++ b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringAnnotationBeanTest.java @@ -19,11 +19,13 @@ package org.apache.dubbo.test.spring; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; -import org.apache.dubbo.test.common.ZooKeeperServer; import org.apache.dubbo.test.common.api.DemoService; +import org.apache.dubbo.test.common.registrycenter.RegistryCenter; +import org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter; import org.apache.dubbo.test.spring.context.MockSpringInitializationCustomizer; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -31,12 +33,22 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; public class SpringAnnotationBeanTest { + + private static RegistryCenter registryCenter; + @BeforeAll public static void beforeAll() { - ZooKeeperServer.start(); + registryCenter = new ZookeeperSingleRegistryCenter(); + registryCenter.startup(); DubboBootstrap.reset(); } + @AfterAll + public static void afterAll(){ + DubboBootstrap.reset(); + registryCenter.shutdown(); + } + @Test public void test() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfiguration.class); diff --git a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringJavaConfigBeanTest.java b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringJavaConfigBeanTest.java index fbff776af7..79392d8592 100644 --- a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringJavaConfigBeanTest.java +++ b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringJavaConfigBeanTest.java @@ -30,15 +30,17 @@ import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.apache.dubbo.rpc.Constants; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.test.common.SysProps; -import org.apache.dubbo.test.common.ZooKeeperServer; import org.apache.dubbo.test.common.api.DemoService; import org.apache.dubbo.test.common.impl.DemoServiceImpl; +import org.apache.dubbo.test.common.registrycenter.RegistryCenter; +import org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter; import org.apache.dubbo.test.spring.context.MockSpringInitializationCustomizer; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -52,12 +54,21 @@ public class SpringJavaConfigBeanTest { private static final String MY_PROTOCOL_ID = "myProtocol"; private static final String MY_REGISTRY_ID = "my-registry"; + private static RegistryCenter registryCenter; + @BeforeAll public static void beforeAll() { - ZooKeeperServer.start(); + registryCenter = new ZookeeperSingleRegistryCenter(); + registryCenter.startup(); DubboBootstrap.reset(); } + @AfterAll + public static void afterAll(){ + DubboBootstrap.reset(); + registryCenter.shutdown(); + } + @BeforeEach public void beforeEach() { DubboBootstrap.reset(); diff --git a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringXmlConfigTest.java b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringXmlConfigTest.java index d600ca2488..ace14ecdee 100644 --- a/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringXmlConfigTest.java +++ b/dubbo-test/dubbo-test-spring/src/main/java/org/apache/dubbo/test/spring/SpringXmlConfigTest.java @@ -18,11 +18,13 @@ package org.apache.dubbo.test.spring; import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.test.common.SysProps; -import org.apache.dubbo.test.common.ZooKeeperServer; import org.apache.dubbo.test.common.api.DemoService; import org.apache.dubbo.test.common.api.GreetingService; import org.apache.dubbo.test.common.api.RestDemoService; +import org.apache.dubbo.test.common.registrycenter.RegistryCenter; +import org.apache.dubbo.test.common.registrycenter.ZookeeperSingleRegistryCenter; import org.apache.dubbo.test.spring.context.MockSpringInitializationCustomizer; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -32,12 +34,21 @@ import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KE public class SpringXmlConfigTest { + private static RegistryCenter registryCenter; + @BeforeAll public static void beforeAll() { - ZooKeeperServer.start(); + registryCenter = new ZookeeperSingleRegistryCenter(); + registryCenter.startup(); DubboBootstrap.reset(); } + @AfterAll + public static void afterAll(){ + DubboBootstrap.reset(); + registryCenter.shutdown(); + } + @Test public void test() { SysProps.setProperty(SHUTDOWN_WAIT_KEY, "2000");