Skip to content

Commit

Permalink
change contextvars modification test to not use trio_asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
agnesnatasya committed Oct 7, 2023
1 parent eafd418 commit 29a4895
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pytest_trio/_tests/test_fixture_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,34 +165,47 @@ def test_contextvars_modification_follows_fixture_ordering(testdir):
Specifically this ensures that fixtures that modify context variables
doesn't lead to a race condition.
This tries to simluate thing like trio_asyncio.open_loop that modifies
the contextvar.
Main assertion is that 2 async tasks in teardown (Resource.__aexit__)
doesn't crash.
"""
testdir.makepyfile(
"""
import pytest
import trio_asyncio
import asyncio
import trio
from contextlib import asynccontextmanager
current_value = ContextVar("variable", default=None)
@asynccontextmanager
async def variable_setter():
old_value = current_value.set("value")
try:
await trio.sleep(0)
yield
finally:
current_value.reset(old_value)
class Resource():
async def __aenter__(self):
await trio_asyncio.aio_as_trio(asyncio.sleep(0))
await trio.sleep(0)
async def __aexit__(self, *_):
# We need to yield and run another trio task
await trio.sleep(0.1)
await trio_asyncio.aio_as_trio(asyncio.sleep(0))
await trio.sleep(0)
assert current_value.get() is not None
@pytest.fixture
async def resource():
async with trio_asyncio.open_loop() as loop:
async with variable_setter() as loop:
async with Resource():
yield
@pytest.fixture
async def trio_asyncio_loop():
async with trio_asyncio.open_loop() as loop:
async with variable_setter() as loop:
yield loop
@pytest.mark.trio
Expand Down

0 comments on commit 29a4895

Please sign in to comment.