[Checkins] SVN: ZODB/trunk/src/ZODB/ Repaired connectionDebugInfo.

Jim Fulton jim at zope.com
Thu Dec 4 17:15:49 EST 2008


Log message for revision 93636:
  Repaired connectionDebugInfo.
  
  use "import time" so we can mess with time.time in tests.
  

Changed:
  U   ZODB/trunk/src/ZODB/DB.py
  U   ZODB/trunk/src/ZODB/tests/testDB.py

-=-
Modified: ZODB/trunk/src/ZODB/DB.py
===================================================================
--- ZODB/trunk/src/ZODB/DB.py	2008-12-04 22:15:45 UTC (rev 93635)
+++ ZODB/trunk/src/ZODB/DB.py	2008-12-04 22:15:49 UTC (rev 93636)
@@ -17,12 +17,14 @@
 
 import warnings
 
-import cPickle, cStringIO, sys
+import cPickle
+import cStringIO
+import sys
 import threading
-from time import time, ctime
 import logging
 import datetime
 import calendar
+import time
 
 from ZODB.broken import find_global
 from ZODB.utils import z64
@@ -109,7 +111,8 @@
 
 class ConnectionPool(AbstractConnectionPool):
 
-    def __init__(self, size, timeout=time()):
+    # XXX WTF, passing time.time() as a default?
+    def __init__(self, size, timeout=time.time()):
         super(ConnectionPool, self).__init__(size, timeout)
 
         # A stack of connections available to hand out.  This is a subset
@@ -129,7 +132,7 @@
         assert c not in self.available
         self._reduce_size(strictly_less=True)
         self.all.add(c)
-        self.available.append((time(), c))
+        self.available.append((time.time(), c))
         n = len(self.all)
         limit = self.size
         if n > limit:
@@ -148,14 +151,14 @@
         assert c in self.all
         assert c not in self.available
         self._reduce_size(strictly_less=True)
-        self.available.append((time(), c))
+        self.available.append((time.time(), c))
 
     def _reduce_size(self, strictly_less=False):
         """Throw away the oldest available connections until we're under our
         target size (strictly_less=False, the default) or no more than that
         (strictly_less=True).
         """
-        threshhold = time() - self.timeout
+        threshhold = time.time() - self.timeout
         target = self.size
         if strictly_less:
             target -= 1
@@ -210,7 +213,7 @@
         
         If a connection is no longer viable because it has timed out, it is
         garbage collected."""
-        threshhold = time() - self.timeout
+        threshhold = time.time() - self.timeout
         for t, c in list(self.available):
             if t < threshhold:
                 del self.available[t]
@@ -227,7 +230,7 @@
 
     # see the comments in ConnectionPool for method descriptions.
 
-    def __init__(self, size, timeout=time()):
+    def __init__(self, size, timeout=time.time()):
         super(KeyedConnectionPool, self).__init__(size, timeout)
         self.pools = {}
 
@@ -741,7 +744,7 @@
 
     def connectionDebugInfo(self):
         result = []
-        t = time()
+        t = time.time()
 
         def get_info(c):
             # `result`, `time` and `before` are lexically inherited.
@@ -755,13 +758,12 @@
             d = "%s (%s)" % (d, len(c._cache))
 
             result.append({
-                'opened': o and ("%s (%.2fs)" % (ctime(o), t-o)),
+                'opened': o and ("%s (%.2fs)" % (time.ctime(o), t-o)),
                 'info': d,
-                'before': before,
+                'before': c.before,
                 })
 
-        for before, pool in self._pools.items():
-            pool.map(get_info)
+        self._connectionMap(get_info)
         return result
 
     def getActivityMonitor(self):
@@ -783,7 +785,7 @@
         time if t is not specified.
         """
         if t is None:
-            t = time()
+            t = time.time()
         t -= days * 86400
         try:
             self.storage.pack(t, self.references)

Modified: ZODB/trunk/src/ZODB/tests/testDB.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/testDB.py	2008-12-04 22:15:45 UTC (rev 93635)
+++ ZODB/trunk/src/ZODB/tests/testDB.py	2008-12-04 22:15:49 UTC (rev 93636)
@@ -104,7 +104,49 @@
         >>> db.close()
     """
 
+def test_connectionDebugInfo():
+    r"""DB.connectionDebugInfo provides information about connections.
 
+    >>> import time
+    >>> now = 1228423244.5
+    >>> def faux_time():
+    ...     global now
+    ...     now += .1
+    ...     return now
+    >>> real_time = time.time
+    >>> time.time = faux_time
+
+    >>> from ZODB.tests.util import DB
+    >>> import transaction
+    >>> db = DB()
+    >>> c1 = db.open()
+    >>> c1.setDebugInfo('test info')
+    >>> c1.root()['a'] = MinPO(1)
+    >>> transaction.commit()
+    >>> c2 = db.open()
+    >>> _ = c1.root()['a']
+    >>> c2.close()
+
+    >>> c3 = db.open(before=c1.root()._p_serial)
+
+    >>> info = db.connectionDebugInfo()
+    >>> import pprint
+    >>> pprint.pprint(info, width=1)
+    [{'before': None,
+      'info': ' (0)',
+      'opened': None},
+     {'before': None,
+      'info': 'test info (2)',
+      'opened': 'Thu Dec  4 15:40:44 2008 (1.40s)'},
+     {'before': '\x03zY\xd8\xc0m9\xdd',
+      'info': ' (0)',
+      'opened': 'Thu Dec  4 15:40:45 2008 (0.30s)'}]
+
+    >>> time.time = real_time
+
+    """
+
+
 def test_suite():
     s = unittest.makeSuite(DBTests)
     s.addTest(doctest.DocTestSuite())



More information about the Checkins mailing list