Skip to content

Commit 92f9799

Browse files
committed
fix test. polish comments and tests
1 parent b826210 commit 92f9799

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

newsfragments/3035.feature.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
:class:`trio.Lock` and :class:`trio.StrictFIFOLock` will now raise :exc:`trio.BrokenResourceError` when :meth:`trio.Lock.acquire` would previously stall due to the owner of the lock having exited without releasing the lock.
1+
:class:`trio.Lock` and :class:`trio.StrictFIFOLock` will now raise :exc:`trio.BrokenResourceError` when :meth:`trio.Lock.acquire` would previously stall due to the owner of the lock exiting without releasing the lock.

src/trio/_core/_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ async def python_wrapper(orig_coro: Awaitable[RetT]) -> RetT:
18971897
return task
18981898

18991899
def task_exited(self, task: Task, outcome: Outcome[Any]) -> None:
1900-
# break parking lots associated with the task exiting
1900+
# break parking lots associated with the exiting task
19011901
if task in GLOBAL_PARKING_LOT_BREAKER:
19021902
for lot in GLOBAL_PARKING_LOT_BREAKER[task]:
19031903
lot.break_lot(task)

src/trio/_core/_tests/test_parking_lot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,11 @@ async def test_parking_lot_breaker_rebreak() -> None:
329329
lot.break_lot(child_task)
330330
nursery.cancel_scope.cancel()
331331

332-
# and appends the task
333332
assert lot.broken_by == [task, child_task]
334333

335334

336335
async def test_parking_lot_multiple_breakers_exit() -> None:
337336
# register multiple tasks as lot breakers, then have them all exit
338-
# No warning is given on task exit, even if the lot is already broken.
339337
lot = ParkingLot()
340338
async with trio.open_nursery() as nursery:
341339
child_task1 = await nursery.start(dummy_task)

src/trio/_tests/test_sync.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
import weakref
45
from typing import TYPE_CHECKING, Callable, Union
56

@@ -599,9 +600,10 @@ async def test_lock_acquire_unowned_lock() -> None:
599600
lock = trio.Lock()
600601
async with trio.open_nursery() as nursery:
601602
nursery.start_soon(lock.acquire)
603+
owner_str = re.escape(str(lock._lot.broken_by[0]))
602604
with pytest.raises(
603605
trio.BrokenResourceError,
604-
match="^Owner of this lock exited without releasing",
606+
match=f"^Owner of this lock exited without releasing: {owner_str}$",
605607
):
606608
await lock.acquire()
607609
assert not GLOBAL_PARKING_LOT_BREAKER
@@ -615,7 +617,7 @@ async def test_lock_multiple_acquire() -> None:
615617
with RaisesGroup(
616618
Matcher(
617619
trio.BrokenResourceError,
618-
match="^Owner of this lock exited without releasing",
620+
match="^Owner of this lock exited without releasing: ",
619621
),
620622
):
621623
async with trio.open_nursery() as nursery:
@@ -626,9 +628,11 @@ async def test_lock_multiple_acquire() -> None:
626628

627629
async def test_lock_handover() -> None:
628630
assert not GLOBAL_PARKING_LOT_BREAKER
631+
child_task: Task | None = None
629632
lock = trio.Lock()
633+
634+
# this task acquires the lock
630635
lock.acquire_nowait()
631-
child_task: Task | None = None
632636
assert GLOBAL_PARKING_LOT_BREAKER == {
633637
_core.current_task(): [
634638
lock._lot,
@@ -639,11 +643,13 @@ async def test_lock_handover() -> None:
639643
nursery.start_soon(lock.acquire)
640644
await wait_all_tasks_blocked()
641645

646+
# hand over the lock to the child task
642647
lock.release()
643648

649+
# check values, and get the identifier out of the dict for later check
644650
assert len(GLOBAL_PARKING_LOT_BREAKER) == 1
645651
child_task = next(iter(GLOBAL_PARKING_LOT_BREAKER))
646652
assert GLOBAL_PARKING_LOT_BREAKER[child_task] == [lock._lot]
647653

648-
assert lock._lot.broken_by == child_task
654+
assert lock._lot.broken_by == [child_task]
649655
assert not GLOBAL_PARKING_LOT_BREAKER

0 commit comments

Comments
 (0)