Обсуждение: Fix more unit test connection leaks

Поиск
Список
Период
Сортировка

Fix more unit test connection leaks

От
Mikko Tiihonen
Дата:
Hi,

I found some more connection leaks from the test cases.

Index: org/postgresql/test/jdbc2/MiscTest.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v
retrieving revision 1.20
diff -u -r1.20 MiscTest.java
--- org/postgresql/test/jdbc2/MiscTest.java    2 Dec 2005 03:05:10 -0000    1.20
+++ org/postgresql/test/jdbc2/MiscTest.java    21 Jul 2007 21:08:18 -0000
@@ -66,6 +66,7 @@
             rs.close();
             stmt.cancel();
         }
+        TestUtil.closeDB(con);
     }

     public void testError() throws Exception
@@ -130,5 +131,6 @@
         con.commit();
         TestUtil.dropTable(con, "test_lock");
         con.close();
+        con2.close();
     }
 }
Index: org/postgresql/test/jdbc2/ConnectionTest.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/ConnectionTest.java,v
retrieving revision 1.21
diff -u -r1.21 ConnectionTest.java
--- org/postgresql/test/jdbc2/ConnectionTest.java    24 Nov 2005 02:31:43 -0000    1.21
+++ org/postgresql/test/jdbc2/ConnectionTest.java    21 Jul 2007 21:08:17 -0000
@@ -22,6 +22,8 @@
 public class ConnectionTest extends TestCase
 {

+    private Connection con;
+
     /*
      * Constructor
      */
@@ -33,7 +35,7 @@
     // Set up the fixture for this testcase: the tables for this test.
     protected void setUp() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();

         TestUtil.createTable(con, "test_a", "imagename name,image oid,id int4");
         TestUtil.createTable(con, "test_c", "source text,cost money,imageid int4");
@@ -44,7 +46,9 @@
     // Tear down the fixture for this test case.
     protected void tearDown() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        TestUtil.closeDB(con);
+
+        con = TestUtil.openDB();

         TestUtil.dropTable(con, "test_a");
         TestUtil.dropTable(con, "test_c");
@@ -57,15 +61,15 @@
      */
     public void testCreateStatement() throws Exception
     {
-        Connection conn = TestUtil.openDB();
+        con = TestUtil.openDB();

         // A standard Statement
-        Statement stat = conn.createStatement();
+        Statement stat = con.createStatement();
         assertNotNull(stat);
         stat.close();

         // Ask for Updateable ResultSets
-        stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+        stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
         assertNotNull(stat);
         stat.close();
     }
@@ -75,17 +79,17 @@
      */
     public void testPrepareStatement() throws Exception
     {
-        Connection conn = TestUtil.openDB();
+        con = TestUtil.openDB();

         String sql = "select source,cost,imageid from test_c";

         // A standard Statement
-        PreparedStatement stat = conn.prepareStatement(sql);
+        PreparedStatement stat = con.prepareStatement(sql);
         assertNotNull(stat);
         stat.close();

         // Ask for Updateable ResultSets
-        stat = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+        stat = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
         assertNotNull(stat);
         stat.close();
     }
@@ -103,7 +107,7 @@
     public void testNativeSQL() throws Exception
     {
         // test a simple escape
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();
         assertEquals("DATE  '2005-01-24'",con.nativeSQL("{d '2005-01-24'}"));
     }

@@ -112,7 +116,7 @@
      */
     public void testTransactions() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();
         Statement st;
         ResultSet rs;

@@ -154,7 +158,7 @@
      */
     public void testIsClosed() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();

         // Should not say closed
         assertTrue(!con.isClosed());
@@ -170,7 +174,7 @@
      */
     public void testWarnings() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();

         String testStr = "This Is OuR TeSt message";

@@ -200,7 +204,7 @@
      */
     public void testTransactionIsolation() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();

         // PostgreSQL defaults to READ COMMITTED
         assertEquals(Connection.TRANSACTION_READ_COMMITTED,
@@ -274,7 +278,7 @@
      */
     public void testTypeMaps() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();

         // preserve the current map
         java.util.Map oldmap = con.getTypeMap();
@@ -296,7 +300,7 @@
      */
     public void testDoubleClose() throws Exception
     {
-        Connection con = TestUtil.openDB();
+        con = TestUtil.openDB();
         con.close();
         con.close();
     }
Index: org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v
retrieving revision 1.38
diff -u -r1.38 DatabaseMetaDataTest.java
--- org/postgresql/test/jdbc2/DatabaseMetaDataTest.java    15 Jul 2007 15:33:33 -0000    1.38
+++ org/postgresql/test/jdbc2/DatabaseMetaDataTest.java    21 Jul 2007 21:08:18 -0000
@@ -167,6 +167,7 @@

         TestUtil.dropTable( con1, "vv" );
         TestUtil.dropTable( con1, "ww" );
+        TestUtil.closeDB(con1);
     }

     public void testForeignKeyActions() throws Exception
@@ -192,6 +193,7 @@
         TestUtil.dropTable(conn, "fkt2");
         TestUtil.dropTable(conn, "fkt1");
         TestUtil.dropTable(conn, "pkt");
+        TestUtil.closeDB(conn);
     }

     public void testForeignKeysToUniqueIndexes() throws Exception
@@ -310,7 +312,7 @@
         TestUtil.dropTable( con1, "users" );
         TestUtil.dropTable( con1, "people" );
         TestUtil.dropTable( con1, "policy" );
-
+        TestUtil.closeDB(con1);
     }

     public void testColumns() throws SQLException



Re: Fix more unit test connection leaks

От
Kris Jurka
Дата:

On Sun, 22 Jul 2007, Mikko Tiihonen wrote:

> I found some more connection leaks from the test cases.
>

Yeah, I started looking at your original patch and figured there would be
more, so I enabled the code to report the location of any opened but
unclosed connections (in the attached hack).  Does your latest patch pass
this test?

Kris Jurka

Вложения

Re: Fix more unit test connection leaks

От
Mikko Tiihonen
Дата:
On Sat, 2007-07-21 at 21:31 -0400, Kris Jurka wrote:
>
> On Sun, 22 Jul 2007, Mikko Tiihonen wrote:
>
> > I found some more connection leaks from the test cases.
> >
>
> Yeah, I started looking at your original patch and figured there would be
> more, so I enabled the code to report the location of any opened but
> unclosed connections (in the attached hack).  Does your latest patch pass
> this test?

Unfortunately it did not. My previous try at ConnectionPoolTest leaks
were unsuccessfull due to the working connection wrapper that the
pool uses.
With this final (third) connection leak patch the unit tests no longer
complain about leaked connections.

Index: org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java,v
retrieving revision 1.15
diff -u -r1.15 ConnectionPoolTest.java
--- org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java    11 Jan 2005 08:25:48 -0000    1.15
+++ org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java    22 Jul 2007 07:56:10 -0000
@@ -14,6 +14,7 @@
 import org.postgresql.test.TestUtil;
 import javax.sql.*;
 import java.sql.*;
+import java.util.*;
 import java.io.*;

 /**
@@ -25,6 +26,8 @@
  */
 public class ConnectionPoolTest extends BaseDataSourceTest
 {
+    private ArrayList connections = new ArrayList();
+
     /**
      * Constructor required by JUnit
      */
@@ -48,6 +51,19 @@
             bds.setPassword(TestUtil.getPassword());
         }
     }
+
+    protected void tearDown() throws Exception
+    {
+        for (Iterator i = connections.iterator(); i.hasNext(); ) {
+            PooledConnection c = (PooledConnection) i.next();
+            try {
+                c.close();
+            } catch (Exception ex) {
+                // close throws nullptr or other evil things if the connection
+                // is already closed
+            }
+        }
+    }

     /**
      * Though the normal client interface is to grab a Connection, in
@@ -61,7 +77,9 @@
         // jdbc.optional.ConnectionPool because our ObjectFactory
         // returns only the top level class, not the specific
         // jdbc2/jdbc3 implementations.
-        return ((PGConnectionPoolDataSource)bds).getPooledConnection();
+        PooledConnection c = ((PGConnectionPoolDataSource)bds).getPooledConnection();
+        connections.add(c);
+        return c;
     }

     /**