Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


@pytest.fixture
def new_event_loop():
"""Run a sync test with its own current asyncio event loop.

Starting a kernel calls IOLoop.current(), which needs one. Without it,
Python 3.12+ warns "There is no current event loop", which fails the test,
so the test only passed if an earlier test happened to leave a loop set.
"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
yield loop
asyncio.set_event_loop(None)
loop.close()


@pytest_asyncio.fixture()
def kernel():
kernel = MockKernel()
Expand Down
12 changes: 10 additions & 2 deletions tests/test_embed_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from jupyter_core import paths

from ipykernel.embed import IPKernelApp, embed_kernel # type:ignore[attr-defined]
from ipykernel.zmqshell import ZMQInteractiveShell

SETUP_TIMEOUT = 60
TIMEOUT = 15
Expand Down Expand Up @@ -197,7 +198,7 @@ def test_embed_kernel_reentrant():
time.sleep(0.2)


def test_embed_kernel_func():
def test_embed_kernel_func(new_event_loop):
from types import ModuleType

module = ModuleType("test")
Expand All @@ -211,4 +212,11 @@ def trigger_stop():
thread = threading.Thread(target=trigger_stop)
thread.start()

embed_kernel(module, outstream_class=None)
try:
embed_kernel(module, outstream_class=None)
finally:
thread.join()
# the embedded kernel's shell is a singleton. Left behind, it breaks
# later tests that create an in-process shell (MultipleInstanceError)
IPKernelApp.clear_instance()
ZMQInteractiveShell.clear_instance()
5 changes: 3 additions & 2 deletions tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import IPython
import psutil
import pytest
from IPython.paths import locate_profile

from .utils import (
TIMEOUT,
Expand Down Expand Up @@ -366,7 +365,9 @@ def test_sys_path():
def test_sys_path_profile_dir():
"""test that sys.path doesn't get messed up when `--profile-dir` is specified"""

with new_kernel(["--profile-dir", locate_profile("default")]) as kc:
# a temporary profile, so this doesn't depend on an earlier test having
# created the default one
with TemporaryDirectory() as profile_dir, new_kernel(["--profile-dir", profile_dir]) as kc:
_msg_id, _content = execute(kc=kc, code="import sys; print(repr(sys.path))")
stdout, stderr = assemble_output(kc.get_iopub_msg)
# for error-output on failure
Expand Down
2 changes: 1 addition & 1 deletion tests/test_kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_blackhole():
app.init_blackhole()


def test_start_app():
def test_start_app(new_event_loop):
app = IPKernelApp()
app.kernel = MockKernel()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_kernelspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def test_install_kernelspec():
assert_is_spec(os.path.join(path, "share", "jupyter", "kernels", KERNEL_NAME))
finally:
shutil.rmtree(path)
# launch_instance leaves the app as a singleton, which stops any later
# test from creating an IPKernelApp (MultipleInstanceError)
InstallIPythonKernelSpecApp.clear_instance()


def test_install_user():
Expand Down
Loading