Skip to content

Commit

Permalink
add "Enhance shenyu-register-center module unit test apache#1457 and …
Browse files Browse the repository at this point in the history
…ZookeeperClientRegisterRepository buildMetadataNodeName add startsWith (apache#1489)
  • Loading branch information
zhaiweij authored May 19, 2021
1 parent 1482285 commit 1a44286
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<tars.version>1.7.2</tars.version>
<skipTests>false</skipTests>
<undertow.version>2.2.2.Final</undertow.version>
<curator-test.version>3.3.0</curator-test.version>
<curator-test.version>4.0.1</curator-test.version>
<wiremock.version>2.18.0</wiremock.version>
<zookeeper.version>3.5.6</zookeeper.version>
<zkclient.version>0.10</zkclient.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>${curator-test.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ZookeeperClientRegisterRepository implements ShenyuClientRegisterRe

private ZkClient zkClient;

private Map<String, String> nodeDataMap = new HashMap<>();
private final Map<String, String> nodeDataMap = new HashMap<>();

@Override
public void init(final ShenyuRegisterCenterConfig config) {
Expand Down Expand Up @@ -110,34 +110,30 @@ private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
if (RpcTypeEnum.HTTP.getName().equals(rpcType) || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
nodeName = String.join("-", metadata.getContextPath(), metadata.getRuleName().replace("/", "-"));
} else {
nodeName = buildNodeName(metadata.getServiceName(), metadata.getMethodName());
nodeName = RegisterPathConstants.buildNodeName(metadata.getServiceName(), metadata.getMethodName());
}
return nodeName.substring(1);
}

private String buildNodeName(final String serviceName, final String methodName) {
return String.join(DOT_SEPARATOR, serviceName, methodName);
return nodeName.startsWith("/") ? nodeName.substring(1) : nodeName;
}

private class ZkStateListener implements IZkStateListener {
@Override
public void handleStateChanged(final Watcher.Event.KeeperState keeperState) throws Exception {
public void handleStateChanged(final Watcher.Event.KeeperState keeperState) {
if (Watcher.Event.KeeperState.SyncConnected.equals(keeperState)) {
nodeDataMap.forEach((k, v) -> {
if (!zkClient.exists(k)) {
zkClient.createEphemeral(k, v);
log.info("zookeeper client reregister success: {}", v);
log.info("zookeeper client register success: {}", v);
}
});
}
}

@Override
public void handleNewSession() throws Exception {
public void handleNewSession() {
}

@Override
public void handleSessionEstablishmentError(final Throwable throwable) throws Exception {
public void handleSessionEstablishmentError(final Throwable throwable) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
package org.apache.shenyu.register.client.zookeeper;

import org.I0Itec.zkclient.ZkClient;
import org.apache.curator.test.TestingServer;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -32,6 +38,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

Expand All @@ -40,12 +47,24 @@
*/
public class ZookeeperClientRegisterRepositoryTest {

private static TestingServer zkServer;

private ZookeeperClientRegisterRepository repository;

private final Map<String, Object> zookeeperBroker = new HashMap<>();

private final Set<String> ephemeralNode = new HashSet<>();

@BeforeClass
public static void setUpBeforeClass() throws Exception {
zkServer = new TestingServer(2181, true);
}

@AfterClass
public static void tearDownAfterClass() throws IOException {
zkServer.stop();
}

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
this.repository = new ZookeeperClientRegisterRepository();
Expand Down Expand Up @@ -122,4 +141,42 @@ public void testPersistInterface() {
assert !zookeeperBroker.containsKey(uriPath);
}

@Test
public void testPersistInterfaceDoAnswerWriteData4Grpc() {

ZkClient zkClient = mock(ZkClient.class);

when(zkClient.exists(anyString())).thenReturn(true);

final MetaDataRegisterDTO data = MetaDataRegisterDTO.builder()
.rpcType(RpcTypeEnum.GRPC.getName())
.host("host")
.port(80)
.contextPath("/context")
.ruleName("ruleName")
.serviceName("testService")
.methodName("testMethod")
.build();

repository.persistInterface(data);
String metadataPath = "/shenyu/register/metadata/grpc/context/testService.testMethod";
assert zookeeperBroker.containsKey(metadataPath);
assert zookeeperBroker.get(metadataPath).equals(GsonUtils.getInstance().toJson(data));

String uriPath = "/shenyu/register/uri/grpc/context/host:80";
assert zookeeperBroker.containsKey(uriPath);
assert zookeeperBroker.get(uriPath).equals(GsonUtils.getInstance().toJson(URIRegisterDTO.transForm(data)));

repository.close();
assert zookeeperBroker.containsKey(metadataPath);
assert !zookeeperBroker.containsKey(uriPath);
}

@Test
public void testInit() {
ShenyuRegisterCenterConfig config = new ShenyuRegisterCenterConfig();
config.setServerLists("127.0.0.1:2181");
repository.init(config);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class RegisterPathConstants {
* constants of separator.
*/
private static final String SEPARATOR = "/";

/**
* Dot separator.
*/
private static final String DOT_SEPARATOR = ".";

/**
* build child path of "/shenyu/register/metadata/{rpcType}/".
Expand Down Expand Up @@ -135,4 +140,15 @@ public static String buildServiceConfigPath(final String rpcType, final String c
return String.join(SEPARATOR, ROOT_PATH, "service", rpcType, contextPath)
.replace("/", ".").substring(1);
}

/**
* Build zookeeper node name.
*
* @param serviceName the service name
* @param methodName the method name
* @return the string
*/
public static String buildNodeName(final String serviceName, final String methodName) {
return String.join(DOT_SEPARATOR, serviceName, methodName);
}
}

0 comments on commit 1a44286

Please sign in to comment.