Skip to content
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

Merged
merged 25 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bb70bcd
Started implementing connection error throws
Amertz08 Jan 10, 2018
6aea29c
updated test cases
Amertz08 Jan 10, 2018
5359bdf
Updated ConnectionError test cases
Amertz08 Jan 10, 2018
f940176
Updated test case to actually fail when decorator does not exist inst…
Amertz08 Jan 10, 2018
17a98dd
Merge branch 'master' into 102-mock-connection-error
Amertz08 Jul 7, 2018
ffb6205
Added venv to ignore file
Amertz08 Jul 7, 2018
4263daa
Added missing check_conn so tests work
Amertz08 Jul 7, 2018
2164cb6
Updated tests and added check decorator as needed
Amertz08 Jul 7, 2018
cbaba98
finished remaining tests
Amertz08 Jul 7, 2018
ef97612
flake8
Amertz08 Jul 7, 2018
269fc1c
Fixed issue with decorator not playing well with _lua_reply
Amertz08 Jul 7, 2018
49db0a3
Modified _patch_responses to take the decorator as a parameter
Amertz08 Jul 10, 2018
5dccd2a
Made connection check decorator private function. Adjusted for use th…
Amertz08 Jul 10, 2018
c1a80b3
Removed decorator from methods
Amertz08 Jul 10, 2018
fa63151
Adjusted patch_responses call for FakePubSub class
Amertz08 Jul 10, 2018
0fa4e3c
Adjusted test cases for access to connected attribute
Amertz08 Jul 10, 2018
ed56f4a
Connection error decorator should be patched to response regardless o…
Amertz08 Jul 11, 2018
dc3e59e
ConnectionError message
Amertz08 Jul 11, 2018
bfe919b
Updated README with documentation on mocking connection errors
Amertz08 Jul 11, 2018
03615de
Proper indentation in README
Amertz08 Jul 11, 2018
71e89ba
_check_conn should be patched on before decode_responses
Amertz08 Jul 11, 2018
8412e25
Spacing fix on documentation
Amertz08 Jul 11, 2018
6f301b3
Patched connection error decorator to PubSub class. Added test cases
Amertz08 Jul 26, 2018
75dced3
Connected arg after decode arg in FakePubSub
Amertz08 Jul 27, 2018
28cae30
Renamed test attribute redis to pubsub
Amertz08 Jul 27, 2018
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
4 changes: 3 additions & 1 deletion fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,11 +2231,13 @@ class FakePubSub(object):
PATTERN_MESSAGE_TYPES = ['psubscribe', 'punsubscribe']
LISTEN_DELAY = 0.1 # delay between listen loops (seconds)

def __init__(self, decode_responses=False, *args, **kwargs):
def __init__(self, connected=True, decode_responses=False, *args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather put connected after decode_responses, so that it doesn't break existing code that passes decode_responses as a positional parameter.

self.channels = {}
self.patterns = {}
self._q = Queue()
self.subscribed = False
self.connected = connected
_patch_responses(self, _check_conn)
if decode_responses:
_patch_responses(self, _make_decode_func)
self._decode_responses = decode_responses
Expand Down
37 changes: 37 additions & 0 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4403,5 +4403,42 @@ def test_hscan_iter(self):
self.redis.hscan_iter('name')


class TestPubSubConnected(unittest.TestCase):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other classes, remove this blank line.

def setUp(self):
self.redis = fakeredis.FakePubSub(connected=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather call this variable self.pubsub since it is not an instance of a Redis object.


def tearDown(self):
del self.redis

def test_basic_subscript(self):
with self.assertRaises(redis.ConnectionError):
self.redis.subscribe('logs')

def test_subscript_conn_lost(self):
self.redis.connected = True
self.redis.subscribe('logs')
self.redis.connected = False
with self.assertRaises(redis.ConnectionError):
self.redis.get_message()

def test_put_listen(self):
self.redis.connected = True
count = self.redis.put('logs', 'mymessage', 'subscribe')
self.assertEqual(count, 1, 'Message could should be 1')
self.redis.connected = False
with self.assertRaises(redis.ConnectionError):
self.redis.get_message()
self.redis.connected = True
msg = self.redis.get_message()
check = {
'type': 'subscribe',
'pattern': None,
'channel': b'logs',
'data': 'mymessage'
}
self.assertEqual(msg, check, 'Message was not published to channel')


if __name__ == '__main__':
unittest.main()