commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1531382 - in /commons/proper/pool/trunk/src: main/java/org/apache/commons/pool2/impl/GenericObjectPool.java test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
Date Fri, 11 Oct 2013 18:27:31 GMT
Author: markt
Date: Fri Oct 11 18:27:31 2013
New Revision: 1531382

URL: http://svn.apache.org/r1531382
Log:
POOL-211
Add a test case for logging the last used stack trace and fix a bug it highlighted.

Modified:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1531382&r1=1531381&r2=1531382&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
Fri Oct 11 18:27:31 2013
@@ -318,6 +318,7 @@ public class GenericObjectPool<T> extend
             this.abandonedConfig.setRemoveAbandonedOnBorrow(abandonedConfig.getRemoveAbandonedOnBorrow());
             this.abandonedConfig.setRemoveAbandonedOnMaintenance(abandonedConfig.getRemoveAbandonedOnMaintenance());
             this.abandonedConfig.setRemoveAbandonedTimeout(abandonedConfig.getRemoveAbandonedTimeout());
+            this.abandonedConfig.setUseUsageTracking(abandonedConfig.getUseUsageTracking());
         }
     }
 

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java?rev=1531382&r1=1531381&r2=1531382&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
(original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
Fri Oct 11 18:27:31 2013
@@ -18,11 +18,17 @@ package org.apache.commons.pool2.proxy;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
 
 import org.apache.commons.pool2.BasePooledObjectFactory;
 import org.apache.commons.pool2.ObjectPool;
 import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.AbandonedConfig;
 import org.apache.commons.pool2.impl.GenericObjectPool;
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -30,13 +36,30 @@ import org.junit.Test;
 public class TestProxiedObjectPoolWithJdkProxy {
 
     private static final String DATA1 = "data1";
+    private static final int ABANDONED_TIMEOUT_SECS = 3;
 
     private ObjectPool<TestObject> pool;
+    private StringWriter log;
 
     @Before
     public void setup() {
+        log = new StringWriter();
+
+        PrintWriter pw = new PrintWriter(log);
+        AbandonedConfig abandonedConfig = new AbandonedConfig();
+        abandonedConfig.setLogAbandoned(true);
+        abandonedConfig.setRemoveAbandonedOnBorrow(true);
+        abandonedConfig.setUseUsageTracking(true);
+        abandonedConfig.setRemoveAbandonedTimeout(ABANDONED_TIMEOUT_SECS);
+        abandonedConfig.setLogWriter(pw);
+
+        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
+        config.setMaxTotal(3);
+
         PooledObjectFactory<TestObject> factory = new TestObjectFactory();
-        ObjectPool<TestObject> innerPool = new GenericObjectPool<TestObject>(factory);
+
+        ObjectPool<TestObject> innerPool =
+                new GenericObjectPool<TestObject>(factory, config, abandonedConfig);
 
         ProxySource<TestObject> proxySource = new JdkProxySource<TestObject>(
                 this.getClass().getClassLoader(), new Class<?>[] { TestObject.class
});
@@ -74,6 +97,26 @@ public class TestProxiedObjectPoolWithJd
     }
 
 
+    @Test
+    public void testUsageTracking() throws Exception {
+        TestObject obj = pool.borrowObject();
+        assertNotNull(obj);
+
+        // Use the object to trigger collection of last used stack trace
+        obj.setData(DATA1);
+
+        // Sleep long enough for the object to be considered abandoned
+        Thread.sleep((ABANDONED_TIMEOUT_SECS + 2) * 1000);
+
+        // Borrow another object to trigger the abandoned object processing
+        pool.borrowObject();
+
+        String logOutput = log.getBuffer().toString();
+
+        assertTrue(logOutput.contains("Pooled object created"));
+        assertTrue(logOutput.contains("The last code to use this object was"));
+    }
+
     private static class TestObjectFactory extends
             BasePooledObjectFactory<TestObject> {
 



Mime
View raw message