Skip to content

Commit

Permalink
properly shut down pools when tests are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Feb 14, 2011
1 parent 05814dd commit 06f99f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
30 changes: 22 additions & 8 deletions master/buildbot/db/pool.py
Expand Up @@ -55,6 +55,15 @@ def _stop(self):
self.engine.dispose()
self.running = False

def shutdown(self):
"""Manually stop the pool. This is only necessary from tests, as the
pool will stop itself when the reactor stops under normal
circumstances."""
if not self._stop_evt:
return # pool is already stopped
reactor.removeSystemEventTrigger(self._stop_evt)
self._stop()

def do(self, callable, *args, **kwargs):
"""
Call CALLABLE in a thread, with a Connection as first argument.
Expand All @@ -64,9 +73,12 @@ def do(self, callable, *args, **kwargs):
"""
def thd():
conn = self.engine.contextual_connect()
rv = callable(conn, *args, **kwargs)
assert not isinstance(rv, engine.ResultProxy), \
"do not return ResultProxy objects!"
try:
rv = callable(conn, *args, **kwargs)
assert not isinstance(rv, engine.ResultProxy), \
"do not return ResultProxy objects!"
finally:
conn.close()
return rv
return threads.deferToThreadPool(reactor, self, thd)

Expand All @@ -75,8 +87,7 @@ def do_with_engine(self, callable, *args, **kwargs):
Like l{do}, but with an SQLAlchemy Engine as the first argument
"""
def thd():
conn = self.engine
rv = callable(conn, *args, **kwargs)
rv = callable(self.engine, *args, **kwargs)
assert not isinstance(rv, engine.ResultProxy), \
"do not return ResultProxy objects!"
return rv
Expand All @@ -92,9 +103,12 @@ def do_081(self, callable, *args, **kwargs):
def thd():
try:
conn = self.engine.contextual_connect()
rv = callable(conn, *args, **kwargs)
assert not isinstance(rv, engine.ResultProxy), \
"do not return ResultProxy objects!"
try:
rv = callable(conn, *args, **kwargs)
assert not isinstance(rv, engine.ResultProxy), \
"do not return ResultProxy objects!"
finally:
conn.close()
reactor.callFromThread(d.callback, rv)
except:
reactor.callFromThread(d.errback, failure.Failure())
Expand Down
1 change: 1 addition & 0 deletions master/buildbot/test/util/connector_component.py
Expand Up @@ -36,6 +36,7 @@ def setUpConnectorComponent(self, db_url, basedir='basedir'):
self.db.model = model.Model(self.db)

def tearDownConnectorComponent(self):
self.db.pool.shutdown()
# break some reference loops, just for fun
del self.db.pool
del self.db.model
Expand Down

0 comments on commit 06f99f2

Please sign in to comment.