Skip to content

Commit

Permalink
Polish apache#3946 : Dubbo Cloud Native : To Add Dubbo metadata service
Browse files Browse the repository at this point in the history
Test case
  • Loading branch information
mercyblitz committed May 14, 2019
1 parent e853ef3 commit e2faa73
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
/**
* A framework interface of Dubbo Metadata Service defines the contract of Dubbo Services registartion and subscription
* between Dubbo service providers and its consumers. The implementationwill be exported as a normal Dubbo service that
* the clients would subscribe, whose version comes from the {@link #getVersion()} method and group gets from
* {@link #currentServiceName()}, that means, The different Dubbo service(application) will export the different
* the clients would subscribe, whose version comes from the {@link #version()} method and group gets from
* {@link #serviceName()}, that means, The different Dubbo service(application) will export the different
* {@link DubboMetadataService} that persists all the exported and subscribed metadata, they are present by
* {@link #getExportedURLs()} and {@link #getSubscribedURLs()} respectively. What's more, {@link DubboMetadataService}
* also providers the fine-grain methods for the precise queries.
Expand All @@ -42,7 +42,7 @@ public interface DubboMetadataService {
/**
* The value of All service instances
*/
String ALL_SERVICE_INTERFACE = "*";
String ALL_SERVICE_INTERFACES = "*";

/**
* The contract version of {@link DubboMetadataService}, the future update must make sure compatible.
Expand All @@ -54,15 +54,15 @@ public interface DubboMetadataService {
*
* @return non-null
*/
String currentServiceName();
String serviceName();

/**
* The version of {@link DubboMetadataService} that always equals {@link #VERSION}
* Gets the version of {@link DubboMetadataService} that always equals {@link #VERSION}
*
* @return non-null
* @see #VERSION
*/
default String getVersion() {
default String version() {
return VERSION;
}

Expand All @@ -79,7 +79,7 @@ default String getVersion() {
* @return non-null read-only {@link List}
*/
default List<String> getExportedURLs() {
return getExportedURLs(ALL_SERVICE_INTERFACE);
return getExportedURLs(ALL_SERVICE_INTERFACES);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public InMemoryMetadataService(String serviceName) {
}

@Override
public String currentServiceName() {
public String serviceName() {
return serviceName;
}

Expand All @@ -85,10 +85,9 @@ public List<String> getSubscribedURLs() {

@Override
public List<String> getExportedURLs(String serviceInterface, String group, String version) {
if (ALL_SERVICE_NAMES.equals(serviceInterface)) {
if (ALL_SERVICE_INTERFACES.equals(serviceInterface)) {
return getAllServiceURLs(exportedServiceURLs);
}

String serviceKey = buildKey(serviceInterface, group, version);
return unmodifiableList(getServiceURLs(exportedServiceURLs, serviceKey));
}
Expand All @@ -112,7 +111,11 @@ public boolean unsubscribeURL(URL url) {
protected boolean addURL(Map<String, List<String>> serviceURLs, URL url) {
String serviceKey = url.getServiceKey();
List<String> urls = getServiceURLs(serviceURLs, serviceKey);
return urls.add(url.toFullString());
String urlString = url.toFullString();
if (!urls.contains(urlString)) {
return urls.add(url.toFullString());
}
return false;
}

protected boolean removeURL(Map<String, List<String>> serviceURLs, URL url) {
Expand All @@ -132,5 +135,4 @@ protected List<String> getAllServiceURLs(Map<String, List<String>> serviceURLs)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import java.util.List;

import static java.util.Arrays.asList;
import static org.apache.dubbo.common.Constants.GROUP_KEY;
import static org.apache.dubbo.common.Constants.VERSION_KEY;
import static org.apache.dubbo.common.URL.valueOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* {@link InMemoryMetadataService} Test
Expand All @@ -36,20 +37,87 @@ public class InMemoryMetadataServiceTest {

private InMemoryMetadataService inMemoryMetadataService = new InMemoryMetadataService("test");

private final URL BASE_URL = valueOf("dubbo://127.0.0.1:20880/org.apache.dubbo.test.TestService?interface=org.apache.dubbo.test.TestService");
private static final String TEST_SERVICE = "org.apache.dubbo.test.TestService";

private static final URL BASE_URL = valueOf("dubbo://127.0.0.1:20880/" + TEST_SERVICE);
private static final URL REST_BASE_URL = valueOf("rest://127.0.0.1:20880/" + TEST_SERVICE);
private static final URL BASE_URL_GROUP = BASE_URL.addParameter(GROUP_KEY, "test");
private static final URL BASE_URL_GROUP_AND_VERSION = BASE_URL_GROUP.addParameter(VERSION_KEY, "1.0.0");

@Test
public void testCurrentServiceName() {
assertEquals("test", inMemoryMetadataService.currentServiceName());
public void testServiceName() {
assertEquals("test", inMemoryMetadataService.serviceName());
}

@Test
public void testVersion() {
assertEquals("1.0.0", DubboMetadataService.VERSION);
assertEquals("1.0.0", inMemoryMetadataService.version());
}

@Test
public void testGetExportedURLs() {

assertTrue(inMemoryMetadataService.exportURL(BASE_URL));
List<String> exportedURLs = inMemoryMetadataService.getExportedURLs("org.apache.dubbo.test.TestService");
List<String> exportedURLs = inMemoryMetadataService.getExportedURLs(TEST_SERVICE);
assertEquals(1, exportedURLs.size());
assertEquals(asList(BASE_URL.toFullString()), exportedURLs);
assertTrue(inMemoryMetadataService.unexportURL(BASE_URL));

assertTrue(inMemoryMetadataService.exportURL(BASE_URL));
assertFalse(inMemoryMetadataService.exportURL(BASE_URL));

assertTrue(inMemoryMetadataService.exportURL(BASE_URL_GROUP));
assertTrue(inMemoryMetadataService.exportURL(BASE_URL_GROUP_AND_VERSION));

exportedURLs = inMemoryMetadataService.getExportedURLs(TEST_SERVICE);
assertEquals(asList(BASE_URL.toFullString()), exportedURLs);
assertEquals(asList(
BASE_URL.toFullString(),
BASE_URL_GROUP.toFullString(),
BASE_URL_GROUP_AND_VERSION.toFullString()), inMemoryMetadataService.getExportedURLs());

assertTrue(inMemoryMetadataService.exportURL(REST_BASE_URL));
exportedURLs = inMemoryMetadataService.getExportedURLs(TEST_SERVICE);
assertEquals(asList(BASE_URL.toFullString(), REST_BASE_URL.toFullString()), exportedURLs);
}

@Test
public void testGetSubscribedURLs() {
assertTrue(inMemoryMetadataService.subscribeServiceURL(BASE_URL));
assertFalse(inMemoryMetadataService.subscribeServiceURL(BASE_URL));

assertTrue(inMemoryMetadataService.subscribeServiceURL(BASE_URL_GROUP));
assertTrue(inMemoryMetadataService.subscribeServiceURL(BASE_URL_GROUP_AND_VERSION));
assertTrue(inMemoryMetadataService.subscribeServiceURL(REST_BASE_URL));

List<String> subscribedURLs = inMemoryMetadataService.getSubscribedURLs();
assertEquals(4, subscribedURLs.size());
assertEquals(asList(
BASE_URL.toFullString(),
REST_BASE_URL.toFullString(),
BASE_URL_GROUP.toFullString(),
BASE_URL_GROUP_AND_VERSION.toFullString()), subscribedURLs);

assertTrue(inMemoryMetadataService.unsubscribeURL(REST_BASE_URL));
subscribedURLs = inMemoryMetadataService.getSubscribedURLs();
assertEquals(3, subscribedURLs.size());
assertEquals(asList(
BASE_URL.toFullString(),
BASE_URL_GROUP.toFullString(),
BASE_URL_GROUP_AND_VERSION.toFullString()), subscribedURLs);

assertTrue(inMemoryMetadataService.unsubscribeURL(BASE_URL_GROUP));
subscribedURLs = inMemoryMetadataService.getSubscribedURLs();
assertEquals(2, subscribedURLs.size());
assertEquals(asList(
BASE_URL.toFullString(),
BASE_URL_GROUP_AND_VERSION.toFullString()), subscribedURLs);

assertTrue(inMemoryMetadataService.unsubscribeURL(BASE_URL_GROUP_AND_VERSION));
subscribedURLs = inMemoryMetadataService.getSubscribedURLs();
assertEquals(1, subscribedURLs.size());
assertEquals(asList(
BASE_URL.toFullString()), subscribedURLs);
}
}
}

0 comments on commit e2faa73

Please sign in to comment.