From: Mike Bayer Date: Tue, 31 Mar 2009 20:16:38 +0000 (+0000) Subject: the return of --mockpool, mocking you and your crappy code that doesn't clean up... X-Git-Tag: rel_0_6_6~243 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd5b6849dbd463336a89c6d0bf379e43daaa61ff;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git the return of --mockpool, mocking you and your crappy code that doesn't clean up after itself --- diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index c39b4b225b..f96e1215c4 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -276,7 +276,7 @@ class _ConnectionRecord(object): def _finalize_fairy(connection, connection_record, pool, ref=None): - if ref is not None and connection_record.backref is not ref: + if ref is not None and (connection_record.backref is not ref or isinstance(pool, AssertionPool)): return if connection is not None: try: @@ -753,7 +753,8 @@ class StaticPool(Pool): Pool.__init__(self, creator, **params) self._conn = creator() self.connection = _ConnectionRecord(self) - + self.connection = None + def status(self): return "StaticPool" @@ -785,68 +786,41 @@ class AssertionPool(Pool): ## TODO: modify this to handle an arbitrary connection count. - def __init__(self, creator, **params): - """ - Construct an AssertionPool. - - :param creator: a callable function that returns a DB-API - connection object. The function will be called with - parameters. - - :param recycle: If set to non -1, number of seconds between - connection recycling, which means upon checkout, if this - timeout is surpassed the connection will be closed and - replaced with a newly opened connection. Defaults to -1. - - :param echo: If True, connections being pulled and retrieved - from the pool will be logged to the standard output, as well - as pool sizing information. Echoing can also be achieved by - enabling logging for the "sqlalchemy.pool" - namespace. Defaults to False. - - :param use_threadlocal: If set to True, repeated calls to - :meth:`connect` within the same application thread will be - guaranteed to return the same connection object, if one has - already been retrieved from the pool and has not been - returned yet. Offers a slight performance advantage at the - cost of individual transactions by default. The - :meth:`unique_connection` method is provided to bypass the - threadlocal behavior installed into :meth:`connect`. - - :param reset_on_return: If true, reset the database state of - connections returned to the pool. This is typically a - ROLLBACK to release locks and transaction resources. - Disable at your own peril. Defaults to True. - - :param listeners: A list of - :class:`~sqlalchemy.interfaces.PoolListener`-like objects or - dictionaries of callables that receive events when DB-API - connections are created, checked out and checked in to the - pool. - - """ - Pool.__init__(self, creator, **params) - self.connection = _ConnectionRecord(self) - self._conn = self.connection - + def __init__(self, *args, **kw): + self._conn = None + self._checked_out = False + Pool.__init__(self, *args, **kw) + def status(self): return "AssertionPool" - def create_connection(self): - raise AssertionError("Invalid") - def do_return_conn(self, conn): - assert conn is self._conn and self.connection is None - self.connection = conn + if not self._checked_out: + raise AssertionError("connection is not checked out") + self._checked_out = False + assert conn is self._conn def do_return_invalid(self, conn): - raise AssertionError("Invalid") + self._conn = None + self._checked_out = False + + def dispose(self): + self._checked_out = False + self._conn.close() + def recreate(self): + self.log("Pool recreating") + return AssertionPool(self._creator, echo=self._should_log_info, listeners=self.listeners) + def do_get(self): - assert self.connection is not None - c = self.connection - self.connection = None - return c + if self._checked_out: + raise AssertionError("connection is already checked out") + + if not self._conn: + self._conn = self.create_connection() + + self._checked_out = True + return self._conn class _DBProxy(object): """Layers connection pooling behavior on top of a standard DB-API module. diff --git a/test/orm/merge.py b/test/orm/merge.py index ab56dd9783..790cd44950 100644 --- a/test/orm/merge.py +++ b/test/orm/merge.py @@ -1,6 +1,7 @@ import testenv; testenv.configure_for_tests() from testlib import sa, testing -from testlib.sa.util import OrderedSet +from testlib.sa import util +OrderedSet = util.OrderedSet from testlib.sa.orm import mapper, relation, create_session, PropComparator, synonym, comparable_property from testlib.testing import eq_, ne_ from orm import _base, _fixtures diff --git a/test/profiling/alltests.py b/test/profiling/alltests.py index 19401098c1..226a02a648 100644 --- a/test/profiling/alltests.py +++ b/test/profiling/alltests.py @@ -1,5 +1,5 @@ import testenv; testenv.configure_for_tests() -from testlib import sa_unittest as unittest +from testlib import sa_unittest as unittest, testing def suite(): @@ -11,7 +11,7 @@ def suite(): 'profiling.zoomark_orm', ) alltests = unittest.TestSuite() - if testenv.testlib.config.coverage_enabled: + if testenv.testlib.config.coverage_enabled or testing.jython: return alltests for name in modules_to_test: diff --git a/test/testlib/compat.py b/test/testlib/compat.py index a0226869c2..dd23560be8 100644 --- a/test/testlib/compat.py +++ b/test/testlib/compat.py @@ -1,9 +1,11 @@ import sys, types, __builtin__ -__all__ = '_function_named', 'callable' +__all__ = '_function_named', 'callable', 'py3k', 'jython' py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0) +jython = sys.platform.startswith('java') + def _function_named(fn, name): """Return a function with a given __name__. diff --git a/test/testlib/testing.py b/test/testlib/testing.py index 982eb026d4..6963fcb49e 100644 --- a/test/testlib/testing.py +++ b/test/testlib/testing.py @@ -12,7 +12,7 @@ import warnings from cStringIO import StringIO import testlib.config as config -from testlib.compat import _function_named +from testlib.compat import * from testlib.engines import drop_all_tables # Delayed imports