update test cases
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 24 Jul 2009 15:25:17 +0000 (15:25 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 24 Jul 2009 15:25:17 +0000 (15:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@797529 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/AbandonPercentageTest.java
modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/BorrowWaitTest.java
modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/DefaultTestCase.java
modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConcurrency.java

index a0437f6..78ef7c0 100644 (file)
@@ -38,7 +38,6 @@ public class AbandonPercentageTest extends DefaultTestCase {
         this.datasource.getPoolProperties().setRemoveAbandoned(true);
         this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
         Connection con = datasource.getConnection();
-        long start = System.currentTimeMillis();
         assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
         Thread.sleep(2000);
         assertEquals("Number of connections active/busy should be 0",0,datasource.getPool().getActive());
@@ -56,7 +55,6 @@ public class AbandonPercentageTest extends DefaultTestCase {
         this.datasource.getPoolProperties().setRemoveAbandoned(true);
         this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
         Connection con = datasource.getConnection();
-        long start = System.currentTimeMillis();
         assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
         Thread.sleep(2000);
         assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
@@ -75,7 +73,6 @@ public class AbandonPercentageTest extends DefaultTestCase {
         this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
         this.datasource.getPoolProperties().setJdbcInterceptors(ResetAbandonedTimer.class.getName());
         Connection con = datasource.getConnection();
-        long start = System.currentTimeMillis();
         assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
         for (int i=0; i<20; i++) {
             Thread.sleep(200);
@@ -97,7 +94,6 @@ public class AbandonPercentageTest extends DefaultTestCase {
         this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
         Connection[] con = new Connection[size];
         con[0] = datasource.getConnection();
-        long start = System.currentTimeMillis();
         assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
         for (int i=1; i<25; i++) {
             con[i] = datasource.getConnection();
index a68d04d..979f6e9 100644 (file)
@@ -32,10 +32,10 @@ public class BorrowWaitTest extends DefaultTestCase {
         this.datasource.setMaxActive(1);
         this.datasource.setMaxWait(wait);
         Connection con = datasource.getConnection();
-        long start = System.currentTimeMillis();
         try {
             Connection con2 = datasource.getConnection();
             assertFalse("This should not happen, connection should be unavailable.",true);
+            con2.close();
         }catch (SQLException x) {
             long delta = System.currentTimeMillis();
             boolean inrange = Math.abs(wait-delta) < 1000;
index 47a85f9..4752692 100644 (file)
@@ -52,6 +52,7 @@ public class DefaultTestCase extends TestCase {
     public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
         org.apache.tomcat.jdbc.pool.DataSource datasource = null;
         PoolConfiguration p = new DefaultProperties();
+        p.setFairQueue(false);
         p.setJmxEnabled(false);
         p.setTestWhileIdle(false);
         p.setTestOnBorrow(false);
@@ -77,9 +78,7 @@ public class DefaultTestCase extends TestCase {
 
     protected void transferProperties() {
         try {
-            BasicDataSourceFactory factory = new BasicDataSourceFactory();
             Properties p = new Properties();
-
             for (int i=0; i< ALL_PROPERTIES.length; i++) {
                 String name = "get" + Character.toUpperCase(ALL_PROPERTIES[i].charAt(0)) + ALL_PROPERTIES[i].substring(1);
                 String bname = "is" + name.substring(3);
index d9b2bb2..a39f3ee 100644 (file)
@@ -31,11 +31,13 @@ public class TestConcurrency extends DefaultTestCase {
         ds.getPoolProperties().setTestWhileIdle(true);
         ds.getPoolProperties().setMinEvictableIdleTimeMillis(750);
         ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(25);
+        ds.setFairQueue(true);
     }
 
     @Override
     protected void tearDown() throws Exception {
         Driver.reset();
+        ds.close(true);
         super.tearDown();
     }
     
@@ -92,8 +94,8 @@ public class TestConcurrency extends DefaultTestCase {
     public void testBrutal() throws Exception {
         ds.getPoolProperties().setRemoveAbandoned(false);
         ds.getPoolProperties().setRemoveAbandonedTimeout(1);
-        ds.getPoolProperties().setMinEvictableIdleTimeMillis(10);
-        ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(-1);
+        ds.getPoolProperties().setMinEvictableIdleTimeMillis(100);
+        ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(10);
         ds.getConnection().close();
         final int iter = 100000 * 10;
         final AtomicInteger loopcount = new AtomicInteger(0);
@@ -119,7 +121,7 @@ public class TestConcurrency extends DefaultTestCase {
         }
         try {
             while (loopcount.get()<iter) {
-                //assertEquals("Size comparison:",10, ds.getPool().getSize());
+                assertTrue("Size comparison(less than 11):",ds.getPool().getSize()<=10);
                 ds.getPool().testAllIdle();
                 ds.getPool().checkAbandoned();
                 ds.getPool().checkIdle();
@@ -137,8 +139,56 @@ public class TestConcurrency extends DefaultTestCase {
         assertEquals("Idle comparison:",10, ds.getPool().getIdle());
         assertEquals("Used comparison:",0, ds.getPool().getActive());
         assertEquals("Connect count",10,Driver.connectCount.get());
-            
     }
 
+    public void testBrutalNonFair() throws Exception {
+        ds.getPoolProperties().setRemoveAbandoned(false);
+        ds.getPoolProperties().setRemoveAbandonedTimeout(1);
+        ds.getPoolProperties().setMinEvictableIdleTimeMillis(100);
+        ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(10);
+        ds.getConnection().close();
+        final int iter = 100000 * 10;
+        final AtomicInteger loopcount = new AtomicInteger(0);
+        final Runnable run = new Runnable() {
+            public void run() {
+                try {
+                    while (loopcount.incrementAndGet() < iter) {
+                        Connection con = ds.getConnection();
+                        con.close();
+                    }
+                }catch (Exception x) {
+                    loopcount.set(iter); //stops the test
+                    x.printStackTrace();
+                }
+            }
+        };
+        Thread[] threads = new Thread[20];
+        for (int i=0; i<threads.length; i++) {
+            threads[i] = new Thread(run);
+        }
+        for (int i=0; i<threads.length; i++) {
+            threads[i].start();
+        }
+        try {
+            while (loopcount.get()<iter) {
+                assertTrue("Size comparison(less than 11):",ds.getPool().getSize()<=10);
+                ds.getPool().testAllIdle();
+                ds.getPool().checkAbandoned();
+                ds.getPool().checkIdle();
+            }
+        }catch (Exception x) {
+            loopcount.set(iter); //stops the test
+            x.printStackTrace();
+        }
+        for (int i=0; i<threads.length; i++) {
+            threads[i].join();
+        }
+        System.out.println("Connect count:"+Driver.connectCount.get());
+        System.out.println("DisConnect count:"+Driver.disconnectCount.get());
+        assertEquals("Size comparison:",10, ds.getPool().getSize());
+        assertEquals("Idle comparison:",10, ds.getPool().getIdle());
+        assertEquals("Used comparison:",0, ds.getPool().getActive());
+        assertEquals("Connect count",10,Driver.connectCount.get());
+    }
     
 }