From 875b78d4909d0e6ef07ccb6b327d2b12eac0384b Mon Sep 17 00:00:00 2001 From: Rajiv Bakulesh Shah Date: Fri, 4 Nov 2016 23:43:36 -0700 Subject: [PATCH] Remove context manager for containers (#20) The context manager only disconnected the Redis connection on exit. This was dangerous because we can pass in / reuse a Redis client when instantiating the container. --- pottery/base.py | 21 +---------- tests/test_context_manager.py | 65 ----------------------------------- 2 files changed, 1 insertion(+), 85 deletions(-) delete mode 100644 tests/test_context_manager.py diff --git a/pottery/base.py b/pottery/base.py index f466cf13..0d67e569 100644 --- a/pottery/base.py +++ b/pottery/base.py @@ -154,26 +154,7 @@ def clear(self): -class _ContextManaged(metaclass=abc.ABCMeta): - @abc.abstractproperty - def redis(self): - 'Redis client.' - - @abc.abstractmethod - def __del__(self): - ... - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): - self.__del__() # We can't do del self because we still need a - # referense to self for the next line... - self.redis.connection_pool.disconnect() - - - -class Base(_Common, _ContextManaged, _Clearable, Pipelined): +class Base(_Common, _Clearable, Pipelined): ... diff --git a/tests/test_context_manager.py b/tests/test_context_manager.py deleted file mode 100644 index 226a0f13..00000000 --- a/tests/test_context_manager.py +++ /dev/null @@ -1,65 +0,0 @@ -#-----------------------------------------------------------------------------# -# test_context_manager.py # -# # -# Copyright © 2015-2016, Rajiv Bakulesh Shah, original author. # -# All rights reserved. # -#-----------------------------------------------------------------------------# - - - -from redis import Redis - -from pottery import RedisCounter -from pottery import RedisDeque -from pottery import RedisDict -from pottery import RedisList -from pottery import RedisSet -from tests.base import TestCase - - - -class ContextManagerTests(TestCase): - def setUp(self): - super().setUp() - self.classes = {RedisCounter, RedisDeque, RedisDict, RedisList, RedisSet} - self.redis = Redis.from_url(self.REDIS_URL) - - def test_redis_disconnect_disconnects_from_redis(self): - num_connections = self.redis.info()['connected_clients'] - for cls in self.classes: - with self.subTest(cls=cls): - obj = cls() - assert self.redis.info()['connected_clients'] == num_connections + 1 - obj._redis.connection_pool.disconnect() - assert self.redis.info()['connected_clients'] == num_connections - - def test_context_manager_disconnects_from_redis(self): - num_connections = self.redis.info()['connected_clients'] - for cls in self.classes: - with self.subTest(cls=cls): - with cls() as obj: - assert self.redis.info()['connected_clients'] == num_connections + 1 - assert self.redis.info()['connected_clients'] == num_connections - - def test_empty_containers_dont_create_redis_keys(self): - for cls in self.classes: - with self.subTest(cls=cls): - with cls() as obj: - assert not self.redis.exists(obj.key) - assert not self.redis.exists(obj.key) - - def test_context_manager_deletes_temporary_redis_key(self): - class_args_kwargs = ( - (RedisCounter, ('gallahad',), {}), - (RedisCounter, ({'red': 4, 'blue': 2},), {}), - (RedisCounter, tuple(), {'cats': 4, 'dogs': 8}), - (RedisDict, tuple(), {'jack': 4098, 'sape': 4139}), - (RedisDict, ([('sape', 4139), ('guido', 4127), ('jack', 4098)],), {}), - (RedisList, ((1, 4, 9, 16, 25),), {}), - (RedisSet, (('apple', 'orange', 'apple', 'pear', 'orange', 'banana'),), {}), - ) - for cls, args, kwargs in class_args_kwargs: - with self.subTest(cls=cls, args=args, kwargs=kwargs): - with cls(*args, **kwargs) as obj: - assert self.redis.exists(obj.key) - assert not self.redis.exists(obj.key)