@@ -54,7 +54,14 @@ def test_single_root_task(self):
5454 self .assertEqual (thread_id , 100 )
5555
5656 def test_parent_child_chain (self ):
57- """Test _build_linear_stacks: BFS follows parent links from leaf to root."""
57+ """Test _build_linear_stacks: BFS follows parent links from leaf to root.
58+
59+ Task graph:
60+
61+ Parent (id=1)
62+ |
63+ Child (id=2)
64+ """
5865 collector = PstatsCollector (sample_interval_usec = 1000 )
5966
6067 child = MockTaskInfo (
@@ -91,10 +98,17 @@ def test_parent_child_chain(self):
9198 self .assertIn ("parent_fn" , func_names )
9299
93100 def test_multiple_leaf_tasks (self ):
94- """Test _find_leaf_tasks: identifies multiple leaves correctly."""
95- collector = PstatsCollector (sample_interval_usec = 1000 )
101+ """Test _find_leaf_tasks: identifies multiple leaves correctly.
102+
103+ Task graph (fan-out from root):
96104
97- # Two leaves, one root
105+ Root (id=1)
106+ / \
107+ Leaf1 (id=10) Leaf2 (id=20)
108+
109+ Expected: 2 stacks (one for each leaf).
110+ """
111+ collector = PstatsCollector (sample_interval_usec = 1000 )
98112 leaf1 = MockTaskInfo (
99113 task_id = 10 ,
100114 task_name = "Leaf1" ,
@@ -125,10 +139,15 @@ def test_multiple_leaf_tasks(self):
125139 self .assertEqual (leaf_ids , {10 , 20 })
126140
127141 def test_cycle_detection (self ):
128- """Test _build_linear_stacks: cycle detection prevents infinite loops."""
129- collector = PstatsCollector (sample_interval_usec = 1000 )
142+ """Test _build_linear_stacks: cycle detection prevents infinite loops.
143+
144+ Task graph (cyclic dependency):
130145
131- # A -> B -> A (cycle)
146+ A (id=1) <---> B (id=2)
147+
148+ Neither task is a leaf (both have parents), so no stacks are produced.
149+ """
150+ collector = PstatsCollector (sample_interval_usec = 1000 )
132151 task_a = MockTaskInfo (
133152 task_id = 1 ,
134153 task_name = "A" ,
@@ -265,12 +284,14 @@ def test_diamond_pattern_multiple_parents(self):
265284 """
266285 collector = PstatsCollector (sample_interval_usec = 1000 )
267286
268- # Diamond: Root spawns A and B, both await Child
269- # Root (id=1)
270- # / \
271- # A(id=2) B(id=3)
272- # \ /
273- # Child(id=4)
287+ # Diamond pattern: Root spawns A and B, both await Child
288+ #
289+ # Root (id=1)
290+ # / \
291+ # A (id=2) B (id=3)
292+ # \ /
293+ # Child (id=4)
294+ #
274295
275296 child = MockTaskInfo (
276297 task_id = 4 ,
@@ -375,11 +396,18 @@ def test_orphaned_parent_with_no_frames_collected(self):
375396 def test_frame_ordering (self ):
376397 """Test _build_linear_stacks: frames are collected in correct order (leaf->root).
377398
378- Verifies stack is built bottom-up: leaf frames first, then parent frames.
399+ Task graph (3-level chain):
400+
401+ Root (id=1) <- root_bottom, root_top
402+ |
403+ Middle (id=2) <- mid_bottom, mid_top
404+ |
405+ Leaf (id=3) <- leaf_bottom, leaf_top
406+
407+ Expected frame order: leaf_bottom, leaf_top, mid_bottom, mid_top, root_bottom, root_top
408+ (stack is built bottom-up: leaf frames first, then parent frames).
379409 """
380410 collector = PstatsCollector (sample_interval_usec = 1000 )
381-
382- # 3-level chain with distinctive frame names
383411 leaf = MockTaskInfo (
384412 task_id = 3 ,
385413 task_name = "Leaf" ,
@@ -438,23 +466,26 @@ def test_frame_ordering(self):
438466 def test_complex_multi_parent_convergence (self ):
439467 """Test _build_linear_stacks: multiple leaves with same parents pick deterministically.
440468
441- Tests that when multiple leaves have multiple parents, each leaf picks the same parent
442- (sorted, first one) and all leaves are annotated with parent count.
443- Root
444- / \\
445- A B
446- \\ /
447- LeafX LeafY
469+ Tests that when multiple leaves have multiple parents, each leaf picks the same
470+ parent (sorted, first one) and all leaves are annotated with parent count.
471+
472+ Task graph structure (both leaves awaited by both A and B):
473+
474+ Root (id=1)
475+ / \
476+ A (id=2) B (id=3)
477+ | \ / |
478+ | \ / |
479+ | \/ |
480+ | /\ |
481+ | / \ |
482+ LeafX (id=4) LeafY (id=5)
483+
484+ Expected behavior: Both leaves pick parent A (lowest id=2) for their stack path.
485+ Result: 2 stacks, both going through A -> Root (B is skipped).
448486 """
449487 collector = PstatsCollector (sample_interval_usec = 1000 )
450488
451- # More complex: 2 leaves, both with 2 parents each, converging to 1 root
452- # Root(1)
453- # / \
454- # A(2) B(3)
455- # / \ / \
456- # LeafX(4) LeafY(5)
457-
458489 leaf_x = MockTaskInfo (
459490 task_id = 4 ,
460491 task_name = "LeafX" ,
0 commit comments