Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 7, 2024
1 parent 0dc2aaa commit fe52720
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ async def process_shell_message(self, msg=None, socket=None):
assert socket is None
socket = self.shell_socket

no_msg = msg is None if self._is_test else not socket.poll(0)
no_msg = msg is None if self._is_test else not await socket.poll(0)
msg = msg or await socket.arecv_multipart(copy=False)

received_time = time.monotonic()
Expand Down
31 changes: 12 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import logging
import os
from math import inf
Expand All @@ -7,8 +6,8 @@

import pytest
import zmq
import zmq.asyncio
from anyio import create_memory_object_stream, create_task_group
import zmq_anyio
from anyio import create_memory_object_stream, create_task_group, sleep
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from jupyter_client.session import Session

Expand All @@ -23,10 +22,9 @@
resource = None # type:ignore


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

# @pytest.fixture
# def anyio_backend():
# return 'asyncio'

pytestmark = pytest.mark.anyio

Expand All @@ -46,11 +44,6 @@ def anyio_backend():
resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))


# Enforce selector event loop on Windows.
if os.name == "nt":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type:ignore


class TestSession(Session):
"""A session that copies sent messages to an internal stream, so that
they can be accessed later.
Expand All @@ -77,21 +70,21 @@ def send(self, socket, *args, **kwargs):


class KernelMixin:
shell_socket: zmq.asyncio.Socket
control_socket: zmq.asyncio.Socket
shell_socket: zmq_anyio.Socket
control_socket: zmq_anyio.Socket
stop: Callable[[], None]

log = logging.getLogger()

def _initialize(self):
self._is_test = True
self.context = context = zmq.asyncio.Context()
self.iopub_socket = context.socket(zmq.PUB)
self.stdin_socket = context.socket(zmq.ROUTER)
self.context = context = zmq.Context()
self.iopub_socket = zmq_anyio.Socket(context.socket(zmq.PUB))
self.stdin_socket = zmq_anyio.Socket(context.socket(zmq.ROUTER))
self.test_sockets = [self.iopub_socket]

for name in ["shell", "control"]:
socket = context.socket(zmq.ROUTER)
socket = zmq_anyio.Socket(context.socket(zmq.ROUTER))
self.test_sockets.append(socket)
setattr(self, f"{name}_socket", socket)

Expand Down Expand Up @@ -142,7 +135,7 @@ def _prep_msg(self, *args, **kwargs):

async def _wait_for_msg(self):
while not self._reply:
await asyncio.sleep(0.1)
await sleep(0.1)
_, msg = self.session.feed_identities(self._reply)
return self.session.deserialize(msg)

Expand Down
16 changes: 8 additions & 8 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 @@ -30,24 +32,22 @@ def test_async_await():
assert content["status"] == "ok", content


# FIXME: @pytest.mark.parametrize("asynclib", ["asyncio", "trio", "curio"])
@pytest.mark.skipif(os.name == "nt", reason="Cannot interrupt on Windows")
@pytest.mark.parametrize("asynclib", ["asyncio"])
def test_async_interrupt(asynclib, request):
def test_async_interrupt(anyio_backend, request):
assert KC is not None
assert KM is not None
try:
__import__(asynclib)
__import__(anyio_backend)
except ImportError:
pytest.skip("Requires %s" % asynclib)
request.addfinalizer(lambda: execute("%autoawait asyncio", KC))
pytest.skip("Requires %s" % anyio_backend)
request.addfinalizer(lambda: execute(f"%autoawait {anyio_backend}", KC))

flush_channels(KC)
msg_id, content = execute("%autoawait " + asynclib, KC)
msg_id, content = execute(f"%autoawait {anyio_backend}", KC)
assert content["status"] == "ok", content

flush_channels(KC)
msg_id = KC.execute(f"print('begin'); import {asynclib}; await {asynclib}.sleep(5)")
msg_id = KC.execute(f"print('begin'); import {anyio_backend}; await {anyio_backend}.sleep(5)")
busy = KC.get_iopub_msg(timeout=TIMEOUT)
validate_message(busy, "status", msg_id)
assert busy["content"]["execution_state"] == "busy"
Expand Down

0 comments on commit fe52720

Please sign in to comment.