some small refactors & add tests

This commit is contained in:
DigitalSonic
2020-02-06 13:53:28 +08:00
parent eb90260afe
commit 34aa0c8cd1
10 changed files with 113 additions and 43 deletions
@@ -5,7 +5,8 @@ import org.junit.Test;
import java.util.List;
import java.util.Properties;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class NodeEventTest {
@@ -11,7 +11,9 @@ import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PoolUpdaterTest {
private HighAvailableDataSource haDataSource;
@@ -1,6 +1,8 @@
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.apache.curator.test.TestingServer;
@@ -17,6 +19,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -35,6 +40,48 @@ public class ZookeeperNodeListenerTest {
server.close();
}
@Test
public void testHaWithZk() throws Exception {
// 1. Init HighAvailableDataSource
ZookeeperNodeListener listener = new ZookeeperNodeListener();
listener.setZkConnectString(server.getConnectString());
listener.setPrefix("foo");
listener.setPath(PATH);
listener.setUrlTemplate("jdbc:derby:memory:${database};create=true");
HighAvailableDataSource dataSource = new HighAvailableDataSource();
dataSource.setNodeListener(listener);
dataSource.setPoolPurgeIntervalSeconds(10);
dataSource.init();
assertTrue(dataSource.getDataSourceMap().isEmpty());
// 2. Register one Node
ZookeeperNodeRegister register = registerNodeAndReturnRegister();
Thread.sleep(3000); // Wait for the Node to be created.
assertFalse(dataSource.getDataSourceMap().isEmpty());
assertEquals(1, dataSource.getAvailableDataSourceMap().size());
DruidDataSource ds = (DruidDataSource) dataSource.getAvailableDataSourceMap().get("foo.test-foo");
assertEquals("jdbc:derby:memory:foo;create=true", ds.getUrl());
assertNotNull(dataSource.getConnection());
// 3. Delete one Node
register.destroy();
boolean checkBlackList = false;
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
if (dataSource.isInBlackList("foo.test-foo")) {
assertTrue(dataSource.getAvailableDataSourceMap().isEmpty());
assertFalse(dataSource.getDataSourceMap().isEmpty());
assertNull(dataSource.getConnection());
checkBlackList = true;
break;
}
}
assertTrue(checkBlackList);
dataSource.destroy();
}
@Test
public void testAddOneNode() throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
@@ -42,7 +89,7 @@ public class ZookeeperNodeListenerTest {
listener.setZkConnectString(server.getConnectString());
listener.setPath(PATH);
listener.setUrlTemplate("jdbc:mysql://${host}:${port}/foo");
listener.setDataPrefix("foo");
listener.setPrefix("foo");
listener.setObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
@@ -51,8 +98,8 @@ public class ZookeeperNodeListenerTest {
NodeEvent event = events[0];
LOG.info("NodeEvent received: " + event);
if (NodeEventTypeEnum.ADD == event.getType()) {
assertEquals("foo", event.getUsername());
assertEquals("password", event.getPassword());
assertEquals("sa", event.getUsername());
assertEquals("", event.getPassword());
assertEquals("foo.test-foo", event.getNodeName());
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
event.getUrl());
@@ -67,9 +114,9 @@ public class ZookeeperNodeListenerTest {
cdl.await(10, TimeUnit.SECONDS);
Properties properties = listener.getProperties();
assertEquals(5, properties.size());
assertEquals("foo", properties.getProperty("foo.test-foo.username"));
assertEquals("password", properties.getProperty("foo.test-foo.password"));
assertEquals(6, properties.size());
assertEquals("sa", properties.getProperty("foo.test-foo.username"));
assertEquals("", properties.getProperty("foo.test-foo.password"));
assertEquals("127.0.0.1", properties.getProperty("foo.test-foo.host"));
assertEquals("1234", properties.getProperty("foo.test-foo.port"));
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
@@ -89,7 +136,7 @@ public class ZookeeperNodeListenerTest {
listener.setZkConnectString(server.getConnectString());
listener.setPath(PATH);
listener.setUrlTemplate("jdbc:mysql://${host}:${port}/foo");
listener.setDataPrefix("foo");
listener.setPrefix("foo");
listener.setObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
@@ -98,8 +145,8 @@ public class ZookeeperNodeListenerTest {
NodeEvent event = events[0];
LOG.info("NodeEvent received: " + event);
if (NodeEventTypeEnum.DELETE == event.getType()) {
assertEquals("foo", event.getUsername());
assertEquals("password", event.getPassword());
assertEquals("sa", event.getUsername());
assertEquals("", event.getPassword());
assertEquals("foo.test-foo", event.getNodeName());
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
event.getUrl());
@@ -113,7 +160,7 @@ public class ZookeeperNodeListenerTest {
listener.update();
addCDL.await(10, TimeUnit.SECONDS);
Properties properties = listener.getProperties();
assertEquals(5, properties.size());
assertEquals(6, properties.size());
register.destroy();
removeCDL.await(10, TimeUnit.SECONDS);
@@ -132,8 +179,9 @@ public class ZookeeperNodeListenerTest {
node.setPrefix("foo");
node.setHost("127.0.0.1");
node.setPort(1234);
node.setUsername("foo");
node.setPassword("password");
node.setDatabase("foo");
node.setUsername("sa");
node.setPassword("");
payload.add(node);
register.register("test-foo", payload);
return register;
@@ -3,8 +3,6 @@ package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;
import org.junit.After;
import org.junit.AfterClass;
@@ -17,12 +15,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ZookeeperNodeRegisterTest {
private final static Log LOG = LogFactory.getLog(ZookeeperNodeRegisterTest.class);
private final String PATH = "/ha-druid-datasource";
private static TestingServer server;
private final String PATH = "/ha-druid-datasource";
private ZookeeperNodeRegister register;
@BeforeClass
@@ -56,6 +56,7 @@ public class ZookeeperNodeRegisterTest {
node1.setPrefix("foo");
node1.setHost("127.0.0.1");
node1.setPort(1234);
node1.setDatabase("foo_db");
node1.setUsername("foo");
node1.setPassword("password");
payload.add(node1);
@@ -87,6 +88,9 @@ public class ZookeeperNodeRegisterTest {
properties.load(new StringReader(str));
validateNodeProperties(node1, properties);
validateNodeProperties(node2, properties);
assertTrue(properties.containsKey("foo.database"));
assertFalse(properties.containsKey("bar.database"));
assertEquals("foo_db", properties.getProperty("foo.database"));
}
private void validateNodeProperties(ZookeeperNodeInfo node, Properties properties) {