Skip to content

Commit

Permalink
Safely set event loop to None
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjacksn committed Sep 8, 2018
1 parent 3772f06 commit a025cfa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiocontextvars/__init__.py
Expand Up @@ -50,7 +50,7 @@ def create_task(loop, coro):


def _patch_loop(loop):
if not hasattr(loop, '_orig_create_task'):
if loop and not hasattr(loop, '_orig_create_task'):
loop._orig_create_task = loop.create_task
loop.create_task = types.MethodType(create_task, loop)
return loop
Expand Down
11 changes: 9 additions & 2 deletions tests/test_aio.py
Expand Up @@ -2,27 +2,29 @@
import pytest
from aiocontextvars import ContextVar

pytestmark = pytest.mark.asyncio

v = ContextVar('v')


@pytest.mark.asyncio
async def parallel(x):
v.set(x)
await asyncio.sleep(0.1)
assert v.get() == x


@pytest.mark.asyncio
async def test_parallel():
await asyncio.gather(*map(parallel, range(16)))


# noinspection PyUnusedLocal
@pytest.mark.asyncio
async def test_parallel_with_inherit():
await asyncio.gather(*map(parallel, range(16)))


# noinspection PyUnusedLocal
@pytest.mark.asyncio
async def test_inherit():
v.set('initial')

Expand All @@ -34,3 +36,8 @@ async def sub():
await asyncio.sleep(0.1)
await fut
assert v.get('default') == 'update'


def test_set_event_loop_none():
# Setting event loop to None should not raise an exception
asyncio.set_event_loop(None)
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py35, py36, flake8
envlist = py35, py36, py37, flake8

[travis]
python =
Expand Down

0 comments on commit a025cfa

Please sign in to comment.