some small refactors

1. refactor RandomDataSourceSelector related classes
2. fix some testcases
This commit is contained in:
DigitalSonic
2020-02-07 15:17:50 +08:00
parent a48c2fd493
commit 2466ac220f
11 changed files with 105 additions and 27 deletions
@@ -22,6 +22,7 @@ public class HighAvailableDataSourceTest {
haDataSource.init();
haDataSource.setTargetDataSource("foo");
assertNotNull(haDataSource.getConnection());
haDataSource.destroy();
}
@Test
@@ -43,5 +44,6 @@ public class HighAvailableDataSourceTest {
}
assertTrue(count[0] > 0);
assertTrue(count[1] > 0);
haDataSource.destroy();
}
}
@@ -31,6 +31,7 @@ public class PoolUpdaterTest {
@After
public void tearDown() throws Exception {
haDataSource.destroy();
haDataSource = null;
updater = null;
}
@@ -36,7 +36,7 @@ public class RandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTe
assertEquals(60, selector.getValidateThread().getCheckingIntervalSeconds());
assertEquals(120, selector.getRecoveryIntervalSeconds());
assertEquals(120, selector.getRecoverThread().getSleepSeconds());
assertEquals(120, selector.getRecoverThread().getRecoverIntervalSeconds());
assertEquals(10, selector.getValidationSleepSeconds());
assertEquals(10, selector.getValidateThread().getValidationSleepSeconds());
@@ -65,7 +65,7 @@ public class RandomDataSourceSelectorWithFilterTest {
Thread.sleep(1000);
assertEquals(initValue, checker.getCountValue(url));
Thread.sleep(2500);
Thread.sleep(3000);
int value = checker.getCountValue(url);
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
assertEquals(initValue + 1, value);
@@ -20,6 +20,7 @@ public class RandomDataSourceSelectorWithValidationTest {
highAvailableDataSource = new HighAvailableDataSource();
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
highAvailableDataSource.setDataSourceFile(file);
initSelector(highAvailableDataSource);
highAvailableDataSource.init();
}
@@ -31,7 +32,7 @@ public class RandomDataSourceSelectorWithValidationTest {
@Test
public void testOneDataSourceFailAndRecover() throws Exception {
RandomDataSourceSelector selector = initSelector();
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo");
dataSource.setValidationQuery("select xxx from yyy");
@@ -40,7 +41,6 @@ public class RandomDataSourceSelectorWithValidationTest {
for (int i = 0; i < 100; i++) {
assertNotEquals(dataSource, selector.get());
}
dataSource.setValidationQuery(null);
Thread.sleep(4 * 1000);
assertFalse(dataSource.isTestOnReturn());
@@ -55,8 +55,7 @@ public class RandomDataSourceSelectorWithValidationTest {
@Test
public void testAllDataSourceFail() throws Exception {
initSelector();
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo");
DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("bar");
@@ -80,15 +79,16 @@ public class RandomDataSourceSelectorWithValidationTest {
assertTrue(count[1] > 0);
}
private RandomDataSourceSelector initSelector() {
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
private RandomDataSourceSelector initSelector(HighAvailableDataSource dataSource) {
RandomDataSourceSelector selector = new RandomDataSourceSelector(dataSource);
RandomDataSourceValidateThread validateThread = new RandomDataSourceValidateThread(selector);
RandomDataSourceRecoverThread recoverThread = new RandomDataSourceRecoverThread(selector);
validateThread.setCheckingIntervalSeconds(3);
recoverThread.setSleepSeconds(3);
recoverThread.setRecoverIntervalSeconds(3);
selector.setValidateThread(validateThread);
selector.setRecoverThread(recoverThread);
selector.init();
dataSource.setDataSourceSelector(selector);
return selector;
}
}