-
Notifications
You must be signed in to change notification settings - Fork 183
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
Mock connection error #197
Changes from 16 commits
bb70bcd
6aea29c
5359bdf
f940176
17a98dd
ffb6205
4263daa
2164cb6
cbaba98
ef97612
269fc1c
49db0a3
5dccd2a
c1a80b3
fa63151
0fa4e3c
ed56f4a
dc3e59e
bfe919b
03615de
71e89ba
8412e25
6f301b3
75dced3
28cae30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ dump.rdb | |
extras/* | ||
.tox | ||
*.pyc | ||
.idea | ||
venv/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,12 +221,12 @@ def decode_response(*args, **kwargs): | |
return decode_response | ||
|
||
|
||
def _patch_responses(obj): | ||
def _patch_responses(obj, decorator): | ||
for attr_name in dir(obj): | ||
attr = getattr(obj, attr_name) | ||
if not callable(attr) or attr_name.startswith('_'): | ||
continue | ||
func = _make_decode_func(attr) | ||
func = decorator(attr) | ||
setattr(obj, attr_name, func) | ||
|
||
|
||
|
@@ -331,6 +331,16 @@ def release(self): | |
self.redis.delete(self.name) | ||
|
||
|
||
def _check_conn(func): | ||
"""Used to mock connection errors""" | ||
@functools.wraps(func) | ||
def func_wrapper(*args, **kwargs): | ||
if not func.__self__.connected: | ||
raise redis.ConnectionError | ||
return func(*args, **kwargs) | ||
return func_wrapper | ||
|
||
|
||
class FakeStrictRedis(object): | ||
@classmethod | ||
def from_url(cls, url, db=None, **kwargs): | ||
|
@@ -343,7 +353,7 @@ def from_url(cls, url, db=None, **kwargs): | |
return cls(db=db, **kwargs) | ||
|
||
def __init__(self, db=0, charset='utf-8', errors='strict', | ||
decode_responses=False, singleton=True, **kwargs): | ||
decode_responses=False, singleton=True, connected=True, **kwargs): | ||
if singleton: | ||
self._dbs = DATABASES | ||
else: | ||
|
@@ -356,8 +366,12 @@ def __init__(self, db=0, charset='utf-8', errors='strict', | |
self._encoding_errors = errors | ||
self._pubsubs = [] | ||
self._decode_responses = decode_responses | ||
self.connected = connected | ||
if decode_responses: | ||
_patch_responses(self) | ||
_patch_responses(self, _make_decode_func) | ||
|
||
if not connected: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be done unconditionally? Otherwise you get the odd behaviour that you can toggle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I believe you are correct. I'll implement that change. |
||
_patch_responses(self, _check_conn) | ||
|
||
@_lua_reply(_lua_bool_ok) | ||
def flushdb(self): | ||
|
@@ -2224,7 +2238,7 @@ def __init__(self, decode_responses=False, *args, **kwargs): | |
self._q = Queue() | ||
self.subscribed = False | ||
if decode_responses: | ||
_patch_responses(self) | ||
_patch_responses(self, _make_decode_func) | ||
self._decode_responses = decode_responses | ||
self.ignore_subscribe_messages = kwargs.get( | ||
'ignore_subscribe_messages', False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also pass a message with the ConnectionError e.g. "fakeredis is emulating a connection error".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. Kinda been thinking that needed a message.