Skip to content

Commit

Permalink
Handle asyncio loop setup/teardown in sphinx doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Dec 22, 2023
1 parent 430e11e commit 764cfa2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def _load_custom_sections(self):
'sphinx.ext.viewcode',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'pytest_doctestplus.sphinx.doctestplus',
'propertyobj',
'builtinproperty',
'eventobj',
Expand Down
19 changes: 12 additions & 7 deletions doc/source/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import pytest
import asyncio

def receiver_setup(request, doctest_namespace):
node_name = request.node.name

@pytest.fixture(scope='function')
def new_loop(request, doctest_namespace):
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
policy.set_event_loop(loop)
# logger.info(f'{request.node=}, {request.function=}, {request.module=}')
# logger.info(f'event loop: {loop!r}')
# doctest_namespace['_asyncio'] = asyncio
# doctest_namespace['_loop'] = loop
doctest_namespace['loop'] = loop
yield loop
loop.close()
policy.set_event_loop(None)

def receiver_setup(request, loop):

cleanup_coro = None

Expand Down Expand Up @@ -47,8 +50,10 @@ async def cleanup():
asyncio.set_event_loop_policy(None)

@pytest.fixture(scope="function", autouse=True)
def doctest_stuff(request, doctest_namespace):
def doctest_stuff(request, new_loop, doctest_namespace):
node_name = request.node.name
loop = asyncio.get_event_loop()
assert loop is new_loop
if node_name == 'receiver.rst':
yield from receiver_setup(request, doctest_namespace)
else:
Expand Down
7 changes: 2 additions & 5 deletions doc/source/receiver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Starting and stopping can be done by calling the :meth:`UmdReceiver.open` and
... await receiver.open()
... ...
... await receiver.close()
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(run())
>>> asyncio.run(run())

or it can be used as an :term:`asynchronous context manager`
in an :keyword:`async with` block
Expand All @@ -39,8 +38,7 @@ in an :keyword:`async with` block
... receiver = UmdReceiver()
... async with receiver:
... ...
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(run())
>>> asyncio.run(run())


Object Access
Expand Down Expand Up @@ -89,7 +87,6 @@ They are indexed 1 through 4 and their screen index is 1.
... for name in props_changed:
... value = getattr(tally, name)
... print(f'tally_updated: {tally!r}.{name} = {value}')
>>> loop = asyncio.get_event_loop()
>>> receiver = UmdReceiver()
>>> receiver.bind(
... on_screen_added=screen_added,
Expand Down
8 changes: 4 additions & 4 deletions doc/source/sender.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Starting and stopping can be done by calling the :meth:`UmdSender.open` and
... await sender.open()
... ...
... await sender.close()
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(run())
>>> asyncio.run(run())

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


Object Access
Expand Down Expand Up @@ -96,6 +94,7 @@ and :term:`TallyColor` arguments are used.
>>> from pprint import pprint
>>> from tslumd import UmdSender, TallyType, TallyColor
>>> sender = UmdSender(clients=[('127.0.0.1', 65000)])
>>> loop.run_until_complete(sender.open())
>>> for cam_num in range(1, 5):
... sender.set_tally_text((1, cam_num), f'Camera {cam_num}') # Creates a new Tally
>>> pprint(sender.tallies)
Expand All @@ -114,6 +113,7 @@ and :term:`TallyColor` arguments are used.
(1, 2): <Tally: ((1, 2) - "Camera 2")>,
(1, 3): <Tally: ((1, 3) - "Camera 3")>,
(1, 4): <Tally: ((1, 4) - "Handheld")>}
>>> loop.run_until_complete(sender.close())


Direct Tally Changes
Expand Down

0 comments on commit 764cfa2

Please sign in to comment.