Skip to content

Commit

Permalink
Test changes to stale timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Feb 4, 2015
1 parent 2f8fdba commit a9676f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions playhouse/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ def _close(self, conn, close_conn=False):
self._closed.add(key)
super(PooledDatabase, self)._close(conn)
elif key in self._in_use:
logger.debug('Returning %s to pool.', key)
ts = self._in_use[key]
del self._in_use[key]
heapq.heappush(self._connections, (ts, conn))
if self.stale_timeout and self._is_stale(ts):
logger.debug('Closing stale connection %s.', key)
self._close(conn, close_conn=True)
else:
logger.debug('Returning %s to pool.', key)
heapq.heappush(self._connections, (ts, conn))

def manual_close(self):
"""
Expand Down
10 changes: 7 additions & 3 deletions playhouse/tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ def test_stale_timeout(self):
# Create a test database with a very short stale timeout.
db = TestDB('testing', stale_timeout=.01)
self.assertEqual(db.get_conn(), 1)

# Return the connection to the pool.
db.close()
self.assertTrue(1 in db._in_use)

# Sleep long enough for the connection to be considered stale.
time.sleep(.01)

# When we close, since the conn is stale it won't be returned to
# the pool.
db.close()
self.assertEqual(db._in_use, {})
self.assertEqual(db._connections, [])

# A new connection will be returned.
self.assertEqual(db.get_conn(), 2)

Expand Down

0 comments on commit a9676f2

Please sign in to comment.