Skip to content

Commit

Permalink
Allow testing trio
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 15, 2024
1 parent 657ca59 commit 18e7317
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
5 changes: 4 additions & 1 deletion ipykernel/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def start_soon(self, task: Callable[[], Awaitable[None]] | None) -> None:

def run(self) -> None:
"""Run the thread."""
run(self._main)
try:
run(self._main)
except Exception:
pass

async def _main(self) -> None:
async with create_task_group() as tg:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ filterwarnings= [

# ignore timeout cancel coroutine not awaited in zmq-anyio
"ignore: coroutine 'Poller._apoll.<locals>.trigger_timeout' was never awaited",
"ignore: Unclosed socket"
]

[tool.coverage.report]
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from math import inf
from threading import Event
from typing import Any, Callable, no_type_check
from unittest.mock import MagicMock

Expand All @@ -21,11 +22,6 @@
resource = None # type:ignore


@pytest.fixture()
def anyio_backend():
return "asyncio"


pytestmark = pytest.mark.anyio


Expand Down Expand Up @@ -159,6 +155,8 @@ class MockKernel(KernelMixin, Kernel): # type:ignore
def __init__(self, *args, **kwargs):
self._initialize()
self.shell = MagicMock()
self.shell_stop = Event()
self.control_stop = Event()
super().__init__(*args, **kwargs)

def do_execute(
Expand All @@ -180,6 +178,8 @@ def do_execute(
class MockIPyKernel(KernelMixin, IPythonKernel): # type:ignore
def __init__(self, *args, **kwargs):
self._initialize()
self.shell_stop = Event()
self.control_stop = Event()
super().__init__(*args, **kwargs)


Expand Down
3 changes: 1 addition & 2 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from .test_message_spec import validate_message
from .utils import TIMEOUT, execute, flush_channels, start_new_kernel

pytestmark = pytest.mark.anyio

KC = KM = None


Expand All @@ -33,6 +31,7 @@ def test_async_await():


@pytest.mark.skipif(os.name == "nt", reason="Cannot interrupt on Windows")
@pytest.mark.parametrize("anyio_backend", ["asyncio"]) # FIXME: %autoawait trio
def test_async_interrupt(anyio_backend, request):
assert KC is not None
assert KM is not None
Expand Down
1 change: 1 addition & 0 deletions tests/test_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def do_thing():


@windows_skip
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_asyncio_loop(kernel):
def do_thing():
loop.call_later(0.01, loop.stop)
Expand Down
23 changes: 12 additions & 11 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def ctx():

@pytest.fixture()
async def iopub_thread(ctx):
async with zmq_anyio.Socket(ctx.socket(zmq.PUB)) as pub:
thread = IOPubThread(pub)
thread.start()
try:
async with zmq_anyio.Socket(ctx.socket(zmq.PUB)) as pub:
thread = IOPubThread(pub)
thread.start()

yield thread
thread.stop()
thread.close()
yield thread
thread.stop()
thread.close()
except Exception:
pass


async def test_io_api(iopub_thread):
Expand Down Expand Up @@ -67,7 +70,7 @@ async def test_io_isatty(iopub_thread):
assert stream.isatty()


async def test_io_thread(anyio_backend, iopub_thread):
async def test_io_thread(iopub_thread):
thread = iopub_thread
thread._setup_pipe_in()
msg = [thread._pipe_uuid, b"a"]
Expand All @@ -81,7 +84,7 @@ async def test_io_thread(anyio_backend, iopub_thread):
thread.stop()


async def test_background_socket(anyio_backend, iopub_thread):
async def test_background_socket(iopub_thread):
sock = BackgroundSocket(iopub_thread)
assert sock.__class__ == BackgroundSocket
with warnings.catch_warnings():
Expand All @@ -92,7 +95,7 @@ async def test_background_socket(anyio_backend, iopub_thread):
sock.send(b"hi")


async def test_outstream(anyio_backend, iopub_thread):
async def test_outstream(iopub_thread):
session = Session()
pub = iopub_thread.socket

Expand All @@ -118,7 +121,6 @@ async def test_outstream(anyio_backend, iopub_thread):
assert stream.writable()


@pytest.mark.anyio()
async def test_event_pipe_gc(iopub_thread):
session = Session(key=b"abc")
stream = OutStream(
Expand Down Expand Up @@ -193,7 +195,6 @@ async def subprocess_test_echo_watch():
iopub_thread.close()


@pytest.mark.anyio()
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Windows")
async def test_echo_watch(ctx):
"""Test echo on underlying FD while capturing the same FD
Expand Down

0 comments on commit 18e7317

Please sign in to comment.