commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r895606 - /commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java
Date Mon, 04 Jan 2010 11:44:59 GMT
Author: sebb
Date: Mon Jan  4 11:44:58 2010
New Revision: 895606

URL: http://svn.apache.org/viewvc?rev=895606&view=rev
Log:
Split multi-threaded tests into separate test cases
Move init of isRun from run() to ctor so main thread can reset it even if run() method is
delayed
Increase maxWait for test with holdTime = 1 to allow for JVMs that may sleep() for much longer
than requested.
Wait for threads to complete in multipleThreads() method
- all threads must be able to set the status variable
- status variable needs to be correctly published

Modified:
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java?rev=895606&r1=895605&r2=895606&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java
(original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java
Mon Jan  4 11:44:58 2010
@@ -372,8 +372,16 @@
         assertEquals(0, tds.getNumIdle("u2", "p2"));
     }
     
-    public void testMultipleThreads() throws Exception {
+    public void testMultipleThreads1() throws Exception {
+        // Override wait time in order to allow for Thread.sleep(1) sometimes taking a lot
longer on
+        // some JVMs, e.g. Windows.
+        final int defaultMaxWait = 430;
+        ((PerUserPoolDataSource) ds).setDefaultMaxWait(defaultMaxWait);
+        ((PerUserPoolDataSource) ds).setPerUserMaxWait("foo",Integer.valueOf(defaultMaxWait));
         assertTrue("Expected multiple threads to succeed with timeout=1",multipleThreads(1,
false));
+    }
+
+    public void testMultipleThreads2() throws Exception {
         assertTrue("Expected multiple threads to fail with timeout=2*maxWait",!multipleThreads(2
* (int)(getMaxWait()), true));
     }
 
@@ -409,11 +417,20 @@
         for (int i = 0; i < pts.length; i++) {
             pts[i].stop();
         }
-        long time = System.currentTimeMillis() - startTime;
         // - (pts.length*10*holdTime);
-        System.out.println("Multithread test time = " + time + " ms. Threads: "+pts.length+".
Hold time: "+holdTime);
 
-        Thread.sleep(holdTime);
+        /*
+         * Wait for all threads to terminate.
+         * This is essential to ensure that all threads have a chance to update success[0]
+         * and to ensure that the variable is published correctly.
+         */
+        for (int i = 0; i < pts.length; i++) {
+            pts[i].thread.join();
+        }
+
+        long time = System.currentTimeMillis() - startTime;
+        System.out.println("Multithread test time = " + time + " ms. Threads: "+pts.length+".
Hold time: "+holdTime
+                +". Maxwait: "+((PerUserPoolDataSource)ds).getDefaultMaxWait());
         return success[0];
     }
 
@@ -429,16 +446,18 @@
 
         private volatile String state;
 
+        private final Thread thread;
+
         protected PoolTest(ThreadGroup threadGroup, int connHoldTime) {
             this.connHoldTime = connHoldTime;
-            Thread thread =
+            isRun = true; // Must be done here so main thread is guaranteed to be able to
set it false
+            thread =
                 new Thread(threadGroup, this, "Thread+" + currentThreadCount++);
             thread.setDaemon(false);
             thread.start();
         }
 
         public void run() {
-            isRun = true;
             while (isRun) {
                 try {
                     Connection conn = null;



Mime
View raw message