From 96870177faadb2ee81fb48f270d076a6add06bab Mon Sep 17 00:00:00 2001 From: Jake Zhang Date: Wed, 27 Apr 2016 18:26:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../registry/zookeeper/ZookeeperRegistry.java | 4 +- .../registry/zookeeper/EmbeddedZookeeper.java | 15 +---- .../zookeeper/ZookeeperRegistryTest.java | 64 +++++++++++-------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/motan-registry-zookeeper/src/main/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistry.java b/motan-registry-zookeeper/src/main/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistry.java index 245320879..8d191d75c 100644 --- a/motan-registry-zookeeper/src/main/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistry.java +++ b/motan-registry-zookeeper/src/main/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistry.java @@ -198,7 +198,7 @@ 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); @@ -206,7 +206,7 @@ private void createNode(URL url, ZkNodeType nodeType) { 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); diff --git a/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java b/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java index d5fad717d..8cb3158c5 100644 --- a/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java +++ b/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java @@ -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; @@ -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(); @@ -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 { diff --git a/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistryTest.java b/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistryTest.java index 8f83e3adf..c706c5080 100644 --- a/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistryTest.java +++ b/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/ZookeeperRegistryTest.java @@ -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; @@ -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 { @@ -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 urls) { } }; - registry.doSubscribe(serverUrl, notifyListener); + registry.doSubscribe(clientUrl, notifyListener); ConcurrentHashMap> 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 urls) { } }; - registry.doUnsubscribe(url, notifyListener); - ConcurrentHashMap> 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 urls = registry.doDiscover(url); + registry.doRegister(url1); + registry.doAvailable(url1); + List urls = registry.doDiscover(url1); - assertTrue(urls.contains(url)); + assertTrue(urls.contains(url1)); } @Test public void testDoAvailable() throws Exception { final Set urls = new HashSet(); - 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) { @@ -137,9 +147,7 @@ public void testDoAvailable() throws Exception { @Test public void testDoUnavailable() throws Exception { - final Set urls = new HashSet(); - 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 urls = new HashSet(); urls.add(url1); urls.add(url2); for (URL u : urls) {