Skip to content

Commit 764cfa2

Browse files
committed
Handle asyncio loop setup/teardown in sphinx doctests
1 parent 430e11e commit 764cfa2

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

doc/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def _load_custom_sections(self):
6767
'sphinx.ext.viewcode',
6868
'sphinx.ext.intersphinx',
6969
'sphinx.ext.todo',
70+
'pytest_doctestplus.sphinx.doctestplus',
7071
'propertyobj',
7172
'builtinproperty',
7273
'eventobj',

doc/source/conftest.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
import pytest
33
import asyncio
44

5-
def receiver_setup(request, doctest_namespace):
6-
node_name = request.node.name
5+
6+
@pytest.fixture(scope='function')
7+
def new_loop(request, doctest_namespace):
78
policy = asyncio.get_event_loop_policy()
89
loop = policy.new_event_loop()
910
policy.set_event_loop(loop)
10-
# logger.info(f'{request.node=}, {request.function=}, {request.module=}')
11-
# logger.info(f'event loop: {loop!r}')
12-
# doctest_namespace['_asyncio'] = asyncio
13-
# doctest_namespace['_loop'] = loop
11+
doctest_namespace['loop'] = loop
12+
yield loop
13+
loop.close()
14+
policy.set_event_loop(None)
15+
16+
def receiver_setup(request, loop):
1417

1518
cleanup_coro = None
1619

@@ -47,8 +50,10 @@ async def cleanup():
4750
asyncio.set_event_loop_policy(None)
4851

4952
@pytest.fixture(scope="function", autouse=True)
50-
def doctest_stuff(request, doctest_namespace):
53+
def doctest_stuff(request, new_loop, doctest_namespace):
5154
node_name = request.node.name
55+
loop = asyncio.get_event_loop()
56+
assert loop is new_loop
5257
if node_name == 'receiver.rst':
5358
yield from receiver_setup(request, doctest_namespace)
5459
else:

doc/source/receiver.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Starting and stopping can be done by calling the :meth:`UmdReceiver.open` and
2525
... await receiver.open()
2626
... ...
2727
... await receiver.close()
28-
>>> loop = asyncio.get_event_loop()
29-
>>> loop.run_until_complete(run())
28+
>>> asyncio.run(run())
3029

3130
or it can be used as an :term:`asynchronous context manager`
3231
in an :keyword:`async with` block
@@ -39,8 +38,7 @@ in an :keyword:`async with` block
3938
... receiver = UmdReceiver()
4039
... async with receiver:
4140
... ...
42-
>>> loop = asyncio.get_event_loop()
43-
>>> loop.run_until_complete(run())
41+
>>> asyncio.run(run())
4442

4543

4644
Object Access
@@ -89,7 +87,6 @@ They are indexed 1 through 4 and their screen index is 1.
8987
... for name in props_changed:
9088
... value = getattr(tally, name)
9189
... print(f'tally_updated: {tally!r}.{name} = {value}')
92-
>>> loop = asyncio.get_event_loop()
9390
>>> receiver = UmdReceiver()
9491
>>> receiver.bind(
9592
... on_screen_added=screen_added,

doc/source/sender.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ Starting and stopping can be done by calling the :meth:`UmdSender.open` and
3333
... await sender.open()
3434
... ...
3535
... await sender.close()
36-
>>> loop = asyncio.get_event_loop()
37-
>>> loop.run_until_complete(run())
36+
>>> asyncio.run(run())
3837

3938
or it can be used as an :term:`asynchronous context manager`
4039
in an :keyword:`async with` block
@@ -47,8 +46,7 @@ in an :keyword:`async with` block
4746
... sender = UmdSender(clients=[('127.0.0.1', 65000)])
4847
... async with sender:
4948
... ...
50-
>>> loop = asyncio.get_event_loop()
51-
>>> loop.run_until_complete(run())
49+
>>> asyncio.run(run())
5250

5351

5452
Object Access
@@ -96,6 +94,7 @@ and :term:`TallyColor` arguments are used.
9694
>>> from pprint import pprint
9795
>>> from tslumd import UmdSender, TallyType, TallyColor
9896
>>> sender = UmdSender(clients=[('127.0.0.1', 65000)])
97+
>>> loop.run_until_complete(sender.open())
9998
>>> for cam_num in range(1, 5):
10099
... sender.set_tally_text((1, cam_num), f'Camera {cam_num}') # Creates a new Tally
101100
>>> pprint(sender.tallies)
@@ -114,6 +113,7 @@ and :term:`TallyColor` arguments are used.
114113
(1, 2): <Tally: ((1, 2) - "Camera 2")>,
115114
(1, 3): <Tally: ((1, 3) - "Camera 3")>,
116115
(1, 4): <Tally: ((1, 4) - "Handheld")>}
116+
>>> loop.run_until_complete(sender.close())
117117

118118

119119
Direct Tally Changes

0 commit comments

Comments
 (0)