Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 85260 invoked from network); 4 Jan 2010 17:04:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 4 Jan 2010 17:04:23 -0000 Received: (qmail 78143 invoked by uid 500); 4 Jan 2010 17:04:22 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 78032 invoked by uid 500); 4 Jan 2010 17:04:22 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 78023 invoked by uid 99); 4 Jan 2010 17:04:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jan 2010 17:04:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jan 2010 17:04:21 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4FA8E2388994; Mon, 4 Jan 2010 17:04:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r895702 - /commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java Date: Mon, 04 Jan 2010 17:04:01 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100104170401.4FA8E2388994@eris.apache.org> Author: sebb Date: Mon Jan 4 17:04:00 2010 New Revision: 895702 URL: http://svn.apache.org/viewvc?rev=895702&view=rev Log: Simplify PoolTest Exception handling. Check for exact number of failed threads expected. 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=895702&r1=895701&r2=895702&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 17:04:00 2010 @@ -17,20 +17,17 @@ package org.apache.commons.dbcp.datasources; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import javax.sql.DataSource; -import junit.framework.Test; -import junit.framework.TestSuite; - import org.apache.commons.dbcp.TestConnectionPool; import org.apache.commons.dbcp.TesterDriver; import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS; @@ -45,11 +42,7 @@ super(testName); } - public static Test suite() { - return new TestSuite(TestPerUserPoolDataSource.class); - } - - protected Connection getConnection() throws Exception { + protected Connection getConnection() throws SQLException { return ds.getConnection("foo","bar"); } @@ -378,64 +371,66 @@ 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)); + multipleThreads(1, false); } public void testMultipleThreads2() throws Exception { - assertTrue("Expected multiple threads to fail with timeout=2*maxWait",!multipleThreads(2 * (int)(getMaxWait()), true)); + multipleThreads(2 * (int)(getMaxWait()), true); } - private boolean multipleThreads(final int holdTime,final boolean expectError) throws Exception { + private void multipleThreads(final int holdTime,final boolean expectError) throws Exception { long startTime = System.currentTimeMillis(); - final boolean[] success = new boolean[1]; - success[0] = true; final PoolTest[] pts = new PoolTest[2 * getMaxActive()]; - ThreadGroup threadGroup = new ThreadGroup("foo") { - public void uncaughtException(Thread t, Throwable e) { - /* - for (int i = 0; i < pts.length; i++) - { - System.out.println(i + ": " + pts[i].reportState()); - } - */ - for (int i = 0; i < pts.length; i++) { - pts[i].stop(); - } - - if (!expectError) { - System.out.println(t.getName()); - e.printStackTrace(System.out); - } - success[0] = false; - } - }; - for (int i = 0; i < pts.length; i++) { - pts[i] = new PoolTest(threadGroup, holdTime); + pts[i] = new PoolTest(null, holdTime); } Thread.sleep(10L * holdTime); for (int i = 0; i < pts.length; i++) { pts[i].stop(); } - // - (pts.length*10*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. */ + int done=0; + int failed=0; for (int i = 0; i < pts.length; i++) { - pts[i].thread.join(); + final PoolTest poolTest = pts[i]; + poolTest.thread.join(); + final String state = poolTest.state; + if (DONE.equals(state)){ + done++; + } + final Throwable thrown = poolTest.thrown; + if (thrown != null) { + failed++; + if (!expectError || !(thrown instanceof SQLException)){ + System.out.println("Unexpected error: "+thrown.getMessage()); + } + } } 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]; + System.out.println("Multithread test time = " + time + + " ms. Threads: " + pts.length + + ". Hold time: " + holdTime + + ". Maxwait: " + ((PerUserPoolDataSource)ds).getDefaultMaxWait() + + ". Done: " + done + + ". Failed: " + failed + + ". expectError: " + expectError + ); + if (expectError) { + assertEquals("Expected half the threads to fail",pts.length/2,failed); + } else { + assertEquals("Did not expect any threads to fail",0,failed); + } } private static int currentThreadCount = 0; + private static final String DONE = "Done"; + private class PoolTest implements Runnable { /** * The number of milliseconds to hold onto a database connection @@ -444,13 +439,16 @@ private volatile boolean isRun; - private volatile String state; + String state; // No need to be volatile if it is read after the thread finishes - private final Thread thread; + final Thread thread; - protected PoolTest(ThreadGroup threadGroup, int connHoldTime) { + Throwable thrown; + + PoolTest(ThreadGroup threadGroup, int connHoldTime) { this.connHoldTime = connHoldTime; isRun = true; // Must be done here so main thread is guaranteed to be able to set it false + thrown = null; thread = new Thread(threadGroup, this, "Thread+" + currentThreadCount++); thread.setDaemon(false); @@ -458,11 +456,10 @@ } public void run() { - while (isRun) { - try { - Connection conn = null; + try { + while (isRun) { state = "Getting Connection"; - conn = getConnection(); + Connection conn = getConnection(); state = "Using Connection"; assertNotNull(conn); PreparedStatement stmt = @@ -477,21 +474,16 @@ rset.close(); stmt.close(); conn.close(); - } catch (RuntimeException e) { - throw e; - } catch (Exception e) { - throw (RuntimeException) new RuntimeException(e.toString()).initCause(e); } + state = DONE; + } catch (Throwable t) { + thrown = t; } } public void stop() { isRun = false; } - - public String reportState() { - return state; - } } public void testTransactionIsolationBehavior() throws Exception {