From 9f1d1e4d0db29dad1505746a71b950192b8c3b27 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 | 2 +- .../registry/zookeeper/EmbeddedZookeeper.java | 51 ------- .../zookeeper/ZookeeperRegistryTest.java | 141 +++++++----------- .../src/test/resources/zoo.cfg | 28 ---- 4 files changed, 58 insertions(+), 164 deletions(-) delete mode 100644 motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java delete mode 100644 motan-registry-zookeeper/src/test/resources/zoo.cfg 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..576f7d3ab 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 @@ -194,7 +194,7 @@ private String toNodeTypePath(URL url, ZkNodeType nodeType) { return toServicePath(url) + MotanConstants.PATH_SEPARATOR + type; } - protected String toNodePath(URL url, ZkNodeType nodeType) { + private String toNodePath(URL url, ZkNodeType nodeType) { return toNodeTypePath(url, nodeType) + MotanConstants.PATH_SEPARATOR + url.getServerPortStr(); } 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 deleted file mode 100644 index d5fad717d..000000000 --- a/motan-registry-zookeeper/src/test/java/com/weibo/api/motan/registry/zookeeper/EmbeddedZookeeper.java +++ /dev/null @@ -1,51 +0,0 @@ -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; - -import java.io.IOException; -import java.io.InputStream; -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(); - InputStream in = EmbeddedZookeeper.class.getResourceAsStream("/zoo.cfg"); - properties.load(in); - - QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig(); - quorumConfiguration.parseProperties(properties); - in.close(); - - zookeeperServer = new ZooKeeperServerMain(); - final ServerConfig configuration = new ServerConfig(); - configuration.readFrom(quorumConfiguration); - - t1 = new Thread(new Runnable() { - @Override - public void run() { - try { - zookeeperServer.runFromConfig(configuration); - } catch (IOException e) { - } - } - }); - t1.start(); - } -} \ No newline at end of file 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..5b294c4f5 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,139 +21,112 @@ import com.weibo.api.motan.rpc.URL; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.ZkClient; +import org.jmock.Expectations; +import org.jmock.integration.junit4.JUnit4Mockery; +import org.jmock.lib.legacy.ClassImposteriser; import org.junit.Before; import org.junit.Test; -import java.io.InputStream; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import java.util.Properties; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ZookeeperRegistryTest { + private static JUnit4Mockery mockery = null; private ZookeeperRegistry registry; - private ZkClient zkClient; + private URL url; + private URL clientUrl; @Before public void setUp() throws Exception { - Properties properties = new Properties(); - InputStream in = EmbeddedZookeeper.class.getResourceAsStream("/zoo.cfg"); - properties.load(in); - int port = Integer.parseInt(properties.getProperty("clientPort")); - 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"); - - - EmbeddedZookeeper embeddedZookeeper = new EmbeddedZookeeper(); - embeddedZookeeper.start(); + URL zkUrl = new URL("zookeeper", "127.0.0.1", 2181, "com.weibo.api.motan.registry.RegistryService"); + mockery = new JUnit4Mockery() { + { + setImposteriser(ClassImposteriser.INSTANCE); + } + }; - zkClient = new ZkClient("127.0.0.1:" + port); - registry = new ZookeeperRegistry(url, zkClient); + ZkClient mockZkClient = mockery.mock(ZkClient.class); + registry = new ZookeeperRegistry(zkUrl, mockZkClient); + + url = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 8001, "com.weibo.motan.demo.service.MotanDemoService"); + clientUrl = new URL(MotanConstants.PROTOCOL_MOTAN, "127.0.0.1", 0, "com.weibo.motan.demo.service.MotanDemoService"); + + final List currentChilds = new ArrayList(); + + mockery.checking(new Expectations() { + { + allowing(any(ZkClient.class)).method("exists"); + will(returnValue(false)); + allowing(any(ZkClient.class)).method("delete"); + will(returnValue(true)); + allowing(any(ZkClient.class)).method("createPersistent"); + will(returnValue(null)); + allowing(any(ZkClient.class)).method("createEphemeral"); + will(returnValue(null)); + allowing(any(ZkClient.class)).method("subscribeChildChanges"); + will(returnValue(currentChilds)); + allowing(any(ZkClient.class)).method("unsubscribeChildChanges"); + will(returnValue(null)); + allowing(any(ZkClient.class)).method("readData"); + will(returnValue("motan://127.0.0.1:8001/com.weibo.motan.demo.service.MotanDemoService?export=demoMotan:8002&protocol=motan&module=motan-demo-rpc&application=myMotanDemo&group=motan-demo-rpc&nodeType=service")); + allowing(any(ZkClient.class)).method("getChildren"); + will(returnValue(currentChilds)); + } + }); } @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); - - assertTrue(zkClient.exists(registry.toNodePath(url, ZkNodeType.UNAVAILABLE_SERVER))); + registry.register(url); + Collection registeredUrls = registry.getRegisteredServiceUrls(); + assertTrue(registeredUrls.contains(url)); } @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); - - assertFalse(zkClient.exists(registry.toNodePath(url, ZkNodeType.UNAVAILABLE_SERVER))); - assertFalse(zkClient.exists(registry.toNodePath(url, ZkNodeType.AVAILABLE_SERVER))); + registry.register(url); + registry.unregister(url); + Collection registeredUrls = registry.getRegisteredServiceUrls(); + assertFalse(registeredUrls.contains(url)); } @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()); } @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); - + registry.doSubscribe(clientUrl, notifyListener); + registry.doUnsubscribe(clientUrl, notifyListener); ConcurrentHashMap> urlListeners = registry.getUrlListeners(); - assertFalse(urlListeners.containsKey(url)); + 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); - - assertTrue(urls.contains(url)); - } - - @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) { - registry.register(u); - } - - registry.available(url1); - assertTrue(zkClient.exists(registry.toNodePath(url1, ZkNodeType.AVAILABLE_SERVER))); - assertFalse(zkClient.exists(registry.toNodePath(url1, ZkNodeType.UNAVAILABLE_SERVER))); - - registry.available(null); - for (URL u : urls) { - assertTrue(zkClient.exists(registry.toNodePath(u, ZkNodeType.AVAILABLE_SERVER))); - assertFalse(zkClient.exists(registry.toNodePath(u, ZkNodeType.UNAVAILABLE_SERVER))); - } - } - - @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"); - urls.add(url1); - urls.add(url2); - for (URL u : urls) { - registry.register(u); - } - - registry.unavailable(url1); - assertFalse(zkClient.exists(registry.toNodePath(url1, ZkNodeType.AVAILABLE_SERVER))); - assertTrue(zkClient.exists(registry.toNodePath(url1, ZkNodeType.UNAVAILABLE_SERVER))); - - registry.unavailable(null); - for (URL u : urls) { - assertFalse(zkClient.exists(registry.toNodePath(u, ZkNodeType.AVAILABLE_SERVER))); - assertTrue(zkClient.exists(registry.toNodePath(u, ZkNodeType.UNAVAILABLE_SERVER))); - } + List urls = registry.doDiscover(clientUrl); + urls.contains(url); } } diff --git a/motan-registry-zookeeper/src/test/resources/zoo.cfg b/motan-registry-zookeeper/src/test/resources/zoo.cfg deleted file mode 100644 index 5f1a4d5ff..000000000 --- a/motan-registry-zookeeper/src/test/resources/zoo.cfg +++ /dev/null @@ -1,28 +0,0 @@ -# The number of milliseconds of each tick -tickTime=2000 -# The number of ticks that the initial -# synchronization phase can take -initLimit=10 -# The number of ticks that can pass between -# sending a request and getting an acknowledgement -syncLimit=5 -# the directory where the snapshot is stored. -# do not use /tmp for storage, /tmp here is just -# example sakes. -dataDir=/tmp/zookeeper/motan-test -# the port at which the clients will connect -clientPort=2182 -# the maximum number of client connections. -# increase this if you need to handle more clients -#maxClientCnxns=60 -# -# Be sure to read the maintenance section of the -# administrator guide before turning on autopurge. -# -# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance -# -# The number of snapshots to retain in dataDir -#autopurge.snapRetainCount=3 -# Purge task interval in hours -# Set to "0" to disable auto purge feature -#autopurge.purgeInterval=1 \ No newline at end of file