Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 30550 invoked from network); 30 May 2009 15:51:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 30 May 2009 15:51:48 -0000 Received: (qmail 56819 invoked by uid 500); 30 May 2009 15:52:00 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 56738 invoked by uid 500); 30 May 2009 15:52:00 -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 56729 invoked by uid 99); 30 May 2009 15:52:00 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 30 May 2009 15:52:00 +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; Sat, 30 May 2009 15:51:58 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 07E59238888F; Sat, 30 May 2009 15:51:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r780290 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java Date: Sat, 30 May 2009 15:51:37 -0000 To: commits@commons.apache.org From: psteitz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090530155138.07E59238888F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: psteitz Date: Sat May 30 15:51:37 2009 New Revision: 780290 URL: http://svn.apache.org/viewvc?rev=780290&view=rev Log: Added test for PoolUtils Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java?rev=780290&r1=780289&r2=780290&view=diff ============================================================================== --- commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java (original) +++ commons/proper/pool/trunk/src/test/org/apache/commons/pool/TestPoolUtils.java Sat May 30 15:51:37 2009 @@ -765,6 +765,84 @@ expectedMethods.add("invalidateObject"); assertEquals(expectedMethods, calledMethods); } + + public void testErodingPerKeyKeyedObjectPool() throws Exception { + try { + PoolUtils.erodingPool((KeyedObjectPool)null, 1, true); + fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool."); + } catch(IllegalArgumentException iae) { + // expected + } + + try { + PoolUtils.erodingPool((KeyedObjectPool)null, 0, true); + fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor."); + } catch(IllegalArgumentException iae) { + // expected + } + + try { + PoolUtils.erodingPool((KeyedObjectPool)null, 1f, true); + fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool."); + } catch(IllegalArgumentException iae) { + // expected + } + + final List calledMethods = new ArrayList(); + final InvocationHandler handler = new MethodCallLogger(calledMethods) { + public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { + Object o = super.invoke(proxy, method, args); + if (o instanceof Integer) { + // so getNumActive/getNumIdle are not zero. + o = new Integer(1); + } + return o; + } + }; + + // If the logic behind PoolUtils.erodingPool changes then this will need to be tweaked. + float factor = 0.01f; // about ~9 seconds until first discard + final KeyedObjectPool pool = PoolUtils.erodingPool((KeyedObjectPool)createProxy(KeyedObjectPool.class, handler), factor, true); + + final List expectedMethods = new ArrayList(); + assertEquals(expectedMethods, calledMethods); + + final Object key = "key"; + + Object o = pool.borrowObject(key); + expectedMethods.add("borrowObject"); + + assertEquals(expectedMethods, calledMethods); + + pool.returnObject(key, o); + expectedMethods.add("returnObject"); + assertEquals(expectedMethods, calledMethods); + + for (int i=0; i < 5; i ++) { + o = pool.borrowObject(key); + expectedMethods.add("borrowObject"); + + Thread.sleep(50); + + pool.returnObject(key, o); + expectedMethods.add("returnObject"); + + assertEquals(expectedMethods, calledMethods); + + expectedMethods.clear(); + calledMethods.clear(); + } + + Thread.sleep(10000); // 10 seconds + + + o = pool.borrowObject(key); + expectedMethods.add("borrowObject"); + pool.returnObject(key, o); + expectedMethods.add("getNumIdle"); + expectedMethods.add("invalidateObject"); + assertEquals(expectedMethods, calledMethods); + } private static List invokeEveryMethod(ObjectPool op) throws Exception { op.addObject();