Add test cases for NacosMetadataReport (#5776)
* Add more test case for NacosMetadataReport * Modify codes * Modify comments * Add more test case and modify codes * Modify codes
This commit is contained in:
parent
a972650208
commit
a5738065a9
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.apache.dubbo.metadata.store.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.common.utils.NetUtils;
|
||||
import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
|
||||
|
|
@ -25,13 +26,17 @@ import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
|
|||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
|
||||
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
|
||||
|
|
@ -40,19 +45,63 @@ import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
|
|||
//FIXME: waiting for embedded Nacos suport, then we can open the switch.
|
||||
@Disabled("https://github.com/alibaba/nacos/issues/1188")
|
||||
public class NacosMetadataReportTest {
|
||||
|
||||
private static final String SESSION_TIMEOUT_KEY = "session";
|
||||
|
||||
private static final String TEST_SERVICE = "org.apache.dubbo.metadata.store.nacos.NacosMetadata4TstService";
|
||||
|
||||
private NacosMetadataReport nacosMetadataReport;
|
||||
|
||||
private NacosMetadataReportFactory nacosMetadataReportFactory;
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
private static final String NACOS_GROUP = "zzz";
|
||||
private static final String NACOS_GROUP = "metadata_test";
|
||||
|
||||
/**
|
||||
* timeout(ms) for nacos session
|
||||
*/
|
||||
private static final int SESSION_TIMEOUT = 15 * 1000;
|
||||
|
||||
/**
|
||||
* timeout(ms) for query operation on nacos
|
||||
*/
|
||||
private static final int NACOS_READ_TIMEOUT = 5 * 1000;
|
||||
|
||||
/**
|
||||
* interval(ms) to make nacos cache refresh
|
||||
*/
|
||||
private static final int INTERVAL_TO_MAKE_NACOS_REFRESH = 1000;
|
||||
|
||||
/**
|
||||
* version for test
|
||||
*/
|
||||
private static final String VERSION = "1.0.0";
|
||||
|
||||
/**
|
||||
* group for test
|
||||
*/
|
||||
private static final String METADATA_GROUP = null;
|
||||
|
||||
/**
|
||||
* application name for test
|
||||
*/
|
||||
private static final String APPLICATION_NAME = "nacos-metdata-report-test";
|
||||
|
||||
/**
|
||||
* revision for test
|
||||
*/
|
||||
private static final String REVISION = "90980";
|
||||
|
||||
/**
|
||||
* protocol for test
|
||||
*/
|
||||
private static final String PROTOCOL = "xxx";
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
// timeout in 15 seconds.
|
||||
URL url = URL.valueOf("nacos://127.0.0.1:8848?group=" + NACOS_GROUP)
|
||||
.addParameter(SESSION_TIMEOUT_KEY, 15000);
|
||||
.addParameter(SESSION_TIMEOUT_KEY, SESSION_TIMEOUT);
|
||||
nacosMetadataReportFactory = new NacosMetadataReportFactory();
|
||||
this.nacosMetadataReport = (NacosMetadataReport) nacosMetadataReportFactory.createMetadataReport(url);
|
||||
this.configService = nacosMetadataReport.buildConfigService(url);
|
||||
|
|
@ -62,31 +111,109 @@ public class NacosMetadataReportTest {
|
|||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStoreProvider() throws Exception {
|
||||
String version = "1.0.0";
|
||||
String group = null;
|
||||
String application = "nacos-metdata-report-test";
|
||||
MetadataIdentifier providerIdentifier =
|
||||
storeProvider(nacosMetadataReport, TEST_SERVICE, version, group, application);
|
||||
String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L);
|
||||
storeProvider(nacosMetadataReport, TEST_SERVICE, VERSION, METADATA_GROUP, APPLICATION_NAME);
|
||||
String serverContent = configService.getConfig(providerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNotNull(serverContent);
|
||||
|
||||
Gson gson = new Gson();
|
||||
FullServiceDefinition fullServiceDefinition = gson.fromJson(serverContent, FullServiceDefinition.class);
|
||||
Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "nacosTest");
|
||||
|
||||
//Clear test data
|
||||
configService.removeConfig(providerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreConsumer() throws Exception {
|
||||
String version = "1.0.0";
|
||||
String group = null;
|
||||
String application = "nacos-metadata-report-consumer-test";
|
||||
MetadataIdentifier consumerIdentifier = storeConsumer(nacosMetadataReport, TEST_SERVICE, version, group, application);
|
||||
MetadataIdentifier consumerIdentifier = storeConsumer(nacosMetadataReport, TEST_SERVICE, VERSION, METADATA_GROUP, APPLICATION_NAME);
|
||||
|
||||
String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, 5000L);
|
||||
String serverContent = configService.getConfig(consumerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNotNull(serverContent);
|
||||
Assertions.assertEquals(serverContent, "{\"paramConsumerTest\":\"nacosConsumer\"}");
|
||||
|
||||
//clear test data
|
||||
configService.removeConfig(consumerIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSaveServiceMetadata() throws Exception {
|
||||
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + TEST_SERVICE +
|
||||
"?paramTest=nacosTest&version=" + VERSION + "&application="
|
||||
+ APPLICATION_NAME + (METADATA_GROUP == null ? "" : "&group=" + METADATA_GROUP));
|
||||
ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, VERSION,
|
||||
METADATA_GROUP, "provider", REVISION, PROTOCOL);
|
||||
nacosMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
String serviceMetaData = configService.getConfig(serviceMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNotNull(serviceMetaData);
|
||||
Assertions.assertEquals(serviceMetaData, URL.encode(url.toFullString()));
|
||||
|
||||
//clear test data
|
||||
configService.removeConfig(serviceMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoRemoveServiceMetadata() throws Exception {
|
||||
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + TEST_SERVICE +
|
||||
"?paramTest=nacosTest&version=" + VERSION + "&application="
|
||||
+ APPLICATION_NAME + (METADATA_GROUP == null ? "" : "&group=" + METADATA_GROUP));
|
||||
ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, VERSION,
|
||||
METADATA_GROUP, "provider", REVISION, PROTOCOL);
|
||||
nacosMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
String serviceMetaData = configService.getConfig(serviceMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNotNull(serviceMetaData);
|
||||
|
||||
nacosMetadataReport.doRemoveMetadata(serviceMetadataIdentifier);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
serviceMetaData = configService.getConfig(serviceMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNull(serviceMetaData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoGetExportedURLs() throws InterruptedException, NacosException {
|
||||
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + TEST_SERVICE +
|
||||
"?paramTest=nacosTest&version=" + VERSION + "&application="
|
||||
+ APPLICATION_NAME + (METADATA_GROUP == null ? "" : "&group=" + METADATA_GROUP));
|
||||
ServiceMetadataIdentifier serviceMetadataIdentifier = new ServiceMetadataIdentifier(TEST_SERVICE, VERSION,
|
||||
METADATA_GROUP, "provider", REVISION, PROTOCOL);
|
||||
|
||||
nacosMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
|
||||
List<String> exportedURLs = nacosMetadataReport.doGetExportedURLs(serviceMetadataIdentifier);
|
||||
Assertions.assertTrue(exportedURLs.size() == 1);
|
||||
|
||||
String exportedUrl = exportedURLs.get(0);
|
||||
Assertions.assertNotNull(exportedUrl);
|
||||
Assertions.assertEquals(exportedUrl, url.toFullString());
|
||||
|
||||
//clear test data
|
||||
configService.removeConfig(serviceMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSaveSubscriberData() throws InterruptedException, NacosException {
|
||||
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + TEST_SERVICE +
|
||||
"?paramTest=nacosTest&version=" + VERSION + "&application="
|
||||
+ APPLICATION_NAME + (METADATA_GROUP == null ? "" : "&group=" + METADATA_GROUP));
|
||||
SubscriberMetadataIdentifier subscriberMetadataIdentifier = new SubscriberMetadataIdentifier(APPLICATION_NAME, REVISION);
|
||||
Gson gson = new Gson();
|
||||
String urlListJsonString = gson.toJson(Arrays.asList(url));
|
||||
nacosMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, urlListJsonString);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
|
||||
String subscriberMetadata = configService.getConfig(subscriberMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP, NACOS_READ_TIMEOUT);
|
||||
Assertions.assertNotNull(subscriberMetadata);
|
||||
Assertions.assertEquals(subscriberMetadata, urlListJsonString);
|
||||
|
||||
//clear test data
|
||||
configService.removeConfig(subscriberMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), NACOS_GROUP);
|
||||
|
||||
}
|
||||
|
||||
private MetadataIdentifier storeProvider(NacosMetadataReport nacosMetadataReport, String interfaceName, String version,
|
||||
|
|
@ -103,7 +230,7 @@ public class NacosMetadataReportTest {
|
|||
ServiceDefinitionBuilder.buildFullDefinition(interfaceClass, url.getParameters());
|
||||
|
||||
nacosMetadataReport.storeProviderMetadata(providerMetadataIdentifier, fullServiceDefinition);
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
return providerMetadataIdentifier;
|
||||
}
|
||||
|
||||
|
|
@ -113,7 +240,8 @@ public class NacosMetadataReportTest {
|
|||
Map<String, String> tmp = new HashMap<>();
|
||||
tmp.put("paramConsumerTest", "nacosConsumer");
|
||||
nacosMetadataReport.storeConsumerMetadata(consumerIdentifier, tmp);
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(INTERVAL_TO_MAKE_NACOS_REFRESH);
|
||||
return consumerIdentifier;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue