Skip to content

Commit

Permalink
Add integration testcase for testing RegistryProtocol and ServiceDisc…
Browse files Browse the repository at this point in the history
…overyRegistry (apache#8278)

Checks if ServiceDiscoveryRegistry works well using reflection
  • Loading branch information
pinxiong authored Jul 13, 2021
1 parent 89aac3c commit d19eaa1
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.integration.servicediscoveryregistry;

import org.apache.dubbo.ZooKeeperServerTesting;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.integration.AbstractIntegrationTest;
import org.apache.dubbo.integration.IntegrationService;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.client.ServiceDiscoveryRegistry;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscovery;
import org.junit.jupiter.api.Assertions;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

/**
* The purpose of is to check if there exists problem
* between {@link org.apache.dubbo.registry.integration.RegistryProtocol} and
* {@link org.apache.dubbo.registry.client.ServiceDiscoveryRegistry}
* using zookeeper as registry center.
*/
public abstract class AbstractServiceDiscoveryRegistryTest extends AbstractIntegrationTest<ServiceDiscoveryRegistryIntegrationService> {

private ServiceConfig<ServiceDiscoveryRegistryIntegrationService> serviceConfig;

@Override
public void initialize() {
// initialize ServiceConfig
this.serviceConfig = new ServiceConfig<>();
this.serviceConfig.setInterface(IntegrationService.class);
this.serviceConfig.setRef(new ServiceDiscoveryRegistryIntegrationService());
this.serviceConfig.setAsync(false);

DubboBootstrap.getInstance()
.application(new ApplicationConfig(this.getApplicationName()))
.registry(new RegistryConfig("zookeeper://127.0.0.1:" + ZooKeeperServerTesting.getPort()))
.protocol(new ProtocolConfig(this.getProtocolName(), this.getProtocolPort()))
.service(this.serviceConfig);
}

@Override
public void process() {
this.beforeExport();
// export provider
DubboBootstrap.getInstance().start();
this.afterExport();
}

/**
* before export
*/
private void beforeExport() {
// assert the service hasn't been exported
Assertions.assertFalse(this.serviceConfig.isExported());
// assert there is no exported url
Assertions.assertEquals(this.serviceConfig.getExportedUrls().size(), 0);
}

/**
* after export
*/
private void afterExport() {
// obtain ServiceDiscoveryRegistry instance
ServiceDiscoveryRegistry serviceDiscoveryRegistry = this.getServiceDiscoveryRegistry();
serviceDiscoveryRegistry.getServiceDiscovery();
// check service discovery protocol
Assertions.assertTrue(serviceDiscoveryRegistry.getServiceDiscovery() instanceof ZookeeperServiceDiscovery);
// convert to ZookeeperServiceDiscovery instance
ZookeeperServiceDiscovery zookeeperServiceDiscovery =(ZookeeperServiceDiscovery) serviceDiscoveryRegistry.getServiceDiscovery();
// zookeeperServiceDiscovery can't destroy
Assertions.assertFalse(zookeeperServiceDiscovery.isDestroy());
// Gets registered service by ZookeeperServiceDiscovery
Set<String> services = zookeeperServiceDiscovery.getServices();
// check service exists
Assertions.assertTrue(!services.isEmpty());
Assertions.assertTrue(services.contains(this.getApplicationName()));
}

/**
* Returns {@link ServiceDiscoveryRegistry} instance.
* <p>
* FIXME It's not a good way to obtain {@link ServiceDiscoveryRegistry} using Reflection.
*/
private ServiceDiscoveryRegistry getServiceDiscoveryRegistry(){
ServiceDiscoveryRegistry serviceDiscoveryRegistry = null;
try {
// get AbstractRegistryFactory.REGISTRIES
Field field = AbstractRegistryFactory.class.getDeclaredField("REGISTRIES");
field.setAccessible(true);
Map<String, Registry> REGISTRIES = (Map<String, Registry>)field.get(AbstractRegistryFactory.class);
for(Registry registry:REGISTRIES.values()){
if(registry instanceof ServiceDiscoveryRegistry){
serviceDiscoveryRegistry = (ServiceDiscoveryRegistry)registry;
break;
}
}
} catch (IllegalAccessException | NoSuchFieldException e) {
// ignore
}
return serviceDiscoveryRegistry;
}

@Override
public void destroy() {
this.serviceConfig = null;
}

/**
* Returns the protocol's name.
*/
protected abstract String getProtocolName();

/**
* Returns the application name.
*/
protected abstract String getApplicationName();

/**
* Returns the protocol's port.
*/
protected abstract int getProtocolPort();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.integration.servicediscoveryregistry;

/**
* Checks if there exists problems between
* {@link org.apache.dubbo.registry.integration.RegistryProtocol} and
* {@link org.apache.dubbo.registry.client.ServiceDiscoveryRegistry}
* using dubbo protocol.
*/
public class ServiceDiscoveryRegistry4DubboTest extends AbstractServiceDiscoveryRegistryTest{
/**
* Returns the protocol's name.
*/
@Override
protected String getProtocolName() {
return "dubbo";
}

/**
* Returns the application name.
*/
@Override
protected String getApplicationName() {
return "integration-service-discovery-registry-dubbo";
}

/**
* Returns the protocol's port.
*/
@Override
protected int getProtocolPort() {
return 20880;
}
}
Original file line number Diff line number Diff line change
@@ -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.integration.servicediscoveryregistry;

import org.apache.dubbo.integration.IntegrationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The implementation for {@link IntegrationService}
*/
public class ServiceDiscoveryRegistryIntegrationService implements IntegrationService {

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

@Override
public String hello(String name) {
String value = "Hello " + name;
logger.info(value);
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/
/**
* The purpose of this package is to define integration testcases for checking
* <p>if there exists problems between {@link org.apache.dubbo.registry.integration.RegistryProtocol}
* and {@link org.apache.dubbo.registry.client.ServiceDiscoveryRegistry}.</p>
*/
package org.apache.dubbo.integration.servicediscoveryregistry;

0 comments on commit d19eaa1

Please sign in to comment.