Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 55808 invoked from network); 15 Jan 2010 00:05:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Jan 2010 00:05:11 -0000 Received: (qmail 23048 invoked by uid 500); 15 Jan 2010 00:05:10 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 22955 invoked by uid 500); 15 Jan 2010 00:05:10 -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 22946 invoked by uid 99); 15 Jan 2010 00:05:10 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 Jan 2010 00:05:10 +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; Fri, 15 Jan 2010 00:05:07 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4E64323889FD; Fri, 15 Jan 2010 00:04:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r899485 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java Date: Fri, 15 Jan 2010 00:04:46 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100115000446.4E64323889FD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebb Date: Fri Jan 15 00:04:45 2010 New Revision: 899485 URL: http://svn.apache.org/viewvc?rev=899485&view=rev Log: Add multi-threaded borrow/wait/return test Prompted by problems with testing DBCP on Continuum Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java?rev=899485&r1=899484&r2=899485&view=diff ============================================================================== --- commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java (original) +++ commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java Fri Jan 15 00:04:45 2010 @@ -1340,6 +1340,55 @@ } /* + * Test multi-threaded pool access. + * Multiple keys, multiple threads, but maxActive only allows half the threads to succeed. + * + * This test was prompted by Continuum build failures in the Commons DBCP test case: + * TestSharedPoolDataSource.testMultipleThreads2() + * Let's see if the this fails on Continuum too! + */ + public void testMaxWaitMultiThreaded() throws Exception { + final long maxWait = 200; // wait for connection + final long holdTime = 2 * maxWait; // how long to hold connection + final int keyCount = 4; // number of different keys + final int threadsPerKey = 5; // number of threads to grab the key initiallu + SimpleFactory factory = new SimpleFactory(); + GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory); + pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); + pool.setMaxWait(maxWait); + pool.setMaxActive(threadsPerKey); + // Create enough threads so half the threads will have to wait + WaitingTestThread wtt[] = new WaitingTestThread[keyCount * threadsPerKey * 2]; + for(int i=0; i < wtt.length; i++){ + wtt[i] = new WaitingTestThread(pool,Integer.toString(i % keyCount),holdTime); + } + long origin = System.currentTimeMillis()-1000; + for(int i=0; i < wtt.length; i++){ + wtt[i].start(); + } + int failed = 0; + for(int i=0; i < wtt.length; i++){ + wtt[i].join(); + if (wtt[i]._thrown != null){ + failed++; + } + } + if (wtt.length/2 != failed){ + for(int i=0; i < wtt.length; i++){ + WaitingTestThread wt = wtt[i]; + System.out.println("" + + " Preborrow: "+(wt.preborrow-origin) + + " Postborrow: "+(wt.postborrow != 0 ? wt.postborrow-origin : -1) + + " BorrowTime: "+(wt.postborrow != 0 ? wt.postborrow-wt.preborrow : -1) + + " PostReturn: "+(wt.postreturn != 0 ? wt.postreturn-origin : -1) + + " Ended: "+(wt.ended-origin) + + " Key: "+(wt._key) + ); + } + } + assertEquals("Expected half the threads to fail",wtt.length/2,failed); + } + /* * Very simple test thread that just tries to borrow an object from * the provided pool with the specified key and returns it */ @@ -1362,6 +1411,44 @@ } } + /* + * Very simple test thread that just tries to borrow an object from + * the provided pool with the specified key and returns it after a wait + */ + static class WaitingTestThread extends Thread { + private final KeyedObjectPool _pool; + private final String _key; + private final long _pause; + private Throwable _thrown; + + private long preborrow; // just before borrow + private long postborrow; // borrow returned + private long postreturn; // after object was returned + private long ended; + + public WaitingTestThread(KeyedObjectPool pool, String key, long pause) { + _pool = pool; + _key = key; + _pause = pause; + _thrown = null; + } + + public void run() { + try { + preborrow = System.currentTimeMillis(); + Object obj = _pool.borrowObject(_key); + postborrow = System.currentTimeMillis(); + Thread.sleep(_pause); + _pool.returnObject(_key, obj); + postreturn = System.currentTimeMillis(); + } catch (Exception e) { + _thrown = e; + } finally{ + ended = System.currentTimeMillis(); + } + } + } + static class TestThread implements Runnable { private final java.util.Random _random = new java.util.Random();