Skip to content

Commit

Permalink
refactor: Test various handlers on TreeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyseek committed Nov 16, 2018
1 parent a601b55 commit a4c566c
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions kazoo/tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
import gc
import importlib
import logging
import uuid

from mock import patch, call, Mock
from nose.tools import eq_, ok_, assert_not_equal, raises
from objgraph import count as count_refs_by_type

from kazoo.testing import KazooTestCase
from kazoo.testing import KazooTestHarness
from kazoo.exceptions import KazooException
from kazoo.recipe.cache import TreeCache, TreeNode, TreeEvent


class KazooTreeCacheTests(KazooTestCase):
logger = logging.getLogger(__name__)


class KazooAdaptiveHandlerTestCase(KazooTestHarness):
HANDLERS = (
('kazoo.handlers.gevent', 'SequentialGeventHandler'),
('kazoo.handlers.eventlet', 'SequentialEventletHandler'),
('kazoo.handlers.threading', 'SequentialThreadingHandler'),
)

def setUp(self):
self.handler = self.choose_an_installed_handler()
self.setup_zookeeper(handler=self.handler)

def tearDown(self):
self.handler = None
self.teardown_zookeeper()

def choose_an_installed_handler(self):
for handler_module, handler_class in self.HANDLERS:
try:
mod = importlib.import_module(handler_module)
cls = getattr(mod, handler_class)
except ImportError:
continue
except Exception:
logger.warning(
'The handler module %s is imported but could not be used',
handler_module, exc_info=1)
continue
else:
return cls()
raise ImportError('No available handler')


class KazooTreeCacheTests(KazooAdaptiveHandlerTestCase):
def setUp(self):
super(KazooTreeCacheTests, self).setUp()
self._event_queue = self.client.handler.queue_impl()
Expand All @@ -20,7 +56,6 @@ def setUp(self):
self.cache = None

def tearDown(self):
super(KazooTreeCacheTests, self).tearDown()
if not self._error_queue.empty():
try:
raise self._error_queue.get()
Expand All @@ -29,6 +64,7 @@ def tearDown(self):
if self.cache is not None:
self.cache.close()
self.cache = None
super(KazooTreeCacheTests, self).tearDown()

def make_cache(self):
if self.cache is None:
Expand Down Expand Up @@ -57,7 +93,11 @@ def spy_client(self, method_name):
return patch.object(self.client, method_name, wraps=method)

def _gc_wait(self):
while not self.client.handler.completion_queue.empty():
if self.handler.name == 'sequential_threading_handler':
while not self.client.handler.completion_queue.empty():
self.client.handler.sleep_func(0.1)
else:
# Let hub of gevent/eventlet be in a short-time loop
self.client.handler.sleep_func(0.1)
for gen in range(3):
gc.collect(gen)
Expand Down

0 comments on commit a4c566c

Please sign in to comment.