Skip to content

Commit 7f89f6a

Browse files
authored
Merge pull request #30 from evhen14/fix-composable-task-29
Make when_all and when_any tasks composable #29
2 parents 4d19ae8 + bd3fdb2 commit 7f89f6a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

durabletask/task.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ def on_child_completed(self, task: Task[T]):
322322
# The order of the result MUST match the order of the tasks provided to the constructor.
323323
self._result = [task.get_result() for task in self._tasks]
324324
self._is_complete = True
325+
if self._parent is not None:
326+
self._parent.on_child_completed(self)
325327

326328
def get_completed_tasks(self) -> int:
327329
return self._completed_tasks
@@ -423,6 +425,8 @@ def on_child_completed(self, task: Task):
423425
if not self.is_complete:
424426
self._is_complete = True
425427
self._result = task
428+
if self._parent is not None:
429+
self._parent.on_child_completed(self)
426430

427431

428432
def when_all(tasks: list[Task[T]]) -> WhenAllTask[T]:

tests/durabletask/test_task.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,51 @@ def test_when_all_happy_path_returns_ordered_results_and_completes_last():
4646
assert all_task.get_result() == ["one", "two", "three"]
4747

4848

49+
def test_when_all_is_composable_with_when_any():
50+
c1 = task.CompletableTask()
51+
c2 = task.CompletableTask()
52+
53+
any_task = task.when_any([c1, c2])
54+
all_task = task.when_all([any_task])
55+
56+
assert not any_task.is_complete
57+
assert not all_task.is_complete
58+
59+
c2.complete("two")
60+
61+
assert any_task.is_complete
62+
assert all_task.is_complete
63+
assert all_task.get_result() == [c2]
64+
65+
66+
def test_when_any_is_composable_with_when_all():
67+
c1 = task.CompletableTask()
68+
c2 = task.CompletableTask()
69+
c3 = task.CompletableTask()
70+
71+
all_task1 = task.when_all([c1, c2])
72+
all_task2 = task.when_all([c3])
73+
any_task = task.when_any([all_task1, all_task2])
74+
75+
assert not any_task.is_complete
76+
assert not all_task1.is_complete
77+
assert not all_task2.is_complete
78+
79+
c1.complete("one")
80+
81+
assert not any_task.is_complete
82+
assert not all_task1.is_complete
83+
assert not all_task2.is_complete
84+
85+
c2.complete("two")
86+
87+
assert any_task.is_complete
88+
assert all_task1.is_complete
89+
assert not all_task2.is_complete
90+
91+
assert any_task.get_result() == all_task1
92+
93+
4994
def test_when_any_happy_path_returns_winner_task_and_completes_on_first():
5095
a = task.CompletableTask()
5196
b = task.CompletableTask()

0 commit comments

Comments
 (0)