From 9ed11376ef1333ba8e38213f454734fe8dd83ea6 Mon Sep 17 00:00:00 2001 From: Cenk Alti Date: Fri, 7 Jun 2013 02:02:27 +0300 Subject: [PATCH] fix apply --- kuyruk/task.py | 14 ++++++++++---- kuyruk/test/tasks.py | 5 +++++ kuyruk/test/test_kuyruk.py | 6 ++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/kuyruk/task.py b/kuyruk/task.py index 6f57bed7..add2cd08 100644 --- a/kuyruk/task.py +++ b/kuyruk/task.py @@ -69,8 +69,8 @@ def __call__(self, *args, **kwargs): task_result = self.apply(*args, **kwargs) else: task_result = TaskResult(self) - task_result.id = self.send_to_queue(args, kwargs, - host=host, local=local) + task_result.id = self.send_to_queue( + args, kwargs, host=host, local=local) self.send_signal(events.task_postsend, args, kwargs) @@ -225,16 +225,22 @@ def __init__(self, task, obj): self.obj = obj def __getattr__(self, item): + """Delegates all attributes to real Task.""" return getattr(self.task, item) def __call__(self, *args, **kwargs): + # Insert the bound object as a first argument to __call__ args = list(args) args.insert(0, self.obj) return super(BoundTask, self).__call__(*args, **kwargs) def apply(self, *args, **kwargs): - args = list(args) - args.insert(0, self.obj) + # apply() may be called directly. Insert the bound object only if + # it is not inserted by __call__() + if args and args[0] is not self.obj: + args = list(args) + args.insert(0, self.obj) + return super(BoundTask, self).apply(*args, **kwargs) diff --git a/kuyruk/test/tasks.py b/kuyruk/test/tasks.py index 039d986e..1e133d0e 100644 --- a/kuyruk/test/tasks.py +++ b/kuyruk/test/tasks.py @@ -117,6 +117,11 @@ def meow(self, message): print "Felix says:", message must_be_called() + @kuyruk.task(eager=True) + def meow_eager(self, message): + print "Felix says:", message + must_be_called() + @kuyruk.task def raise_exception(self): raise Exception diff --git a/kuyruk/test/test_kuyruk.py b/kuyruk/test/test_kuyruk.py index 45439113..8003b030 100644 --- a/kuyruk/test/test_kuyruk.py +++ b/kuyruk/test/test_kuyruk.py @@ -180,6 +180,12 @@ def test_class_task(self): @patch('kuyruk.test.tasks.must_be_called') def test_class_task_eager(self, mock_func): + cat = tasks.Cat(1, 'Felix') + cat.meow_eager('Oh my god') + mock_func.assert_called_once_with() + + @patch('kuyruk.test.tasks.must_be_called') + def test_class_task_apply(self, mock_func): cat = tasks.Cat(1, 'Felix') cat.meow.apply('Oh my god') mock_func.assert_called_once_with()