Commit 78ee0abe authored by Jim Fulton's avatar Jim Fulton

Repaired connectionDebugInfo.

use "import time" so we can mess with time.time in tests.
parent 15548c4a
...@@ -17,12 +17,14 @@ $Id$""" ...@@ -17,12 +17,14 @@ $Id$"""
import warnings import warnings
import cPickle, cStringIO, sys import cPickle
import cStringIO
import sys
import threading import threading
from time import time, ctime
import logging import logging
import datetime import datetime
import calendar import calendar
import time
from ZODB.broken import find_global from ZODB.broken import find_global
from ZODB.utils import z64 from ZODB.utils import z64
...@@ -109,7 +111,8 @@ class AbstractConnectionPool(object): ...@@ -109,7 +111,8 @@ class AbstractConnectionPool(object):
class ConnectionPool(AbstractConnectionPool): 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) super(ConnectionPool, self).__init__(size, timeout)
# A stack of connections available to hand out. This is a subset # A stack of connections available to hand out. This is a subset
...@@ -129,7 +132,7 @@ class ConnectionPool(AbstractConnectionPool): ...@@ -129,7 +132,7 @@ class ConnectionPool(AbstractConnectionPool):
assert c not in self.available assert c not in self.available
self._reduce_size(strictly_less=True) self._reduce_size(strictly_less=True)
self.all.add(c) self.all.add(c)
self.available.append((time(), c)) self.available.append((time.time(), c))
n = len(self.all) n = len(self.all)
limit = self.size limit = self.size
if n > limit: if n > limit:
...@@ -148,14 +151,14 @@ class ConnectionPool(AbstractConnectionPool): ...@@ -148,14 +151,14 @@ class ConnectionPool(AbstractConnectionPool):
assert c in self.all assert c in self.all
assert c not in self.available assert c not in self.available
self._reduce_size(strictly_less=True) self._reduce_size(strictly_less=True)
self.available.append((time(), c)) self.available.append((time.time(), c))
def _reduce_size(self, strictly_less=False): def _reduce_size(self, strictly_less=False):
"""Throw away the oldest available connections until we're under our """Throw away the oldest available connections until we're under our
target size (strictly_less=False, the default) or no more than that target size (strictly_less=False, the default) or no more than that
(strictly_less=True). (strictly_less=True).
""" """
threshhold = time() - self.timeout threshhold = time.time() - self.timeout
target = self.size target = self.size
if strictly_less: if strictly_less:
target -= 1 target -= 1
...@@ -210,7 +213,7 @@ class ConnectionPool(AbstractConnectionPool): ...@@ -210,7 +213,7 @@ class ConnectionPool(AbstractConnectionPool):
If a connection is no longer viable because it has timed out, it is If a connection is no longer viable because it has timed out, it is
garbage collected.""" garbage collected."""
threshhold = time() - self.timeout threshhold = time.time() - self.timeout
for t, c in list(self.available): for t, c in list(self.available):
if t < threshhold: if t < threshhold:
del self.available[t] del self.available[t]
...@@ -227,7 +230,7 @@ class KeyedConnectionPool(AbstractConnectionPool): ...@@ -227,7 +230,7 @@ class KeyedConnectionPool(AbstractConnectionPool):
# see the comments in ConnectionPool for method descriptions. # 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) super(KeyedConnectionPool, self).__init__(size, timeout)
self.pools = {} self.pools = {}
...@@ -741,7 +744,7 @@ class DB(object): ...@@ -741,7 +744,7 @@ class DB(object):
def connectionDebugInfo(self): def connectionDebugInfo(self):
result = [] result = []
t = time() t = time.time()
def get_info(c): def get_info(c):
# `result`, `time` and `before` are lexically inherited. # `result`, `time` and `before` are lexically inherited.
...@@ -755,13 +758,12 @@ class DB(object): ...@@ -755,13 +758,12 @@ class DB(object):
d = "%s (%s)" % (d, len(c._cache)) d = "%s (%s)" % (d, len(c._cache))
result.append({ result.append({
'opened': o and ("%s (%.2fs)" % (ctime(o), t-o)), 'opened': o and ("%s (%.2fs)" % (time.ctime(o), t-o)),
'info': d, 'info': d,
'before': before, 'before': c.before,
}) })
for before, pool in self._pools.items(): self._connectionMap(get_info)
pool.map(get_info)
return result return result
def getActivityMonitor(self): def getActivityMonitor(self):
...@@ -783,7 +785,7 @@ class DB(object): ...@@ -783,7 +785,7 @@ class DB(object):
time if t is not specified. time if t is not specified.
""" """
if t is None: if t is None:
t = time() t = time.time()
t -= days * 86400 t -= days * 86400
try: try:
self.storage.pack(t, self.references) self.storage.pack(t, self.references)
......
...@@ -104,6 +104,48 @@ def test_invalidateCache(): ...@@ -104,6 +104,48 @@ def test_invalidateCache():
>>> db.close() >>> 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(): def test_suite():
s = unittest.makeSuite(DBTests) s = unittest.makeSuite(DBTests)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment