Skip to content

Commit

Permalink
One more defer test (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche authored Mar 6, 2025
1 parent a0ab72a commit faf5c58
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/test_defer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,58 @@ async def coro(a: int) -> int:
assert exited


async def test_defer_nested() -> None:
"""The `defer` decorator works on nested coroutines."""

entered = False
entered2 = False
exited = False
exited2 = False

@asynccontextmanager
async def asynccm():
nonlocal entered, exited
entered = True
yield 1
exited = True

@asynccontextmanager
async def asynccm2():
nonlocal entered2, exited2
entered2 = True
yield 2
exited2 = True

@defer.enable
async def coro(a: int) -> int:
assert not entered
assert await defer(asynccm()) == 1
assert entered
return a

@defer.enable
async def coro2(b: int) -> int:
assert not entered
assert not entered2
assert await defer(asynccm2()) == 2
assert not entered
assert entered2

assert not exited
assert not exited2

await coro(b)

assert exited
assert not exited2
return b

assert await coro2(1) == 1

assert exited
assert exited2


async def test_defer_no_decorator() -> None:
"""Forgetting the decorator raises."""

Expand Down

0 comments on commit faf5c58

Please sign in to comment.