Skip to content

Update lock methods to match current redis-py #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mockredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ def ping(self):

# Transactions Functions #

def lock(self, key, timeout=0, sleep=0):
def lock(self, name, timeout=None, sleep=0.1, blocking_timeout=None,
lock_class=None, thread_local=True):
"""Emulate lock."""
return MockRedisLock(self, key, timeout, sleep)
if lock_class is None:
lock_class = MockRedisLock
return lock_class(self, name, timeout=timeout, sleep=sleep,
blocking_timeout=blocking_timeout,
thread_local=thread_local)

def pipeline(self, transaction=True, shard_hint=None):
"""Emulate a redis-python pipeline."""
Expand Down
14 changes: 12 additions & 2 deletions mockredis/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ class MockRedisLock(object):
to allow testing without a real redis server.
"""

def __init__(self, redis, name, timeout=None, sleep=0.1):
def __init__(self, redis, name, timeout=None, sleep=0.1,
blocking=True, blocking_timeout=None, thread_local=True):
"""Initialize the object."""

self.redis = redis
self.name = name
self.acquired_until = None
self.timeout = timeout
self.sleep = sleep
self.blocking = blocking
self.blocking_timeout = blocking_timeout
self.thread_local = bool(thread_local)

def acquire(self, blocking=True): # pylint: disable=R0201,W0613
"""Emulate acquire."""
Expand All @@ -23,8 +27,14 @@ def release(self): # pylint: disable=R0201

return

def extend(self, additional_time): # pylint: disable=R0201,W0613
"""Emulate extend."""

return

def __enter__(self):
return self.acquire()
self.acquire(blocking=True)
return self

def __exit__(self, exc_type, exc_value, traceback):
self.release()