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

Refactoring Metadata Unit Tests #15089

Open
wants to merge 1 commit into
base: 3.3
Choose a base branch
from
Open
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
22 changes: 0 additions & 22 deletions dubbo-metadata/dubbo-metadata-report-zookeeper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,6 @@
<artifactId>dubbo-configcenter-zookeeper</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-test-common</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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.JsonUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.metadata.MappingChangedEvent;
Expand All @@ -30,47 +29,79 @@
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.remoting.zookeeper.curator5.ZookeeperClient;
import org.apache.dubbo.remoting.zookeeper.curator5.ZookeeperClientManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ZookeeperMetadataReportTest {

/**
* 2018/10/9
*/
class ZookeeperMetadataReportTest {
private ZookeeperMetadataReport zookeeperMetadataReport;
private URL registryUrl;
private ZookeeperMetadataReportFactory zookeeperMetadataReportFactory;
private static String zookeeperConnectionAddress1;

@BeforeAll
public static void beforeAll() {
zookeeperConnectionAddress1 = System.getProperty("zookeeper.connection.address.1");
}
/**
* Uses Map<String, String> to simulate ZK node storage:
* key corresponds to the ZK node path, and value corresponds to the node content.
*/
private final Map<String, String> zkDataStore = new ConcurrentHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we've mocked ZookeeperClient, I think we can simulate the return result directly without actually storing it and spread the setUp logic across each unit test.


@BeforeEach
public void setUp() throws Exception {
this.registryUrl = URL.valueOf(zookeeperConnectionAddress1);

zookeeperMetadataReportFactory = new ZookeeperMetadataReportFactory(ApplicationModel.defaultModel());
this.zookeeperMetadataReport =
(ZookeeperMetadataReport) zookeeperMetadataReportFactory.getMetadataReport(registryUrl);
void setUp() {
// 1. Create mock objects
ZookeeperClientManager mockClientManager = mock(ZookeeperClientManager.class);
ZookeeperClient mockZkClient = mock(ZookeeperClient.class);

// 2. When connect(...) is called, return mockZkClient
when(mockClientManager.connect(any(URL.class))).thenReturn(mockZkClient);

// 3. Simulate createOrUpdate behavior: store path->data in zkDataStore
doAnswer(invocation -> {
String path = invocation.getArgument(0, String.class);
String data = invocation.getArgument(1, String.class);
zkDataStore.put(path, data);
return null;
})
.when(mockZkClient)
.createOrUpdate(anyString(), anyString(), eq(false));

// 4. Simulate delete behavior: remove key=path from zkDataStore
doAnswer(invocation -> {
String path = invocation.getArgument(0, String.class);
zkDataStore.remove(path);
return null;
})
.when(mockZkClient)
.delete(anyString());

// 5. Simulate getContent behavior: retrieve value by key=path from zkDataStore
when(mockZkClient.getContent(anyString())).thenAnswer(invocation -> {
String path = invocation.getArgument(0, String.class);
return zkDataStore.get(path);
});

// 6. Construct a fake ZK URL and create ZookeeperMetadataReport using mockClientManager
URL fakeZkUrl = URL.valueOf("zookeeper://127.0.0.1:2181");
zookeeperMetadataReport = new ZookeeperMetadataReport(fakeZkUrl, mockClientManager);
}

private void deletePath(MetadataIdentifier metadataIdentifier, ZookeeperMetadataReport zookeeperMetadataReport) {
Expand All @@ -89,23 +120,20 @@ void testStoreProvider() throws ClassNotFoundException, InterruptedException {

String fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
Assertions.assertNotNull(fileContent);

deletePath(providerMetadataIdentifier, zookeeperMetadataReport);
fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 1000, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
Assertions.assertNull(fileContent);

providerMetadataIdentifier = storePrivider(zookeeperMetadataReport, interfaceName, version, group, application);
fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier));
Assertions.assertNotNull(fileContent);

FullServiceDefinition fullServiceDefinition = JsonUtils.toJavaObject(fileContent, FullServiceDefinition.class);
Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "zkTest");
Assertions.assertEquals("zkTest", fullServiceDefinition.getParameters().get("paramTest"));
}

@Test
Expand All @@ -119,21 +147,18 @@ void testConsumer() throws ClassNotFoundException, InterruptedException {

String fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
Assertions.assertNotNull(fileContent);

deletePath(consumerMetadataIdentifier, zookeeperMetadataReport);
fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 1000, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
Assertions.assertNull(fileContent);

consumerMetadataIdentifier = storeConsumer(zookeeperMetadataReport, interfaceName, version, group, application);
fileContent = zookeeperMetadataReport.zkClient.getContent(
zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
fileContent = waitSeconds(fileContent, 3000, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier));
Assertions.assertNotNull(fileContent);
Assertions.assertEquals(fileContent, "{\"paramConsumerTest\":\"zkCm\"}");
Assertions.assertEquals("{\"paramConsumerTest\":\"zkCm\"}", fileContent);
}

@Test
Expand All @@ -157,7 +182,7 @@ void testDoSaveMetadata() throws ExecutionException, InterruptedException {
}

@Test
void testDoRemoveMetadata() throws ExecutionException, InterruptedException {
void testDoRemoveMetadata() {
String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
String version = "1.0.0";
String group = null;
Expand All @@ -181,7 +206,7 @@ void testDoRemoveMetadata() throws ExecutionException, InterruptedException {
}

@Test
void testDoGetExportedURLs() throws ExecutionException, InterruptedException {
void testDoGetExportedURLs() {
String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
String version = "1.0.0";
String group = null;
Expand All @@ -194,7 +219,7 @@ void testDoGetExportedURLs() throws ExecutionException, InterruptedException {
zookeeperMetadataReport.doSaveMetadata(serviceMetadataIdentifier, url);

List<String> r = zookeeperMetadataReport.doGetExportedURLs(serviceMetadataIdentifier);
Assertions.assertTrue(r.size() == 1);
Assertions.assertEquals(1, r.size());

String fileContent = r.get(0);
Assertions.assertNotNull(fileContent);
Expand All @@ -203,17 +228,16 @@ void testDoGetExportedURLs() throws ExecutionException, InterruptedException {
}

@Test
void testDoSaveSubscriberData() throws ExecutionException, InterruptedException {
void testDoSaveSubscriberData() {
String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
String version = "1.0.0";
String group = null;
String application = "etc-metadata-report-consumer-test";
String revision = "90980";
String protocol = "xxx";
URL url = generateURL(interfaceName, version, group, application);
SubscriberMetadataIdentifier subscriberMetadataIdentifier =
new SubscriberMetadataIdentifier(application, revision);
String r = JsonUtils.toJson(Arrays.asList(url.toString()));
String r = JsonUtils.toJson(Collections.singletonList(url.toString()));
zookeeperMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);

String fileContent = zookeeperMetadataReport.zkClient.getContent(
Expand All @@ -225,17 +249,16 @@ void testDoSaveSubscriberData() throws ExecutionException, InterruptedException
}

@Test
void testDoGetSubscribedURLs() throws ExecutionException, InterruptedException {
void testDoGetSubscribedURLs() {
String interfaceName = "org.apache.dubbo.metadata.store.zookeeper.ZookeeperMetadataReport4TstService";
String version = "1.0.0";
String group = null;
String application = "etc-metadata-report-consumer-test";
String revision = "90980";
String protocol = "xxx";
URL url = generateURL(interfaceName, version, group, application);
SubscriberMetadataIdentifier subscriberMetadataIdentifier =
new SubscriberMetadataIdentifier(application, revision);
String r = JsonUtils.toJson(Arrays.asList(url.toString()));
String r = JsonUtils.toJson(Collections.singletonList(url.toString()));
zookeeperMetadataReport.doSaveSubscriberData(subscriberMetadataIdentifier, r);

String fileContent = zookeeperMetadataReport.zkClient.getContent(
Expand All @@ -259,12 +282,12 @@ private MetadataIdentifier storePrivider(

MetadataIdentifier providerMetadataIdentifier =
new MetadataIdentifier(interfaceName, version, group, PROVIDER_SIDE, application);
Class interfaceClass = Class.forName(interfaceName);
Class<?> interfaceClass = Class.forName(interfaceName);
FullServiceDefinition fullServiceDefinition =
ServiceDefinitionBuilder.buildFullDefinition(interfaceClass, url.getParameters());

zookeeperMetadataReport.storeProviderMetadata(providerMetadataIdentifier, fullServiceDefinition);
Thread.sleep(2000);
Thread.sleep(1000);
return providerMetadataIdentifier;
}

Expand All @@ -274,36 +297,23 @@ private MetadataIdentifier storeConsumer(
String version,
String group,
String application)
throws ClassNotFoundException, InterruptedException {
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":4444/" + interfaceName
+ "?version=" + version + "&application=" + application + (group == null ? "" : "&group=" + group));

throws InterruptedException {
MetadataIdentifier consumerMetadataIdentifier =
new MetadataIdentifier(interfaceName, version, group, CONSUMER_SIDE, application);
Class interfaceClass = Class.forName(interfaceName);

Map<String, String> tmp = new HashMap<>();
tmp.put("paramConsumerTest", "zkCm");
zookeeperMetadataReport.storeConsumerMetadata(consumerMetadataIdentifier, tmp);
Thread.sleep(2000);
Thread.sleep(1000);

return consumerMetadataIdentifier;
}

private String waitSeconds(String value, long moreTime, String path) throws InterruptedException {
if (value == null) {
Thread.sleep(moreTime);
return zookeeperMetadataReport.zkClient.getContent(path);
}
return value;
}

private URL generateURL(String interfaceName, String version, String group, String application) {
URL url = URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":8989/" + interfaceName
return URL.valueOf("xxx://" + NetUtils.getLocalAddress().getHostName() + ":8989/" + interfaceName
+ "?paramTest=etcdTest&version="
+ version + "&application="
+ application + (group == null ? "" : "&group=" + group));
return url;
}

@Test
Expand All @@ -313,13 +323,16 @@ void testMapping() throws InterruptedException {
String appNames = "demo1,demo2";

CountDownLatch latch = new CountDownLatch(1);
String pathKey = zookeeperMetadataReport.toRootDir() + DEFAULT_MAPPING_GROUP + "/" + serviceKey;
zkDataStore.put(pathKey, ""); // Alternatively, do not put anything, default to null

Set<String> serviceAppMapping = zookeeperMetadataReport.getServiceAppMapping(
serviceKey,
new MappingListener() {
@Override
public void onEvent(MappingChangedEvent event) {
Set<String> apps = event.getApps();
Assertions.assertEquals(apps.size(), 2);
Assertions.assertEquals(2, apps.size());
Assertions.assertTrue(apps.contains("demo1"));
Assertions.assertTrue(apps.contains("demo2"));
latch.countDown();
Expand All @@ -331,9 +344,8 @@ public void stop() {}
url);
Assertions.assertTrue(serviceAppMapping.isEmpty());

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

Expand Down
8 changes: 0 additions & 8 deletions dubbo-metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,4 @@
<module>dubbo-metadata-report-nacos</module>
</modules>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-test-check</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading