Skip to content

Commit

Permalink
Let the namespace managers ignore options they do not support
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Apr 29, 2018
1 parent 72a5a2e commit 300b736
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion beaker/ext/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, namespace, url=None, sa_opts=None, optimistic=False,
OpenResourceNamespaceManager.__init__(self, namespace)

if sa_opts is None:
sa_opts = params
sa_opts = {}

self.lock_dir = None

Expand Down
11 changes: 6 additions & 5 deletions beaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ def __init__(self, request, id=None, invalidate_corrupt=False,
if timeout and not save_accessed_time:
raise BeakerException("timeout requires save_accessed_time")
self.timeout = timeout
# We want to pass timeout param to redis backend to support expiration of keys
# In future, I believe, we can use this param for memcached and mongo as well
if self.timeout is not None and self.type == 'ext:redis':
# The backend expiration should always be a bit longer (I decied to use 2 minutes) than the

# If a timeout was provided, forward it to the backend too, so the backend
# can automatically expire entries if it's supported.
if self.timeout is not None:
# The backend expiration should always be a bit longer than the
# session expiration itself to prevent the case where the backend data expires while
# the session is being read (PR#153)
# the session is being read (PR#153). 2 Minutes seems a reasonable time.
self.namespace_args['timeout'] = self.timeout + 60 * 2

self.save_atime = save_accessed_time
Expand Down
16 changes: 16 additions & 0 deletions tests/test_managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class CacheManagerBaseTests(unittest.TestCase):
SUPPORTS_EXPIRATION = True
SUPPORTS_TIMEOUT = True
CACHE_ARGS = {}

@classmethod
Expand Down Expand Up @@ -112,6 +113,21 @@ def test_session_invalid(self):
Cookie='beaker.session.id=df7324911e246b70b5781c3c58328442; Path=/'))
assert 'current value is: 2' in res

def test_session_timeout(self):
app = TestApp(SessionMiddleware(self.simple_session_app, timeout=1, **self.CACHE_ARGS))

session = app.app._get_session()
session.save()
if self.SUPPORTS_TIMEOUT:
assert session.namespace.timeout == 121

res = app.get('/')
assert 'current value is: 1' in res
res = app.get('/')
assert 'current value is: 2' in res
res = app.get('/')
assert 'current value is: 3' in res

def test_has_key(self):
cache = Cache('test', **self.CACHE_ARGS)
o = object()
Expand Down
1 change: 1 addition & 0 deletions tests/test_managers/test_ext_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


class TestMongoDB(base.CacheManagerBaseTests):
SUPPORTS_TIMEOUT = False
CACHE_ARGS = {
'type': 'ext:mongodb',
'url': 'mongodb://localhost:27017/beaker_testdb'
Expand Down

0 comments on commit 300b736

Please sign in to comment.