add Testcases

This commit is contained in:
DigitalSonic
2019-11-05 18:01:08 +08:00
parent ea16b58cc0
commit 4f45e7f8dc
3 changed files with 197 additions and 8 deletions
@@ -0,0 +1,45 @@
package com.alibaba.druid.pool.ha;
import com.alibaba.druid.pool.ValidConnectionChecker;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
public class CounterValidConnectionChecker implements ValidConnectionChecker {
private int countBeforeFailure = 100;
private Map<String, Integer> counterMap = new ConcurrentHashMap<String, Integer>();
@Override
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) throws Exception {
String url = c.getMetaData().getURL();
if (!counterMap.containsKey(url)) {
counterMap.put(url, 0);
}
Integer counter = counterMap.get(url);
counterMap.put(url, ++counter);
if (counter >= countBeforeFailure) {
return false;
}
return true;
}
@Override
public void configFromProperties(Properties properties) {
}
public int getCountValue(String url) {
return counterMap.containsKey(url) ? counterMap.get(url) : 0;
}
public int getCountBeforeFailure() {
return countBeforeFailure;
}
public void setCountBeforeFailure(int countBeforeFailure) {
this.countBeforeFailure = countBeforeFailure;
}
}
@@ -0,0 +1,137 @@
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.CounterValidConnectionChecker;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class RandomDataSourceSelectorWithFilterTest {
private final static Log LOG = LogFactory.getLog(RandomDataSourceSelectorWithFilterTest.class);
private HighAvailableDataSource highAvailableDataSource;
private CounterValidConnectionChecker checker;
@Before
public void setUp() throws Exception {
LOG.info("setUp()");
checker = new CounterValidConnectionChecker();
highAvailableDataSource = new HighAvailableDataSource();
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
highAvailableDataSource.setDataSourceFile(file);
highAvailableDataSource.setFilters("haRandomValidator");
highAvailableDataSource.init();
initChecker();
}
@After
public void tearDown() {
LOG.info("tearDown()");
highAvailableDataSource.destroy();
highAvailableDataSource = null;
checker = null;
}
@Test
public void testSkipValidation() throws Exception {
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
RandomDataSourceValidateThread thread = new RandomDataSourceValidateThread(selector);
thread.setCheckingIntervalSeconds(2);
selector.setValidateThread(thread);
selector.init();
Connection conn = highAvailableDataSource.getConnection();
createTable(conn);
String url = conn.getMetaData().getURL();
Thread.sleep(5000);
int initValue = checker.getCountValue(url);
executeSQL(conn, "select * from foo");
Thread.sleep(1000);
executeSQL(conn, "select * from foo");
Thread.sleep(1000);
assertEquals(initValue, checker.getCountValue(url));
Thread.sleep(3000);
int value = checker.getCountValue(url);
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
assertEquals(initValue + 1, value);
Thread.sleep(2000);
value = checker.getCountValue(url);
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
assertEquals(initValue + 2, value);
conn.close();
}
@Test
public void testForceValidation() throws Exception {
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
RandomDataSourceValidateThread thread = new RandomDataSourceValidateThread(selector);
thread.setCheckingIntervalSeconds(2);
selector.setValidateThread(thread);
selector.init();
Connection conn = highAvailableDataSource.getConnection();
createTable(conn);
String url = conn.getMetaData().getURL();
Thread.sleep(2000);
LOG.info("Start execute SQL");
int initValue = 0;
int value = 0;
for (int i = 0; i < 10; i++) {
executeSQL(conn, "select * from foo");
if (initValue == 0) {
initValue = checker.getCountValue(url);
}
Thread.sleep(600);
value = checker.getCountValue(url);
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
assertEquals(initValue, value);
}
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
Thread.sleep(5000);
value = checker.getCountValue(url);
assertTrue(value > initValue + 1);
}
private void createTable(Connection connection) {
try {
executeSQL(connection, "create table foo (bar varchar(1))");
} catch(SQLException e) {
try {
executeSQL(connection, "drop table foo");
executeSQL(connection, "create table foo (bar varchar(1))");
} catch (SQLException ex) {
// ignore
}
}
}
private void executeSQL(Connection connection, String sql) throws SQLException {
Statement stmt = connection.createStatement();
stmt.execute(sql);
LOG.info("Executing " + sql);
stmt.close();
}
private void initChecker() {
Map<String, DataSource> map = highAvailableDataSource.getDataSourceMap();
for (DataSource ds : map.values()) {
DruidDataSource dds = (DruidDataSource) ds;
dds.setValidConnectionChecker(checker);
}
}
}
@@ -30,10 +30,7 @@ public class RandomDataSourceSelectorWithValidationTest {
@Test
public void testOneDataSourceFailAndRecover() throws Exception {
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
selector.getValidateThread().setCheckingIntervalSeconds(3);
selector.getRecoverThread().setSleepSeconds(3);
selector.init();
RandomDataSourceSelector selector = initSelector();
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
dataSource.setValidationQuery("select xxx from yyy");
@@ -57,10 +54,8 @@ public class RandomDataSourceSelectorWithValidationTest {
@Test
public void testAllDataSourceFail() throws Exception {
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
selector.setCheckingIntervalSeconds(3);
selector.setRecoveryIntervalSeconds(3);
selector.init();
initSelector();
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("bar");
@@ -83,4 +78,16 @@ public class RandomDataSourceSelectorWithValidationTest {
assertTrue(count[0] > 0);
assertTrue(count[1] > 0);
}
private RandomDataSourceSelector initSelector() {
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
RandomDataSourceValidateThread validateThread = new RandomDataSourceValidateThread(selector);
RandomDataSourceRecoverThread recoverThread = new RandomDataSourceRecoverThread(selector);
validateThread.setCheckingIntervalSeconds(3);
recoverThread.setSleepSeconds(3);
selector.setValidateThread(validateThread);
selector.setRecoverThread(recoverThread);
selector.init();
return selector;
}
}