Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for ZookeeperMetadataReportTest #8840

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private Annotation getServiceAnnotation(Class<?> serviceType) {
*/
public String resolveInterfaceClassName() {

Class interfaceClass = null;
Class interfaceClass;
// first, try to get the value from "interfaceName" attribute
String interfaceName = resolveAttribute("interfaceName");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
*/
public class ClassPathServiceRestMetadataReader implements ServiceRestMetadataReader {

private final String serviceRestMetadataJsonResoucePath;
private final String serviceRestMetadataJsonResourcePath;

public ClassPathServiceRestMetadataReader() {
this(SERVICE_REST_METADATA_RESOURCE_PATH);
}

public ClassPathServiceRestMetadataReader(String serviceRestMetadataJsonResoucePath) {
this.serviceRestMetadataJsonResoucePath = serviceRestMetadataJsonResoucePath;
public ClassPathServiceRestMetadataReader(String serviceRestMetadataJsonResourcePath) {
this.serviceRestMetadataJsonResourcePath = serviceRestMetadataJsonResourcePath;
}

@Override
Expand All @@ -59,7 +59,7 @@ public List<ServiceRestMetadata> read() {
ClassLoader classLoader = getClass().getClassLoader();

execute(() -> {
Enumeration<URL> resources = classLoader.getResources(serviceRestMetadataJsonResoucePath);
Enumeration<URL> resources = classLoader.getResources(serviceRestMetadataJsonResourcePath);
Gson gson = new Gson();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testSupports() {
@Test
public void testResolve() {
// Generated by "dubbo-metadata-processor"
ClassPathServiceRestMetadataReader reader = new ClassPathServiceRestMetadataReader("META-INF/dubbo/spring-mvc-servoce-rest-metadata.json");
ClassPathServiceRestMetadataReader reader = new ClassPathServiceRestMetadataReader("META-INF/dubbo/spring-mvc-service-rest-metadata.json");
List<ServiceRestMetadata> serviceRestMetadataList = reader.read();

ServiceRestMetadata expectedServiceRestMetadata = serviceRestMetadataList.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private final static String REDIS_DATABASE_KEY = "database";
private final static Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class);

JedisPool pool;
Set<HostAndPort> jedisClusterNodes;
// protected , for test
protected JedisPool pool;
private Set<HostAndPort> jedisClusterNodes;
private int timeout;
private String password;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.apache.dubbo.metadata.store.zookeeper;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigItem;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.report.MetadataReport;
Expand All @@ -33,14 +35,13 @@
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.*;
import java.util.concurrent.CountDownLatch;
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;
import static org.apache.dubbo.metadata.ServiceNameMapping.DEFAULT_MAPPING_GROUP;

/**
* 2018/10/9
Expand Down Expand Up @@ -273,4 +274,45 @@ private URL generateURL(String interfaceName, String version, String group, Stri
+ application + (group == null ? "" : "&group=" + group));
return url;
}


@Test
public void testMapping() throws InterruptedException {
String serviceKey = ZookeeperMetadataReportTest.class.getName();
URL url = URL.valueOf("test://127.0.0.1:8888/" + serviceKey);
String appNames = "demo1,demo2";

CountDownLatch latch = new CountDownLatch(1);
Set<String> serviceAppMapping = zookeeperMetadataReport.getServiceAppMapping(serviceKey, event -> {
Set<String> apps = event.getApps();
Assertions.assertEquals(apps.size(), 2);
Assertions.assertTrue(apps.contains("demo1"));
Assertions.assertTrue(apps.contains("demo2"));
latch.countDown();
}, url);
Assertions.assertTrue(serviceAppMapping.isEmpty());

ConfigItem configItem = zookeeperMetadataReport.getConfigItem(serviceKey, DEFAULT_MAPPING_GROUP);
zookeeperMetadataReport.registerServiceAppMapping(serviceKey, DEFAULT_MAPPING_GROUP, appNames, configItem.getTicket());
latch.await();
}

@Test
public void testAppMetadata() {
String serviceKey = ZookeeperMetadataReportTest.class.getName();
String appName = "demo";
URL url = URL.valueOf("test://127.0.0.1:8888/" + serviceKey);
MetadataInfo metadataInfo = new MetadataInfo(appName);
metadataInfo.addService(new MetadataInfo.ServiceInfo(url));

SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(appName, metadataInfo.calAndGetRevision());
MetadataInfo appMetadata = zookeeperMetadataReport.getAppMetadata(identifier, Collections.emptyMap());
Assertions.assertNull(appMetadata);

zookeeperMetadataReport.publishAppMetadata(identifier, metadataInfo);
appMetadata = zookeeperMetadataReport.getAppMetadata(identifier, Collections.emptyMap());
Assertions.assertNotNull(appMetadata);
Assertions.assertEquals(appMetadata.calAndGetRevision(), metadataInfo.calAndGetRevision());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ServiceDiscoveryRegistry(URL registryURL) {
this.writableMetadataService = WritableMetadataService.getDefaultExtension(registryURL.getScopeModel());
}

// Currently for test purpose
// Currently, for test purpose
protected ServiceDiscoveryRegistry(URL registryURL, ServiceDiscovery serviceDiscovery, WritableMetadataService writableMetadataService) {
this.registryURL = registryURL;
this.serviceDiscovery = serviceDiscovery;
Expand Down Expand Up @@ -139,7 +139,7 @@ protected boolean shouldRegister(URL providerURL) {

if (!should) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("The URL[%s] should not be registered.", providerURL.toString()));
logger.debug(String.format("The URL[%s] should not be registered.", providerURL));
}
}

Expand Down