Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand the example test by showing cancelling() is 0
  • Loading branch information
ambv committed Jul 29, 2022
commit f0a215d7ffbf7ff402461127c6cdb386648d320a
15 changes: 11 additions & 4 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,18 @@ def on_timeout():
# code might have been nested (think multiple timeouts). See
# commit 7fce1063b6e5a366f8504e039a8ccdd6944625cd for
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that commit 7d611b4 changes the behavior again.

# details.
# 5. we only convert CancelledError to TimeoutError; if the user
# code raised a different exception due to the cancellation
# (like a ConnectionLostError from a database client), we
# propagate it.
# 5. we only convert CancelledError to TimeoutError; for other
# exceptions raised due to the cancellation (like
# a ConnectionLostError from a database client), simply
# propagate them.
#
# Those checks need to take place in this exact order to make
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Those checks" == (2), (4), (5), right? Because (1) and (3) don't describe "checks".

Other than that nit, these comments seem correct, and the code looks so too (but I'm not taking money :-).

(Another nit: the huge comment interrupts the logic. Maybe move it to the top of the test function?)

# sure the `cancelling()` counter always stays in sync.
#
# Additionally, the original stimulus to `cancel()` the task
# needs to be unscheduled to avoid re-cancelling the task later.
# Here we do it by cancelling `timeout_handle` in the `finally:`
# block.
raise TimeoutError
except TimeoutError:
self.assertTrue(timed_out)
Expand All @@ -648,6 +653,7 @@ def on_timeout():
self.assertTrue(outer_code_reached) # task got uncancelled after leaving
# the structured block and continued until
# completion
self.assertEqual(t1.cancelling(), 0) # no pending cancellation of the outer task

# Test which did not time out.
t2 = self.new_task(loop, make_request_with_timeout(sleep=0, timeout=10.0))
Expand All @@ -657,6 +663,7 @@ def on_timeout():
self.assertFalse(timed_out)
self.assertTrue(structured_block_finished)
self.assertTrue(outer_code_reached)
self.assertEqual(t2.cancelling(), 0)

def test_cancel(self):

Expand Down