diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterAfterResetTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterAfterResetTest.java new file mode 100644 index 0000000000..3726d708a4 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterAfterResetTest.java @@ -0,0 +1,86 @@ +package com.alibaba.druid.bvt.filter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.stat.JdbcSqlStat; +import com.alibaba.druid.stat.JdbcStatManager; +import com.alibaba.druid.util.Histogram; +import com.alibaba.druid.util.JdbcUtils; + +public class StatFilterAfterResetTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setFilters("stat"); + + dataSource.init(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_stat() throws Exception { + + final String sql = "SELECT 1"; + Assert.assertTrue(dataSource.isInited()); + + JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + + Assert.assertNull(sqlStat); + + { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(sql); + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.close(); + + sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + Assert.assertNotNull(sqlStat); + + Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram(); + Assert.assertEquals("first failed", 1, + histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); + + rs.close(); + + Assert.assertEquals("second failed", 1, + histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); + + stmt.close(); + + conn.close(); + + Assert.assertEquals(1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); + } + + JdbcStatManager.getInstance().reset(); + + Assert.assertFalse(sqlStat.isRemoved()); + + JdbcStatManager.getInstance().reset(); + Assert.assertTrue(sqlStat.isRemoved()); + + { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(sql); + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.close(); + conn.close(); + } + + Assert.assertNotSame(sqlStat, dataSource.getDataSourceStat().getSqlStat(sql)); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterResultSetMultiCloseTest.java b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterResultSetMultiCloseTest.java index cb612e9f6e..32778eccb9 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/StatFilterResultSetMultiCloseTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/StatFilterResultSetMultiCloseTest.java @@ -41,48 +41,27 @@ public class StatFilterResultSetMultiCloseTest extends TestCase { Assert.assertNull(sqlStat); - { - Connection conn = dataSource.getConnection(); - PreparedStatement stmt = conn.prepareStatement(sql); - ResultSet rs = stmt.executeQuery(); - rs.next(); - rs.close(); + Connection conn = dataSource.getConnection(); - sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); - Assert.assertNotNull(sqlStat); + PreparedStatement stmt = conn.prepareStatement(sql); + ResultSet rs = stmt.executeQuery(); + rs.next(); + rs.close(); - Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram(); - Assert.assertEquals("first failed", 1, - histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); + sqlStat = dataSource.getDataSourceStat().getSqlStat(sql); + Assert.assertNotNull(sqlStat); - rs.close(); + Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram(); + Assert.assertEquals("first failed", 1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); - Assert.assertEquals("second failed", 1, - histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); + rs.close(); - stmt.close(); + Assert.assertEquals("second failed", 1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); - conn.close(); + stmt.close(); - Assert.assertEquals(1, histogram.getValue(1)); - } + conn.close(); - JdbcStatManager.getInstance().reset(); - - Assert.assertFalse(sqlStat.isRemoved()); - - JdbcStatManager.getInstance().reset(); - Assert.assertTrue(sqlStat.isRemoved()); - - { - Connection conn = dataSource.getConnection(); - PreparedStatement stmt = conn.prepareStatement(sql); - ResultSet rs = stmt.executeQuery(); - rs.next(); - rs.close(); - conn.close(); - } - - Assert.assertNotSame(sqlStat, dataSource.getDataSourceStat().getSqlStat(sql)); + Assert.assertEquals(1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2)); } }