summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick White <patrick@patrickwhite.org>2018-03-15 15:37:22 -0700
committerJeff Widman <jeff@jeffwidman.com>2018-03-26 00:38:26 -0700
commit0905c47bff3cfc42382daff0d5ac81189c8ba46d (patch)
tree991b189203edbf6ac8faaeeb0a8e65aa21e59a71
parent32fd01ff7d5f0c419380233cfa0aab0c0eb11012 (diff)
downloadkazoo-0905c47bff3cfc42382daff0d5ac81189c8ba46d.tar.gz
fix(core): Correctly fire multiple callbacks
Due to the use of unbound lambdas in AsyncResult, multiple callbacks wouldn't actually get called, it would only call the final callback N times, where N is the number of registered callbacks.
-rw-r--r--kazoo/handlers/utils.py6
-rw-r--r--kazoo/tests/test_threading_handler.py16
2 files changed, 19 insertions, 3 deletions
diff --git a/kazoo/handlers/utils.py b/kazoo/handlers/utils.py
index e889f82..f18f3ac 100644
--- a/kazoo/handlers/utils.py
+++ b/kazoo/handlers/utils.py
@@ -46,7 +46,7 @@ class AsyncResult(object):
self._exception = None
for callback in self._callbacks:
self._handler.completion_queue.put(
- lambda: callback(self)
+ functools.partial(callback, self)
)
self._condition.notify_all()
@@ -56,7 +56,7 @@ class AsyncResult(object):
self._exception = exception
for callback in self._callbacks:
self._handler.completion_queue.put(
- lambda: callback(self)
+ functools.partial(callback, self)
)
self._condition.notify_all()
@@ -103,7 +103,7 @@ class AsyncResult(object):
# Are we already set? Dispatch it now
if self.ready():
self._handler.completion_queue.put(
- lambda: callback(self)
+ functools.partial(callback, self)
)
return
diff --git a/kazoo/tests/test_threading_handler.py b/kazoo/tests/test_threading_handler.py
index 2444533..a6b6f3a 100644
--- a/kazoo/tests/test_threading_handler.py
+++ b/kazoo/tests/test_threading_handler.py
@@ -376,3 +376,19 @@ class TestThreadingAsync(unittest.TestCase):
assert regular_function() == 'hello'
assert mock_handler.completion_queue.put.called
assert async_result.get() == 'hello'
+
+ def test_multiple_callbacks(self):
+ mockback1 = mock.Mock(name='mockback1')
+ mockback2 = mock.Mock(name='mockback2')
+ handler = self._makeHandler()
+ handler.start()
+
+ async_result = self._makeOne(handler)
+ async_result.rawlink(mockback1)
+ async_result.rawlink(mockback2)
+ async_result.set('howdy')
+ async_result.wait()
+ handler.stop()
+
+ mockback2.assert_called_once_with(async_result)
+ mockback1.assert_called_once_with(async_result)