Skip to content

Commit

Permalink
aioble/multitests: Store a reference to tasks and cancel when done.
Browse files Browse the repository at this point in the history
Storing references to tasks is required by CPython, and enforced by Ruff
RUF006.  In this case it's also reasonable to cancel these tasks once the
test is finished.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jun 12, 2024
1 parent 4063dac commit 3485f7b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions micropython/bluetooth/aioble/multitests/ble_write_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ async def instance0_task():

# Register characteristic.written() handlers as asyncio background tasks.
# The order of these is important!
asyncio.create_task(task_written(characteristic_second, "second"))
asyncio.create_task(task_written(characteristic_first, "first"))
task_second = asyncio.create_task(task_written(characteristic_second, "second"))
task_first = asyncio.create_task(task_written(characteristic_first, "first"))

# This dummy task simulates background processing on a real system that
# can block the asyncio loop for brief periods of time
asyncio.create_task(task_dummy())
task_dummy_ = asyncio.create_task(task_dummy())

multitest.globals(BDADDR=aioble.config("mac"))
multitest.next()
Expand All @@ -63,6 +63,10 @@ async def instance0_task():

await connection.disconnected()

task_second.cancel()
task_first.cancel()
task_dummy_.cancel()


async def task_written(chr, label):
while True:
Expand Down

0 comments on commit 3485f7b

Please sign in to comment.