slightly modify

Use ConcurrentHashMap instead of HashMap in RandomDataSourceValidateThread.
Add some Log in StickyRandomDataSourceSelector.
Add some test cases for StickyRandomDataSourceSelect.
This commit is contained in:
DigitalSonic
2019-02-02 16:12:23 +08:00
parent c7715b7ec3
commit 851eb96673
5 changed files with 99 additions and 37 deletions
@@ -0,0 +1,31 @@
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import com.alibaba.druid.pool.ha.MockDataSource;
import org.junit.After;
import org.junit.Before;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
public class BaseRandomDataSourceSelectorTest {
protected Map<String, DataSource> dataSourceMap;
protected HighAvailableDataSource dataSource;
@Before
public void setUp() {
dataSourceMap = new HashMap<String, DataSource>();
for (int i = 0; i < 10; i++) {
dataSourceMap.put(Integer.toString(i), new MockDataSource(Integer.toString(i)));
}
dataSource = new HighAvailableDataSource();
dataSource.setDataSourceMap(dataSourceMap);
}
@After
public void tearDown() {
dataSourceMap = null;
dataSource = null;
}
}
@@ -1,39 +1,13 @@
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import com.alibaba.druid.pool.ha.MockDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertTrue;
public class RandomDataSourceSelectorTest {
private Map<String, DataSource> dataSourceMap;
private HighAvailableDataSource dataSource;
@Before
public void setUp() {
dataSourceMap = new HashMap<String, DataSource>();
for (int i = 0; i < 10; i++) {
dataSourceMap.put(Integer.toString(i), new MockDataSource(Integer.toString(i)));
}
dataSource = new HighAvailableDataSource();
dataSource.setDataSourceMap(dataSourceMap);
}
@After
public void tearDown() {
dataSourceMap = null;
dataSource = null;
}
public class RandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTest {
@Test
public void testRandomGet() throws Exception {
public void testRandomGet() {
RandomDataSourceSelector selector = new RandomDataSourceSelector(dataSource);
int[] count = new int[10];
@@ -46,10 +20,4 @@ public class RandomDataSourceSelectorTest {
assertTrue(i > 0);
}
}
@Test
public void testOneDataSourceFail() {
}
}
@@ -0,0 +1,51 @@
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.pool.ha.MockDataSource;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public class StickyRandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTest {
@Test
public void testGetSameDataSourceInFiveSeconds() throws InterruptedException {
StickyRandomDataSourceSelector selector = new StickyRandomDataSourceSelector(dataSource);
int lastHashCode = selector.get().hashCode();
for (int i = 0; i < 4; i++) {
assertEquals(lastHashCode, selector.get().hashCode());
Thread.sleep(1000);
}
}
@Test
public void testGetDifferentDataSource() throws InterruptedException {
StickyRandomDataSourceSelector selector = new StickyRandomDataSourceSelector(dataSource);
selector.setExpireSeconds(1);
int lastHashCode = selector.get().hashCode();
boolean hasDifferentDataSource = false;
for (int i = 0; i < 20; i++) {
hasDifferentDataSource = selector.get().hashCode() != lastHashCode;
Thread.sleep(500);
}
assertTrue(hasDifferentDataSource);
}
@Test
public void testIgnoreForbiddenDataSource() throws InterruptedException {
MockDataSource normal = new MockDataSource("Normal");
MockDataSource forbidden = new MockDataSource("Forbidden");
dataSourceMap.put("N", normal);
dataSourceMap.put("F", forbidden);
dataSource.setDataSourceMap(dataSourceMap);
StickyRandomDataSourceSelector selector = new StickyRandomDataSourceSelector(dataSource);
selector.setExpireSeconds(1);
selector.getBlacklist().add(forbidden);
for (int i = 0; i < 20; i++) {
assertNotEquals(forbidden, selector.get());
Thread.sleep(500);
}
}
}