From 4f45e7f8dcb6520fe23379f39b794afa54a81f76 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 5 Nov 2019 18:01:08 +0800 Subject: [PATCH] add Testcases --- .../ha/CounterValidConnectionChecker.java | 45 ++++++ ...andomDataSourceSelectorWithFilterTest.java | 137 ++++++++++++++++++ ...mDataSourceSelectorWithValidationTest.java | 23 ++- 3 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java diff --git a/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java b/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java new file mode 100644 index 0000000000..c40798ee8f --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java @@ -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 counterMap = new ConcurrentHashMap(); + + @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; + } +} diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java new file mode 100644 index 0000000000..7b42b8337a --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java @@ -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 map = highAvailableDataSource.getDataSourceMap(); + for (DataSource ds : map.values()) { + DruidDataSource dds = (DruidDataSource) ds; + dds.setValidConnectionChecker(checker); + } + } +} diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java index 50c284818f..2e919b20bb 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java @@ -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; + } }