Skip to content

Commit 48de89d

Browse files
pachewisegeorgepsarakis
authored andcommitted
Fixes celery#4106 - change GroupResult:as_tuple() to include parent (celery#4205)
* include parent in GroupResult:to_tuple() * unit test for celery#4106 * check for list/tuple before unpacking id, parent * flake8 issues * wrong method call in unit test * assert results equal in test_GroupResult_with_parent * GroupResult.__eq__ checks parents, del redundant parent=None * added test for GroupResult _eq_ * GroupResult_as_tuple unit test
1 parent bda678b commit 48de89d

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

celery/result.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ class GroupResult(ResultSet):
815815
Arguments:
816816
id (str): The id of the group.
817817
results (Sequence[AsyncResult]): List of result instances.
818+
parent (ResultBase): Parent result of this group.
818819
"""
819820

820821
#: The UUID of the group.
@@ -823,8 +824,9 @@ class GroupResult(ResultSet):
823824
#: List/iterator of results in the group
824825
results = None
825826

826-
def __init__(self, id=None, results=None, **kwargs):
827+
def __init__(self, id=None, results=None, parent=None, **kwargs):
827828
self.id = id
829+
self.parent = parent
828830
ResultSet.__init__(self, results, **kwargs)
829831

830832
def save(self, backend=None):
@@ -853,7 +855,11 @@ def __bool__(self):
853855

854856
def __eq__(self, other):
855857
if isinstance(other, GroupResult):
856-
return other.id == self.id and other.results == self.results
858+
return (
859+
other.id == self.id and
860+
other.results == self.results and
861+
other.parent == self.parent
862+
)
857863
return NotImplemented
858864

859865
def __ne__(self, other):
@@ -865,7 +871,7 @@ def __repr__(self):
865871
', '.join(r.id for r in self.results))
866872

867873
def as_tuple(self):
868-
return self.id, [r.as_tuple() for r in self.results]
874+
return (self.id, self.parent), [r.as_tuple() for r in self.results]
869875

870876
@property
871877
def children(self):
@@ -969,13 +975,15 @@ def result_from_tuple(r, app=None):
969975
Result = app.AsyncResult
970976
if not isinstance(r, ResultBase):
971977
res, nodes = r
972-
if nodes:
973-
return app.GroupResult(
974-
res, [result_from_tuple(child, app) for child in nodes],
975-
)
976-
# previously didn't include parent
977978
id, parent = res if isinstance(res, (list, tuple)) else (res, None)
978979
if parent:
979980
parent = result_from_tuple(parent, app)
981+
982+
if nodes:
983+
return app.GroupResult(
984+
id, [result_from_tuple(child, app) for child in nodes],
985+
parent=parent,
986+
)
987+
980988
return Result(id, parent=parent)
981989
return r

t/unit/tasks/test_result.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,21 @@ def test_len(self):
595595
def test_eq_other(self):
596596
assert self.ts != 1
597597

598+
def test_eq_with_parent(self):
599+
# GroupResult instances with different .parent are not equal
600+
grp_res = self.app.GroupResult(
601+
uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
602+
parent=self.app.AsyncResult(uuid())
603+
)
604+
grp_res_2 = self.app.GroupResult(grp_res.id, grp_res.results)
605+
assert grp_res != grp_res_2
606+
607+
grp_res_2.parent = self.app.AsyncResult(uuid())
608+
assert grp_res != grp_res_2
609+
610+
grp_res_2.parent = grp_res.parent
611+
assert grp_res == grp_res_2
612+
598613
@pytest.mark.usefixtures('depends_on_current_app')
599614
def test_pickleable(self):
600615
assert pickle.loads(pickle.dumps(self.ts))
@@ -892,3 +907,29 @@ def test_GroupResult(self):
892907
)
893908
assert x, result_from_tuple(x.as_tuple() == self.app)
894909
assert x, result_from_tuple(x == self.app)
910+
911+
def test_GroupResult_with_parent(self):
912+
parent = self.app.AsyncResult(uuid())
913+
result = self.app.GroupResult(
914+
uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
915+
parent
916+
)
917+
second_result = result_from_tuple(result.as_tuple(), self.app)
918+
assert second_result == result
919+
assert second_result.parent == parent
920+
921+
def test_GroupResult_as_tuple(self):
922+
parent = self.app.AsyncResult(uuid())
923+
result = self.app.GroupResult(
924+
'group-result-1',
925+
[self.app.AsyncResult('async-result-{}'.format(i))
926+
for i in range(2)],
927+
parent
928+
)
929+
(result_id, parent_id), group_results = result.as_tuple()
930+
assert result_id == result.id
931+
assert parent_id == parent.id
932+
assert isinstance(group_results, list)
933+
expected_grp_res = [(('async-result-{}'.format(i), None), None)
934+
for i in range(2)]
935+
assert group_results == expected_grp_res

0 commit comments

Comments
 (0)