Skip to content

Commit 51e689e

Browse files
committed
test fixes
1 parent 65aee3c commit 51e689e

File tree

1 file changed

+195
-32
lines changed

1 file changed

+195
-32
lines changed

Lib/test/test_asyncio/test_tools.py

Lines changed: 195 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
# mock output of get_all_awaited_by function.
9-
TEST_INPUTS = [
9+
TEST_INPUTS_TREE = [
1010
[
1111
# test case containing a task called timer being awaited in two
1212
# different subtasks part of a TaskGroup (root1 and root2) which call
@@ -80,7 +80,6 @@
8080
),
8181
(0, []),
8282
),
83-
([]),
8483
(
8584
[
8685
[
@@ -121,6 +120,184 @@
121120
]
122121
]
123122
),
123+
],
124+
[
125+
# test case containing two roots
126+
(
127+
(
128+
9,
129+
[
130+
(5, "Task-5", []),
131+
(6, "Task-6", [[["main2"], 5]]),
132+
(7, "Task-7", [[["main2"], 5]]),
133+
(8, "Task-8", [[["main2"], 5]]),
134+
],
135+
),
136+
(
137+
10,
138+
[
139+
(1, "Task-1", []),
140+
(2, "Task-2", [[["main"], 1]]),
141+
(3, "Task-3", [[["main"], 1]]),
142+
(4, "Task-4", [[["main"], 1]]),
143+
],
144+
),
145+
(11, []),
146+
(0, []),
147+
),
148+
(
149+
[
150+
[
151+
"└── (T) Task-5",
152+
" └── main2",
153+
" ├── (T) Task-6",
154+
" ├── (T) Task-7",
155+
" └── (T) Task-8",
156+
],
157+
[
158+
"└── (T) Task-1",
159+
" └── main",
160+
" ├── (T) Task-2",
161+
" ├── (T) Task-3",
162+
" └── (T) Task-4",
163+
],
164+
]
165+
),
166+
],
167+
]
168+
169+
TEST_INPUTS_CYCLES_TREE = [
170+
[
171+
# this test case contains a cycle: two tasks awaiting each other.
172+
(
173+
[
174+
(
175+
1,
176+
[
177+
(2, "Task-1", []),
178+
(
179+
3,
180+
"a",
181+
[[["awaiter2"], 4], [["main"], 2]],
182+
),
183+
(4, "b", [[["awaiter"], 3]]),
184+
],
185+
),
186+
(0, []),
187+
]
188+
),
189+
([[4, 3, 4]]),
190+
],
191+
[
192+
# this test case contains two cycles
193+
(
194+
[
195+
(
196+
1,
197+
[
198+
(2, "Task-1", []),
199+
(
200+
3,
201+
"A",
202+
[[["nested", "nested", "task_b"], 4]],
203+
),
204+
(
205+
4,
206+
"B",
207+
[
208+
[["nested", "nested", "task_c"], 5],
209+
[["nested", "nested", "task_a"], 3],
210+
],
211+
),
212+
(5, "C", [[["nested", "nested"], 6]]),
213+
(
214+
6,
215+
"Task-2",
216+
[[["nested", "nested", "task_b"], 4]],
217+
),
218+
],
219+
),
220+
(0, []),
221+
]
222+
),
223+
([[4, 3, 4], [4, 6, 5, 4]]),
224+
],
225+
]
226+
227+
TEST_INPUTS_TABLE = [
228+
[
229+
# test case containing a task called timer being awaited in two
230+
# different subtasks part of a TaskGroup (root1 and root2) which call
231+
# awaiter functions.
232+
(
233+
(
234+
1,
235+
[
236+
(2, "Task-1", []),
237+
(
238+
3,
239+
"timer",
240+
[
241+
[["awaiter3", "awaiter2", "awaiter"], 4],
242+
[["awaiter1_3", "awaiter1_2", "awaiter1"], 5],
243+
[["awaiter1_3", "awaiter1_2", "awaiter1"], 6],
244+
[["awaiter3", "awaiter2", "awaiter"], 7],
245+
],
246+
),
247+
(
248+
8,
249+
"root1",
250+
[[["_aexit", "__aexit__", "main"], 2]],
251+
),
252+
(
253+
9,
254+
"root2",
255+
[[["_aexit", "__aexit__", "main"], 2]],
256+
),
257+
(
258+
4,
259+
"child1_1",
260+
[
261+
[
262+
["_aexit", "__aexit__", "blocho_caller", "bloch"],
263+
8,
264+
]
265+
],
266+
),
267+
(
268+
6,
269+
"child2_1",
270+
[
271+
[
272+
["_aexit", "__aexit__", "blocho_caller", "bloch"],
273+
8,
274+
]
275+
],
276+
),
277+
(
278+
7,
279+
"child1_2",
280+
[
281+
[
282+
["_aexit", "__aexit__", "blocho_caller", "bloch"],
283+
9,
284+
]
285+
],
286+
),
287+
(
288+
5,
289+
"child2_2",
290+
[
291+
[
292+
["_aexit", "__aexit__", "blocho_caller", "bloch"],
293+
9,
294+
]
295+
],
296+
),
297+
],
298+
),
299+
(0, []),
300+
),
124301
(
125302
[
126303
[
@@ -230,25 +407,6 @@
230407
(11, []),
231408
(0, []),
232409
),
233-
([]),
234-
(
235-
[
236-
[
237-
"└── (T) Task-5",
238-
" └── main2",
239-
" ├── (T) Task-6",
240-
" ├── (T) Task-7",
241-
" └── (T) Task-8",
242-
],
243-
[
244-
"└── (T) Task-1",
245-
" └── main",
246-
" ├── (T) Task-2",
247-
" ├── (T) Task-3",
248-
" └── (T) Task-4",
249-
],
250-
]
251-
),
252410
(
253411
[
254412
[9, "0x6", "Task-6", "main2", "Task-5", "0x5"],
@@ -260,7 +418,7 @@
260418
]
261419
),
262420
],
263-
# Tests cycle detection.
421+
# CASES WITH CYCLES
264422
[
265423
# this test case contains a cycle: two tasks awaiting each other.
266424
(
@@ -280,8 +438,6 @@
280438
(0, []),
281439
]
282440
),
283-
([[4, 3, 4]]),
284-
([]),
285441
(
286442
[
287443
[1, "0x3", "a", "awaiter2", "b", "0x4"],
@@ -322,8 +478,6 @@
322478
(0, []),
323479
]
324480
),
325-
([[4, 3, 4], [4, 6, 5, 4]]),
326-
([]),
327481
(
328482
[
329483
[
@@ -372,15 +526,24 @@
372526
]
373527

374528

375-
class TestAsyncioTools(unittest.TestCase):
529+
class TestAsyncioToolsTree(unittest.TestCase):
376530

377531
def test_asyncio_utils(self):
378-
for input_, cycles, tree, table in TEST_INPUTS:
379-
if cycles:
532+
for input_, tree in TEST_INPUTS_TREE:
533+
with self.subTest(input_):
534+
self.assertEqual(tools.print_async_tree(input_), tree)
535+
536+
def test_asyncio_utils_cycles(self):
537+
for input_, cycles in TEST_INPUTS_CYCLES_TREE:
538+
with self.subTest(input_):
380539
try:
381540
tools.print_async_tree(input_)
382541
except tools.CycleFoundException as e:
383542
self.assertEqual(e.cycles, cycles)
384-
else:
385-
self.assertEqual(tools.print_async_tree(input_), tree)
386-
self.assertEqual(tools.build_task_table(input_), table)
543+
544+
545+
class TestAsyncioToolsTable(unittest.TestCase):
546+
def test_asyncio_utils(self):
547+
for input_, table in TEST_INPUTS_TABLE:
548+
with self.subTest(input_):
549+
self.assertEqual(tools.build_task_table(input_), table)

0 commit comments

Comments
 (0)