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 9f1d1e4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<String> currentChilds = new ArrayList<String>();

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<URL> 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<URL> 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<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());
}

@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);

registry.doSubscribe(clientUrl, notifyListener);
registry.doUnsubscribe(clientUrl, notifyListener);
ConcurrentHashMap<URL, ConcurrentHashMap<NotifyListener, IZkChildListener>> 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<URL> urls = registry.doDiscover(url);

assertTrue(urls.contains(url));
}

@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) {
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<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) {
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<URL> urls = registry.doDiscover(clientUrl);
urls.contains(url);
}
}
28 changes: 0 additions & 28 deletions motan-registry-zookeeper/src/test/resources/zoo.cfg

This file was deleted.

0 comments on commit 9f1d1e4

Please sign in to comment.