mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
test
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@695 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -474,6 +474,10 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getNameInternal() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
@@ -24,23 +24,20 @@ import com.alibaba.druid.util.ConcurrentIdentityHashMap;
|
||||
|
||||
public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBean {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSourceStatManager.class);
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSourceStatManager.class);
|
||||
|
||||
private final static DruidDataSourceStatManager instance = new DruidDataSourceStatManager();
|
||||
private final static DruidDataSourceStatManager instance = new DruidDataSourceStatManager();
|
||||
|
||||
// global instances
|
||||
private static final Object PRESENT = new Object();
|
||||
private static final ConcurrentIdentityHashMap<DruidDataSource, Object> instances = new ConcurrentIdentityHashMap<DruidDataSource, Object>();
|
||||
private static final ConcurrentIdentityHashMap<DruidDataSource, ObjectName> instances = new ConcurrentIdentityHashMap<DruidDataSource, ObjectName>();
|
||||
|
||||
private final static String MBEAN_NAME = "com.alibaba.druid:type=DruidDataSourceStat";
|
||||
private final static String MBEAN_NAME = "com.alibaba.druid:type=DruidDataSourceStat";
|
||||
|
||||
public static DruidDataSourceStatManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized static void add(DruidDataSource dataSource) {
|
||||
instances.put(dataSource, PRESENT);
|
||||
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
if (instances.size() == 0) {
|
||||
try {
|
||||
@@ -50,23 +47,42 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
int id = System.identityHashCode(dataSource);
|
||||
mbeanServer.registerMBean(dataSource, new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + id));
|
||||
} catch (JMException ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
ObjectName objectName = null;
|
||||
if (dataSource.getNameInternal() != null) {
|
||||
try {
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + dataSource.getNameInternal());
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (JMException ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (objectName == null) {
|
||||
try {
|
||||
int id = System.identityHashCode(dataSource);
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + id);
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (JMException ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
instances.put(dataSource, objectName);
|
||||
}
|
||||
|
||||
public synchronized static void remove(DruidDataSource dataSource) {
|
||||
instances.remove(dataSource);
|
||||
ObjectName objectName = instances.remove(dataSource);
|
||||
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
try {
|
||||
int id = System.identityHashCode(dataSource);
|
||||
mbeanServer.unregisterMBean(new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + id));
|
||||
} catch (JMException ex) {
|
||||
LOG.error("unregister mbean error", ex);
|
||||
|
||||
if (objectName != null) {
|
||||
try {
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
} catch (JMException ex) {
|
||||
LOG.error("unregister mbean error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (instances.size() == 0) {
|
||||
|
||||
@@ -18,6 +18,8 @@ public class DruidDataSourceFilterTest extends TestCase {
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
Assert.assertEquals(1, dataSource.getProxyFilters().size());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_filter_2() throws Exception {
|
||||
@@ -28,6 +30,8 @@ public class DruidDataSourceFilterTest extends TestCase {
|
||||
dataSource.setFilters("stat,trace");
|
||||
|
||||
Assert.assertEquals(2, dataSource.getProxyFilters().size());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_filter_3() throws Exception {
|
||||
|
||||
@@ -24,6 +24,8 @@ public class PasswordCallbackTest extends TestCase {
|
||||
|
||||
Assert.assertEquals(dataSource.getUrl(), passwordCallback.getUrl());
|
||||
Assert.assertEquals(dataSource.getConnectProperties(), passwordCallback.getProperties());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public static class TestPasswordCallback extends PasswordCallback {
|
||||
|
||||
@@ -10,7 +10,6 @@ import javax.sql.DataSource;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
@@ -20,7 +19,7 @@ import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
public class SpringFilterTest extends TestCase {
|
||||
|
||||
public void test_spring() throws Exception {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("com/alibaba/druid/pool/spring-config-1.xml");
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/alibaba/druid/pool/spring-config-1.xml");
|
||||
|
||||
DataSource dataSource = (DataSource) context.getBean("dataSource");
|
||||
Connection conn = dataSource.getConnection();
|
||||
@@ -28,6 +27,8 @@ public class SpringFilterTest extends TestCase {
|
||||
|
||||
TestFilter filter = (TestFilter) context.getBean("test-filter");
|
||||
Assert.assertEquals(1, filter.getConnectCount());
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
public static class TestFilter extends FilterAdapter {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
|
||||
public class TestDisable extends TestCase {
|
||||
@@ -32,6 +33,11 @@ public class TestDisable extends TestCase {
|
||||
dataSource.setFilters("stat");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
}
|
||||
|
||||
public void test_close() throws Exception {
|
||||
final int threadCount = 100;
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
|
||||
@@ -35,12 +35,10 @@ public class TestIdle3_Concurrent_MaxActive extends TestCase {
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
|
||||
new ObjectName("com.alibaba:type=DataSource"));
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
ManagementFactory.getPlatformMBeanServer().unregisterMBean(new ObjectName("com.alibaba:type=DataSource"));
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_idle2() throws Exception {
|
||||
@@ -80,7 +78,6 @@ public class TestIdle3_Concurrent_MaxActive extends TestCase {
|
||||
}
|
||||
// Assert.assertEquals(2, dataSource.getPoolingCount());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
private void concurrent(final int threadCount) throws Exception {
|
||||
|
||||
@@ -36,12 +36,10 @@ public class TestIdle3_Concurrent_Starvation extends TestCase {
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
|
||||
new ObjectName("com.alibaba:type=DataSource"));
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
ManagementFactory.getPlatformMBeanServer().unregisterMBean(new ObjectName("com.alibaba:type=DataSource"));
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_idle2() throws Exception {
|
||||
@@ -79,7 +77,6 @@ public class TestIdle3_Concurrent_Starvation extends TestCase {
|
||||
}
|
||||
// Assert.assertEquals(2, dataSource.getPoolingCount());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
private void concurrent(final int threadCount) throws Exception {
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -21,6 +18,7 @@ public class TestStat extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setName("com.alibaba.dragoon.monitor");
|
||||
|
||||
}
|
||||
|
||||
@@ -49,7 +47,6 @@ public class TestStat extends TestCase {
|
||||
JdbcStatManager.getInstance().getDataSourceList();
|
||||
Assert.assertEquals(1, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(DruidDataSourceStatManager.getInstance(), new ObjectName("com.alibaba.druid:type=DruidDataSourceStat"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user