Skip to content

Commit

Permalink
Merge 9771515 into ce88ec5
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi committed Apr 7, 2016
2 parents ce88ec5 + 9771515 commit 8136e71
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
26 changes: 23 additions & 3 deletions vumi/persist/redis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,28 @@ def __new__(meta, classname, bases, class_dict):
return type.__new__(meta, classname, bases, new_class_dict)


class ClientProxy(object):
def __init__(self, client):
self.client = client


class Manager(object):

__metaclass__ = CallMakerMetaclass

def __init__(self, client, config, key_prefix, key_separator=None):
def __init__(
self, client, config, key_prefix, key_separator=None,
client_proxy=None):
assert \
(client is None and client_proxy is not None) or \
(client is not None and client_proxy is None), \
'Only one of client or client_proxy may be specified'
if key_separator is None:
key_separator = ':'
self._client = client
if client_proxy is None:
self._client_proxy = ClientProxy(client)
else:
self._client_proxy = client_proxy
self._config = config
self._key_prefix = key_prefix
self._key_separator = key_separator
Expand All @@ -71,13 +85,19 @@ def __deepcopy__(self, memo):
"This is to let managers pass through config deepcopies in tests."
return self

@property
def _client(self):
return self._client_proxy.client

def get_key_prefix(self):
"""This is only intended for use in testing, not production."""
return self._key_prefix

def sub_manager(self, sub_prefix):
key_prefix = self._key(sub_prefix)
sub_man = self.__class__(self._client, self._config, key_prefix)
sub_man = self.__class__(
None, self._config, key_prefix,
client_proxy=self._client_proxy)
if isinstance(self._client, FakeRedis):
sub_man._close = self._client.teardown
return sub_man
Expand Down
10 changes: 10 additions & 0 deletions vumi/persist/tests/test_redis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ def test_no_key_prefix_sub_manager(self):
self.assertEqual(sub_manager._key_prefix, "foo")
self.assertEqual(sub_manager._client, manager._client)
self.assertEqual(sub_manager._key_separator, manager._key_separator)

def test_client_and_client_proxy_disallowed(self):
'''If both the client and the client proxy are specified when creating
a manager, then an exception should be raised.'''
e = self.assertRaises(
AssertionError, Manager, object(), None, None,
client_proxy=object())
self.assertEqual(
str(e),
'Only one of client or client_proxy may be specified')
10 changes: 1 addition & 9 deletions vumi/persist/txredis_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ class TxRedisManager(Manager):

def __init__(self, *args, **kwargs):
super(TxRedisManager, self).__init__(*args, **kwargs)
self._sub_managers = []

@classmethod
def _fake_manager(cls, fake_redis, manager_config):
Expand Down Expand Up @@ -283,15 +282,8 @@ def _make_manager(cls, client, manager_config):
cls._attach_reconnector(manager)
return manager

def sub_manager(self, sub_prefix):
sub_man = super(TxRedisManager, self).sub_manager(sub_prefix)
self._sub_managers.append(sub_man)
return sub_man

def set_client(self, client):
self._client = client
for sub_man in self._sub_managers:
sub_man.set_client(client)
self._client_proxy.client = client
return client

@staticmethod
Expand Down

0 comments on commit 8136e71

Please sign in to comment.