Skip to content

Commit

Permalink
remove event loop detection
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfischer2781 committed Sep 5, 2024
1 parent 36958b9 commit 0c95e71
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 31 deletions.
2 changes: 0 additions & 2 deletions docs/source/devel/testloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ to an ``async def`` test case.
Async commands
==============

.. autoclass:: PingPong

.. autoclass:: Schedule(*await Any)

.. py:class:: Switch(skip: int, /)
Expand Down
5 changes: 2 additions & 3 deletions unittests/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import asyncstdlib as a

from .utility import sync, asyncify, awaitify, inside_loop
from .utility import sync, asyncify, awaitify


def hide_coroutine(corofunc):
Expand Down Expand Up @@ -83,8 +83,7 @@ async def __aiter__(self):
yield 1
finally:
nonlocal closed
if await inside_loop():
closed = True
closed = True

zip_iter = a.zip(asyncify(range(-5, 0)), SomeIterable())
async for va, vb in zip_iter:
Expand Down
28 changes: 2 additions & 26 deletions unittests/utility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import (
Callable,
Coroutine,
Generator,
Iterable,
AsyncIterator,
TypeVar,
Expand Down Expand Up @@ -45,25 +44,6 @@ async def await_wrapper(*args: Any, **kwargs: Any) -> T:
return await_wrapper


class PingPong:
"""
Signal to the event loop which gets returned unchanged
The coroutine yields to the event loop but is resumed
immediately, without running others in the meantime.
This is mainly useful for ensuring the event loop is used.
"""

def __await__(self) -> "Generator[PingPong, Any, Any]":
return (yield self)


async def inside_loop() -> bool:
"""Test whether there is an active event loop available"""
signal = PingPong()
return await signal is signal


class Schedule:
r"""
Signal to the event loop to adopt and run new coroutines
Expand Down Expand Up @@ -136,9 +116,7 @@ def sync(test_case: Callable[..., Coroutine[None, Any, Any]], /) -> Callable[...
Mark an ``async def`` test case to be run synchronously with children
This provides a primitive "event loop" which only responds
to the :py:class:`PingPong`, :py:class:`Schedule`, :py:class:`Switch`
and :py:class:`Lock`. This loop is appropriate for tests that need
to check concurrency.
to :py:class:`Schedule`, :py:class:`Switch` and :py:class:`Lock`.
It should be applied as a decorator on an ``async def`` function, which
is then turned into a synchronous callable that will run the ``async def``
Expand All @@ -159,9 +137,7 @@ def run_sync(*args: Any, **kwargs: Any):
result = e.args[0] if e.args else None
assert result is None, f"got '{result!r}' expected 'None'"
else:
if isinstance(event, PingPong):
run_queue.appendleft((coro, event))
elif isinstance(event, Schedule):
if isinstance(event, Schedule):
run_queue.extend((new_coro, None) for new_coro in event.coros)
run_queue.append((coro, event))
elif isinstance(event, Switch):
Expand Down

0 comments on commit 0c95e71

Please sign in to comment.