Skip to content

Commit

Permalink
Chain with one task should run as expected (celery#4730)
Browse files Browse the repository at this point in the history
* bugfix: Chain with one task
* Fixes celery#4498

* add unit_test
  • Loading branch information
tothegump authored and auvipy committed May 14, 2018
1 parent d178dbb commit 3aaa753
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 3 additions & 4 deletions celery/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,9 @@ class chain(_chain):
def __new__(cls, *tasks, **kwargs):
# This forces `chain(X, Y, Z)` to work the same way as `X | Y | Z`
if not kwargs and tasks:
if len(tasks) == 1 and is_list(tasks[0]):
# ensure chain(generator_expression) works.
tasks = tasks[0]
return reduce(operator.or_, tasks)
if len(tasks) != 1 or is_list(tasks[0]):
tasks = tasks[0] if len(tasks) == 1 else tasks
return reduce(operator.or_, tasks)
return super(chain, cls).__new__(cls, *tasks, **kwargs)


Expand Down
5 changes: 5 additions & 0 deletions t/integration/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def test_simple_chain(self, manager):
c = add.s(4, 4) | add.s(8) | add.s(16)
assert c().get(timeout=TIMEOUT) == 32

@flaky
def test_single_chain(self, manager):
c = chain(add.s(3, 4))()
assert c.get(timeout=TIMEOUT) == 7

@flaky
def test_complex_chain(self, manager):
c = (
Expand Down
5 changes: 5 additions & 0 deletions t/unit/tasks/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ def test_apply(self):
assert res.parent.parent.get() == 8
assert res.parent.parent.parent is None

def test_single_expresion(self):
x = chain(self.add.s(1, 2)).apply()
assert x.get() == 3
assert x.parent is None

def test_empty_chain_returns_none(self):
assert chain(app=self.app)() is None
assert chain(app=self.app).apply_async() is None
Expand Down

0 comments on commit 3aaa753

Please sign in to comment.