Skip to content

Commit 0905c47

Browse files
packysaucejeffwidman
authored andcommitted
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.
1 parent 32fd01f commit 0905c47

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

kazoo/handlers/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def set(self, value=None):
4646
self._exception = None
4747
for callback in self._callbacks:
4848
self._handler.completion_queue.put(
49-
lambda: callback(self)
49+
functools.partial(callback, self)
5050
)
5151
self._condition.notify_all()
5252

@@ -56,7 +56,7 @@ def set_exception(self, exception):
5656
self._exception = exception
5757
for callback in self._callbacks:
5858
self._handler.completion_queue.put(
59-
lambda: callback(self)
59+
functools.partial(callback, self)
6060
)
6161
self._condition.notify_all()
6262

@@ -103,7 +103,7 @@ def rawlink(self, callback):
103103
# Are we already set? Dispatch it now
104104
if self.ready():
105105
self._handler.completion_queue.put(
106-
lambda: callback(self)
106+
functools.partial(callback, self)
107107
)
108108
return
109109

kazoo/tests/test_threading_handler.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,19 @@ def regular_function():
376376
assert regular_function() == 'hello'
377377
assert mock_handler.completion_queue.put.called
378378
assert async_result.get() == 'hello'
379+
380+
def test_multiple_callbacks(self):
381+
mockback1 = mock.Mock(name='mockback1')
382+
mockback2 = mock.Mock(name='mockback2')
383+
handler = self._makeHandler()
384+
handler.start()
385+
386+
async_result = self._makeOne(handler)
387+
async_result.rawlink(mockback1)
388+
async_result.rawlink(mockback2)
389+
async_result.set('howdy')
390+
async_result.wait()
391+
handler.stop()
392+
393+
mockback2.assert_called_once_with(async_result)
394+
mockback1.assert_called_once_with(async_result)

0 commit comments

Comments
 (0)