Skip to content

Commit

Permalink
修改单测
Browse files Browse the repository at this point in the history
  • Loading branch information
zyseap committed Apr 27, 2016
1 parent 83183d6 commit 9687017
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ protected String toNodePath(URL url, ZkNodeType nodeType) {
return toNodeTypePath(url, nodeType) + MotanConstants.PATH_SEPARATOR + url.getServerPortStr();
}

private void createNode(URL url, ZkNodeType nodeType) {
protected void createNode(URL url, ZkNodeType nodeType) {
String nodeTypePath = toNodeTypePath(url, nodeType);
if (!zkClient.exists(nodeTypePath)) {
zkClient.createPersistent(nodeTypePath, true);
}
zkClient.createEphemeral(toNodePath(url, nodeType), url.toFullStr());
}

private void removeNode(URL url, ZkNodeType nodeType) {
protected void removeNode(URL url, ZkNodeType nodeType) {
String nodePath = toNodePath(url, nodeType);
if (zkClient.exists(nodePath)) {
zkClient.delete(nodePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.weibo.api.motan.registry.zookeeper;

import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
Expand All @@ -10,19 +9,7 @@
import java.util.Properties;

public class EmbeddedZookeeper {
private static Properties properties = new Properties();

static {
InputStream in = EmbeddedZookeeper.class.getResourceAsStream("/zoo.cfg");
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}

private ZooKeeperServerMain zookeeperServer;
private Thread t1;

public void start() throws IOException, QuorumPeerConfig.ConfigException {
Properties properties = new Properties();
Expand All @@ -37,7 +24,7 @@ public void start() throws IOException, QuorumPeerConfig.ConfigException {
final ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumConfiguration);

t1 = new Thread(new Runnable() {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.weibo.api.motan.rpc.URL;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -37,6 +38,9 @@
public class ZookeeperRegistryTest {
private ZookeeperRegistry registry;
private ZkClient zkClient;
private URL clientUrl;
private URL url1;
private URL url2;

@Before
public void setUp() throws Exception {
Expand All @@ -47,77 +51,83 @@ public void setUp() throws Exception {
in.close();

// zookeeper://127.0.0.1:2181/com.weibo.api.motan.registry.RegistryService?group=yf_rpc
URL url = new URL("zookeeper", "127.0.0.1", port, "com.weibo.api.motan.registry.RegistryService");

URL zkUrl = new URL("zookeeper", "127.0.0.1", port, "com.weibo.api.motan.registry.RegistryService");
clientUrl = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 0, "com.weibo.motan.demo.service.MotanDemoService");
url1 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
url2 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8002, "com.weibo.motan.demo.service.MotanDemoService");

EmbeddedZookeeper embeddedZookeeper = new EmbeddedZookeeper();
embeddedZookeeper.start();

zkClient = new ZkClient("127.0.0.1:" + port);
registry = new ZookeeperRegistry(url, zkClient);
registry = new ZookeeperRegistry(zkUrl, zkClient);
}

@After
public void tearDown() throws Exception {
registry.removeNode(clientUrl, ZkNodeType.CLIENT);
registry.removeNode(url1, ZkNodeType.AVAILABLE_SERVER);
registry.removeNode(url1, ZkNodeType.UNAVAILABLE_SERVER);
registry.removeNode(url2, ZkNodeType.AVAILABLE_SERVER);
registry.removeNode(url2, ZkNodeType.UNAVAILABLE_SERVER);
}

@Test
public void testDoRegister() {
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
registry.doRegister(url);
registry.doRegister(url1);

assertTrue(zkClient.exists(registry.toNodePath(url, ZkNodeType.UNAVAILABLE_SERVER)));
assertTrue(zkClient.exists(registry.toNodePath(url1, ZkNodeType.UNAVAILABLE_SERVER)));
}

@Test
public void testDoUnregister() {
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
registry.doUnregister(url);
registry.doUnregister(url1);

assertFalse(zkClient.exists(registry.toNodePath(url, ZkNodeType.UNAVAILABLE_SERVER)));
assertFalse(zkClient.exists(registry.toNodePath(url, ZkNodeType.AVAILABLE_SERVER)));
assertFalse(zkClient.exists(registry.toNodePath(url1, ZkNodeType.UNAVAILABLE_SERVER)));
assertFalse(zkClient.exists(registry.toNodePath(url1, ZkNodeType.AVAILABLE_SERVER)));
}

@Test
public void testDoSubscribe() {
final URL serverUrl = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 0, "com.weibo.motan.demo.service.MotanDemoService");
NotifyListener notifyListener = new NotifyListener() {
@Override
public void notify(URL registryUrl, List<URL> urls) {
}
};
registry.doSubscribe(serverUrl, notifyListener);
registry.doSubscribe(clientUrl, notifyListener);

ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>> urlListeners = registry.getUrlListeners();
assertTrue(urlListeners.containsKey(serverUrl));
assertTrue(zkClient.exists(registry.toNodePath(serverUrl, ZkNodeType.CLIENT)));
assertTrue(urlListeners.containsKey(clientUrl));
assertFalse(urlListeners.get(clientUrl).isEmpty());
assertTrue(zkClient.exists(registry.toNodePath(clientUrl, ZkNodeType.CLIENT)));
}

@Test
public void testDoUnsubscribe() {
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
NotifyListener notifyListener = new NotifyListener() {
@Override
public void notify(URL registryUrl, List<URL> urls) {
}
};
registry.doUnsubscribe(url, notifyListener);

ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>> urlListeners = registry.getUrlListeners();
assertFalse(urlListeners.containsKey(url));
registry.doSubscribe(clientUrl, notifyListener);
registry.doUnsubscribe(clientUrl, notifyListener);

assertTrue(urlListeners.get(clientUrl).isEmpty());
}

@Test
public void testDoDiscover() {
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
registry.doRegister(url);
registry.doAvailable(url);
List<URL> urls = registry.doDiscover(url);
registry.doRegister(url1);
registry.doAvailable(url1);
List<URL> urls = registry.doDiscover(url1);

assertTrue(urls.contains(url));
assertTrue(urls.contains(url1));
}

@Test
public void testDoAvailable() throws Exception {
final Set<URL> urls = new HashSet<URL>();
URL url1 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
URL url2 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8002, "com.weibo.motan.demo.service.MotanDemoService");
urls.add(url1);
urls.add(url2);
for (URL u : urls) {
Expand All @@ -137,9 +147,7 @@ public void testDoAvailable() throws Exception {

@Test
public void testDoUnavailable() throws Exception {
final Set<URL> urls = new HashSet<URL>();
URL url1 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService");
URL url2 = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8002, "com.weibo.motan.demo.service.MotanDemoService");
Set<URL> urls = new HashSet<URL>();
urls.add(url1);
urls.add(url2);
for (URL u : urls) {
Expand Down

0 comments on commit 9687017

Please sign in to comment.