Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated from event_loop fixture #569

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Changes from all commits
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
33 changes: 22 additions & 11 deletions tests/test_contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ def _clear_contextvars():


class TestContextvars:
async def test_bind(self, event_loop):
async def test_bind(self):
"""
Binding a variable causes it to be included in the result of
merge_contextvars.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
return merge_contextvars(None, None, {"b": 2})

assert {"a": 1, "b": 2} == await event_loop.create_task(coro())

async def test_multiple_binds(self, event_loop):
async def test_multiple_binds(self):
"""
Multiple calls to bind_contextvars accumulate values instead of
replacing them. But they override redefined ones.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1, c=3)
Expand All @@ -63,11 +65,12 @@ async def coro():
"d": 4,
} == await event_loop.create_task(coro())

async def test_reset(self, event_loop):
async def test_reset(self):
"""
reset_contextvars allows resetting contexvars to
previously-set values.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
Expand All @@ -87,10 +90,11 @@ async def nested_coro():

await event_loop.create_task(coro())

async def test_nested_async_bind(self, event_loop):
async def test_nested_async_bind(self):
"""
Context is passed correctly between "nested" concurrent operations.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
Expand All @@ -102,34 +106,37 @@ async def nested_coro():

assert {"a": 1, "b": 2, "c": 3} == await event_loop.create_task(coro())

async def test_merge_works_without_bind(self, event_loop):
async def test_merge_works_without_bind(self):
"""
merge_contextvars returns values as normal even when there has
been no previous calls to bind_contextvars.
"""
event_loop = asyncio.get_running_loop()

async def coro():
return merge_contextvars(None, None, {"b": 2})

assert {"b": 2} == await event_loop.create_task(coro())

async def test_merge_overrides_bind(self, event_loop):
async def test_merge_overrides_bind(self):
"""
Variables included in merge_contextvars override previously
bound variables.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
return merge_contextvars(None, None, {"a": 111, "b": 2})

assert {"a": 111, "b": 2} == await event_loop.create_task(coro())

async def test_clear(self, event_loop):
async def test_clear(self):
"""
The context-local context can be cleared, causing any previously bound
variables to not be included in merge_contextvars's result.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
Expand All @@ -138,23 +145,25 @@ async def coro():

assert {"b": 2} == await event_loop.create_task(coro())

async def test_clear_without_bind(self, event_loop):
async def test_clear_without_bind(self):
"""
The context-local context can be cleared, causing any previously bound
variables to not be included in merge_contextvars's result.
"""
event_loop = asyncio.get_running_loop()

async def coro():
clear_contextvars()
return merge_contextvars(None, None, {})

assert {} == await event_loop.create_task(coro())

async def test_unbind(self, event_loop):
async def test_unbind(self):
"""
Unbinding a previously bound variable causes it to be removed from the
result of merge_contextvars.
"""
event_loop = asyncio.get_running_loop()

async def coro():
bind_contextvars(a=1)
Expand All @@ -163,10 +172,11 @@ async def coro():

assert {"b": 2} == await event_loop.create_task(coro())

async def test_unbind_not_bound(self, event_loop):
async def test_unbind_not_bound(self):
"""
Unbinding a not bound variable causes doesn't raise an exception.
"""
event_loop = asyncio.get_running_loop()

async def coro():
# Since unbinding means "setting to Ellipsis", we have to make
Expand All @@ -177,11 +187,12 @@ async def coro():

assert {"b": 2} == await event_loop.create_task(coro())

async def test_parallel_binds(self, event_loop):
async def test_parallel_binds(self):
"""
Binding a variable causes it to be included in the result of
merge_contextvars.
"""
event_loop = asyncio.get_running_loop()
coro1_bind = asyncio.Event()
coro2_bind = asyncio.Event()

Expand Down