[test] fix test w/ closed connection pb stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 18 Jun 2010 09:07:41 +0200
branchstable
changeset 5785 c0661b963ced
parent 5784 8b2264e485bc
child 5786 7c16a4e7a592
[test] fix test w/ closed connection pb
devtools/testlib.py
web/test/unittest_session.py
--- a/devtools/testlib.py	Fri Jun 18 08:14:00 2010 +0200
+++ b/devtools/testlib.py	Fri Jun 18 09:07:41 2010 +0200
@@ -283,10 +283,8 @@
         MAILBOX[:] = [] # reset mailbox
 
     def tearDown(self):
-        try:
+        if not self.cnx._closed:
             self.cnx.rollback()
-        except ProgrammingError:
-            pass
         for cnx in self._cnxs:
             if not cnx._closed:
                 cnx.close()
@@ -337,11 +335,12 @@
 
     def restore_connection(self):
         if not self.cnx is self._orig_cnx[0]:
+            if not cnx._closed:
+                self.cnx.close()
             try:
-                self.cnx.close()
                 self._cnxs.remove(self.cnx)
-            except ProgrammingError:
-                pass # already closed
+            except ValueError:
+                pass
         self.cnx, self.websession = self._orig_cnx
 
     # db api ##################################################################
@@ -373,7 +372,7 @@
         try:
             self.cnx.rollback()
         except ProgrammingError:
-            pass
+            pass # connection closed
         finally:
             self.session.set_pool() # ensure pool still set after commit
 
--- a/web/test/unittest_session.py	Fri Jun 18 08:14:00 2010 +0200
+++ b/web/test/unittest_session.py	Fri Jun 18 09:07:41 2010 +0200
@@ -19,14 +19,18 @@
         self.assertEquals(self.websession.sessionid, self.websession.cnx.sessionid)
         # fake the repo session is expiring
         self.repo.close(sessionid)
-        # fake an incoming http query with sessionid in session cookie
-        # don't use self.request() which try to call req.set_session
-        req = self.requestcls(self.vreg)
-        websession = sm.get_session(req, sessionid)
-        self.assertEquals(len(sm._sessions), 1)
-        self.assertIs(websession, self.websession)
-        self.assertEquals(websession.sessionid, sessionid)
-        self.assertNotEquals(websession.sessionid, websession.cnx.sessionid)
+        try:
+            # fake an incoming http query with sessionid in session cookie
+            # don't use self.request() which try to call req.set_session
+            req = self.requestcls(self.vreg)
+            websession = sm.get_session(req, sessionid)
+            self.assertEquals(len(sm._sessions), 1)
+            self.assertIs(websession, self.websession)
+            self.assertEquals(websession.sessionid, sessionid)
+            self.assertNotEquals(websession.sessionid, websession.cnx.sessionid)
+        finally:
+            # avoid error in tearDown by telling this connection is closed...
+            self.cnx._closed = True
 
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main