Skip to content

Commit

Permalink
[Feature] Support the global registry center in unit tests (apache#9138)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinxiong authored Nov 6, 2021
1 parent 57ab318 commit b06e5c2
Show file tree
Hide file tree
Showing 166 changed files with 2,301 additions and 2,120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -60,11 +61,11 @@ public void testToUrl() {
ConfigCenterConfig config = new ConfigCenterConfig();
config.setNamespace("namespace");
config.setGroup("group");
config.setAddress("zookeeper://127.0.0.1:2181");
config.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress());
config.setHighestPriority(null);
config.refresh();

Assertions.assertEquals("zookeeper://127.0.0.1:2181/ConfigCenterConfig?check=true&" +
Assertions.assertEquals(ZookeeperRegistryCenterConfig.getConnectionAddress()+"/ConfigCenterConfig?check=true&" +
"config-file=dubbo.properties&group=group&" +
"namespace=namespace&timeout=3000",
config.toUrl().toFullString()
Expand All @@ -74,7 +75,7 @@ public void testToUrl() {
@Test
public void testOverrideConfig() {

String zkAddr = "zookeeper://127.0.0.1:2181";
String zkAddr = ZookeeperRegistryCenterConfig.getConnectionAddress();
// sysprops has no id
SysProps.setProperty("dubbo.config-center.check", "false");
SysProps.setProperty("dubbo.config-center.address", zkAddr);
Expand Down Expand Up @@ -116,7 +117,7 @@ public void testOverrideConfig2() {
try {
//No id but has address
ConfigCenterConfig configCenter = new ConfigCenterConfig();
configCenter.setAddress("zookeeper://127.0.0.1:2181");
configCenter.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress());

DubboBootstrap.getInstance()
.application("demo-app")
Expand Down Expand Up @@ -277,7 +278,7 @@ public void testParameters() {
@Test
public void testAttributes() {
ConfigCenterConfig cc = new ConfigCenterConfig();
cc.setAddress("zookeeper://127.0.0.1:2181");
cc.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress());
Map<String, String> attributes = new LinkedHashMap<>();
ConfigCenterConfig.appendAttributes(attributes, cc);

Expand All @@ -291,7 +292,7 @@ public void testAttributes() {

@Test
public void testSetAddress() {
String address = "zookeeper://127.0.0.1:2181";
String address = ZookeeperRegistryCenterConfig.getConnectionAddress();
ConfigCenterConfig cc = new ConfigCenterConfig();
cc.setUsername("user123"); // set username first
cc.setPassword("pass123");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.apache.dubbo.config.context.ModuleConfigManager;
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.Invoker;
import org.apache.dubbo.rpc.ProxyFactory;
Expand All @@ -54,7 +52,6 @@
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.NotFoundException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -127,17 +124,11 @@ public class ReferenceConfigTest {
private static String zkUrl1;
private static String zkUrl2;
private static String registryUrl1;
private static RegistryCenter registryCenter1;
private static RegistryCenter registryCenter2;

@BeforeAll
public static void beforeAll() {
int zkServerPort1 = NetUtils.getAvailablePort(NetUtils.getRandomPort());
registryCenter1 = new ZookeeperSingleRegistryCenter(zkServerPort1);
registryCenter1.startup();
int zkServerPort2 = NetUtils.getAvailablePort(NetUtils.getRandomPort());
registryCenter2 = new ZookeeperSingleRegistryCenter(zkServerPort2);
registryCenter2.startup();
int zkServerPort1 = 2181;
int zkServerPort2 = 2182;
zkUrl1 = "zookeeper://localhost:" + zkServerPort1;
zkUrl2 = "zookeeper://localhost:" + zkServerPort2;
registryUrl1 = "registry://localhost:" + zkServerPort1 + "?registry=zookeeper";
Expand All @@ -156,12 +147,6 @@ public void tearDown() throws IOException {
Mockito.framework().clearInlineMocks();
}

@AfterAll
public static void afterAll() {
registryCenter1.shutdown();
registryCenter2.shutdown();
}

/**
* Test whether the configuration required for the aggregation service reference meets expectations
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.rpc.model.ApplicationModel;

import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void testDefault() throws Exception {
public void testEquals() throws Exception {
RegistryConfig registry1 = new RegistryConfig();
RegistryConfig registry2 = new RegistryConfig();
registry1.setAddress("zookeeper://127.0.0.1:2182");
registry1.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress2());
registry2.setAddress("zookeeper://127.0.0.1:2183");
Assertions.assertNotEquals(registry1, registry2);
}
Expand Down Expand Up @@ -241,7 +242,7 @@ public void testPreferredWithTrueValue() {
// process Parameter annotation
AbstractConfig.appendParameters(map, registry);
// Simulate the check that ZoneAwareClusterInvoker#doInvoke do
URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map);
URL url = UrlUtils.parseURL(ZookeeperRegistryCenterConfig.getConnectionAddress1(), map);
Assertions.assertTrue(url.getParameter(PREFERRED_KEY, false));
}

Expand All @@ -253,7 +254,7 @@ public void testPreferredWithFalseValue() {
// Process Parameter annotation
AbstractConfig.appendParameters(map, registry);
// Simulate the check that ZoneAwareClusterInvoker#doInvoke do
URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map);
URL url = UrlUtils.parseURL(ZookeeperRegistryCenterConfig.getConnectionAddress1(), map);
Assertions.assertFalse(url.getParameter(PREFERRED_KEY, false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;

import org.apache.curator.test.TestingServer;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -192,7 +193,7 @@ public void testLoadUserMonitor_registry() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("registry");

URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf("zookeeper://127.0.0.1:2181"));
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf(ZookeeperRegistryCenterConfig.getConnectionAddress()));
Assertions.assertEquals("dubbo", url.getProtocol());
Assertions.assertEquals("registry", url.getParameter("protocol"));
}
Expand All @@ -203,14 +204,14 @@ public void testLoadUserMonitor_service_discovery() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("service-discovery-registry");

URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf("zookeeper://127.0.0.1:2181"));
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf(ZookeeperRegistryCenterConfig.getConnectionAddress()));
Assertions.assertEquals("dubbo", url.getProtocol());
Assertions.assertEquals("service-discovery-registry", url.getParameter("protocol"));
}

@Test
public void testLoadUserMonitor_no_monitor() {
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(null), URL.valueOf("zookeeper://127.0.0.1:2181"));
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(null), URL.valueOf(ZookeeperRegistryCenterConfig.getConnectionAddress()));
Assertions.assertNull(url);
}

Expand All @@ -220,7 +221,7 @@ public void testLoadUserMonitor_user() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("user");

URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf("zookeeper://127.0.0.1:2181"));
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf(ZookeeperRegistryCenterConfig.getConnectionAddress()));
Assertions.assertEquals("user", url.getProtocol());
}

Expand All @@ -229,7 +230,7 @@ public void testLoadUserMonitor_user_address() {
// dubbo.monitor.address=user://1.2.3.4:5678?k=v
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setAddress("user://1.2.3.4:5678?param1=value1");
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf("zookeeper://127.0.0.1:2181"));
URL url = ConfigValidationUtils.loadMonitor(getTestInterfaceConfig(monitorConfig), URL.valueOf(ZookeeperRegistryCenterConfig.getConnectionAddress()));
Assertions.assertEquals("user", url.getProtocol());
Assertions.assertEquals("1.2.3.4:5678", url.getAddress());
Assertions.assertEquals("value1", url.getParameter("param1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;

/**
* Dubbo Provider Bootstrap
Expand All @@ -35,8 +36,8 @@ public static void main(String[] args) throws Exception {
// .registry(builder -> builder.address("eureka://127.0.0.1:8761?registry-type=service&subscribed-services=dubbo-provider-demo"))

// Zookeeper
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=dubbo-provider-demo"))
.metadataReport(new MetadataReportConfig("zookeeper://127.0.0.1:2181"))
.registry("zookeeper", builder -> builder.address(ZookeeperRegistryCenterConfig.getConnectionAddress()+"?registry-type=service&subscribed-services=dubbo-provider-demo"))
.metadataReport(new MetadataReportConfig(ZookeeperRegistryCenterConfig.getConnectionAddress()))

// Nacos
// .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service&subscribed.services=dubbo-provider-demo"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.config.bootstrap.rest.UserServiceImpl;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;

import java.util.Arrays;

Expand All @@ -45,11 +46,11 @@ private static void multipleRegistries() {

RegistryConfig interfaceRegistry = new RegistryConfig();
interfaceRegistry.setId("interfaceRegistry");
interfaceRegistry.setAddress("zookeeper://127.0.0.1:2181");
interfaceRegistry.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress());

RegistryConfig serviceRegistry = new RegistryConfig();
serviceRegistry.setId("serviceRegistry");
serviceRegistry.setAddress("zookeeper://127.0.0.1:2181?registry-type=service");
serviceRegistry.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress()+"?registry-type=service");

ServiceConfig<EchoService> echoService = new ServiceConfig<>();
echoService.setInterface(EchoService.class.getName());
Expand All @@ -73,7 +74,7 @@ private static void multipleRegistries() {
.registries(Arrays.asList(interfaceRegistry, serviceRegistry))
// .registry(RegistryBuilder.newBuilder().address("consul://127.0.0.1:8500?registry.type=service").build())
.protocol(builder -> builder.port(-1).name("dubbo"))
.metadataReport(new MetadataReportConfig("zookeeper://127.0.0.1:2181"))
.metadataReport(new MetadataReportConfig(ZookeeperRegistryCenterConfig.getConnectionAddress()))
.service(echoService)
.service(userService)
.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.config.bootstrap.rest.UserServiceImpl;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;

/**
* TODO
Expand All @@ -27,7 +28,7 @@ public class DubboServiceProviderMinimumBootstrap {
public static void main(String[] args) {
DubboBootstrap.getInstance()
.application("dubbo-provider-demo")
.registry(builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
.registry(builder -> builder.address(ZookeeperRegistryCenterConfig.getConnectionAddress()+"?registry-type=service"))
// .registry(builder -> builder.address("eureka://127.0.0.1:8761?registry-type=service"))
.protocol(builder -> builder.port(-1).name("dubbo"))
.service("echo", builder -> builder.interfaceClass(EchoService.class).ref(new EchoServiceImpl()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@
import org.apache.dubbo.config.api.Greeting;
import org.apache.dubbo.config.mock.GreetingLocal2;
import org.apache.dubbo.config.provider.impl.DemoServiceImpl;
import org.apache.dubbo.registrycenter.RegistryCenter;
import org.apache.dubbo.registrycenter.ZookeeperSingleRegistryCenter;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.FrameworkServiceRepository;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.test.check.DubboTestChecker;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -63,8 +62,6 @@ public class MultiInstanceTest {

private static final Logger logger = LoggerFactory.getLogger(MultiInstanceTest.class);

private static ZookeeperSingleRegistryCenter registryCenter;

private static RegistryConfig registryConfig;

private static DubboTestChecker testChecker;
Expand All @@ -73,21 +70,14 @@ public class MultiInstanceTest {
@BeforeAll
public static void beforeAll() {
FrameworkModel.destroyAll();
registryCenter = new ZookeeperSingleRegistryCenter(NetUtils.getAvailablePort());
registryCenter.startup();
RegistryCenter.Instance instance = registryCenter.getRegistryCenterInstance().get(0);
registryConfig = new RegistryConfig(String.format("%s://%s:%s",
instance.getType(),
instance.getHostname(),
instance.getPort()));
registryConfig = new RegistryConfig(ZookeeperRegistryCenterConfig.getConnectionAddress1());

// pre-check threads
//precheckUnclosedThreads();
}

@AfterAll
public static void afterAll() throws Exception {
registryCenter.shutdown();
FrameworkModel.destroyAll();

// check threads
Expand Down Expand Up @@ -321,7 +311,6 @@ public void testMultiProviderApplicationsStopOneByOne() {

DubboBootstrap providerBootstrap1 = null;
DubboBootstrap providerBootstrap2 = null;
ZookeeperSingleRegistryCenter registryCenter2 = null;

try {

Expand Down Expand Up @@ -349,13 +338,7 @@ public void testMultiProviderApplicationsStopOneByOne() {
Assertions.assertTrue(stackTraces1.size() > 0, "Get threads of provider app 1 failed");

// start zk server 2
registryCenter2 = new ZookeeperSingleRegistryCenter(NetUtils.getAvailablePort());
registryCenter2.startup();
RegistryCenter.Instance instance = registryCenter2.getRegistryCenterInstance().get(0);
RegistryConfig registryConfig2 = new RegistryConfig(String.format("%s://%s:%s",
instance.getType(),
instance.getHostname(),
instance.getPort()));
RegistryConfig registryConfig2 = new RegistryConfig(ZookeeperRegistryCenterConfig.getConnectionAddress2());

// start provider app 2 use a difference zk server 2
ServiceConfig serviceConfig2 = new ServiceConfig();
Expand Down Expand Up @@ -388,7 +371,6 @@ public void testMultiProviderApplicationsStopOneByOne() {
// stop provider app 2 and check threads
providerBootstrap2.stop();
// shutdown register center after dubbo application to avoid unregister services blocking
registryCenter2.shutdown();
checkUnclosedThreadsOfApp(stackTraces2, "Found unclosed threads of app 2: ", null);

} finally {
Expand All @@ -398,9 +380,6 @@ public void testMultiProviderApplicationsStopOneByOne() {
if (providerBootstrap2 != null) {
providerBootstrap2.stop();
}
if (registryCenter2 != null) {
registryCenter2.shutdown();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config.bootstrap;

import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;

import static org.apache.dubbo.common.constants.CommonConstants.COMPOSITE_METADATA_STORAGE_TYPE;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY;
Expand All @@ -33,7 +34,7 @@ public static void main(String[] args) throws Exception {

DubboBootstrap bootstrap = DubboBootstrap.getInstance()
.application("zookeeper-dubbo-consumer", app -> app.metadata(COMPOSITE_METADATA_STORAGE_TYPE))
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181")
.registry("zookeeper", builder -> builder.address(ZookeeperRegistryCenterConfig.getConnectionAddress())
.parameter(REGISTRY_TYPE_KEY, SERVICE_REGISTRY_TYPE)
.useAsConfigCenter(true)
.useAsMetadataCenter(true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.bootstrap.EchoService;
import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.test.check.registrycenter.config.ZookeeperRegistryCenterConfig;

/**
* Dubbo Provider Bootstrap
Expand All @@ -31,7 +32,7 @@ public class DubboInterfaceConsumerBootstrap {
public static void main(String[] args) throws Exception {
RegistryConfig interfaceRegistry = new RegistryConfig();
interfaceRegistry.setId("interfaceRegistry");
interfaceRegistry.setAddress("zookeeper://127.0.0.1:2181");
interfaceRegistry.setAddress(ZookeeperRegistryCenterConfig.getConnectionAddress());

DubboBootstrap bootstrap = DubboBootstrap.getInstance()
.application("dubbo-consumer-demo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.integration;
package org.apache.dubbo.config.integration;

import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.ExporterListener;
Expand Down
Loading

0 comments on commit b06e5c2

Please sign in to comment.