Skip to content

Commit 50dff2e

Browse files
Merge pull request #627 from kevin-bates/mkm-subclass-testing
Add tests to ensure MultiKernelManager subclass methods are called
2 parents 873d6d3 + ae9f6cc commit 50dff2e

File tree

3 files changed

+222
-30
lines changed

3 files changed

+222
-30
lines changed

jupyter_client/tests/test_kernelmanager.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from ..manager import start_new_kernel, start_new_async_kernel
2424
from ..manager import _ShutdownStatus
25-
from .utils import test_env, SyncKernelManagerSubclass, AsyncKernelManagerSubclass, AsyncKernelManagerWithCleanup
25+
from .utils import test_env, SyncKMSubclass, AsyncKMSubclass, AsyncKernelManagerWithCleanup
2626

2727
pjoin = os.path.join
2828

@@ -106,7 +106,7 @@ def km(config):
106106

107107
@pytest.fixture
108108
def km_subclass(config):
109-
km = SyncKernelManagerSubclass(config=config)
109+
km = SyncKMSubclass(config=config)
110110
return km
111111

112112

@@ -118,15 +118,15 @@ def zmq_context():
118118
ctx.term()
119119

120120

121-
@pytest.fixture(params=[AsyncKernelManager, AsyncKernelManagerSubclass, AsyncKernelManagerWithCleanup])
121+
@pytest.fixture(params=[AsyncKernelManager, AsyncKMSubclass, AsyncKernelManagerWithCleanup])
122122
def async_km(request, config):
123123
km = request.param(config=config)
124124
return km
125125

126126

127127
@pytest.fixture
128128
def async_km_subclass(config):
129-
km = AsyncKernelManagerSubclass(config=config)
129+
km = AsyncKMSubclass(config=config)
130130
return km
131131

132132

@@ -173,6 +173,7 @@ def test_signal_kernel_subprocesses(self, name, install, expected):
173173

174174
assert km._shutdown_status == expected
175175

176+
@pytest.mark.asyncio
176177
@pytest.mark.skipif(
177178
sys.platform == "win32", reason="Windows doesn't support signals"
178179
)
@@ -452,7 +453,7 @@ async def test_get_connect_info(self, async_km):
452453
])
453454
assert keys == expected
454455

455-
async def test_subclasses(self, async_km):
456+
async def test_subclass_deprecations(self, async_km):
456457
await async_km.start_kernel(stdout=PIPE, stderr=PIPE)
457458
is_alive = await async_km.is_alive()
458459
assert is_alive
@@ -464,7 +465,7 @@ async def test_subclasses(self, async_km):
464465

465466
if isinstance(async_km, AsyncKernelManagerWithCleanup):
466467
assert async_km.which_cleanup == "cleanup"
467-
elif isinstance(async_km, AsyncKernelManagerSubclass):
468+
elif isinstance(async_km, AsyncKMSubclass):
468469
assert async_km.which_cleanup == "cleanup_resources"
469470
else:
470471
assert hasattr(async_km, "which_cleanup") is False

jupyter_client/tests/test_multikernelmanager.py

+125-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from unittest import TestCase
1010
from tornado.testing import AsyncTestCase, gen_test
1111
from traitlets.config.loader import Config
12-
from jupyter_client import KernelManager, AsyncKernelManager
12+
from jupyter_client import KernelManager
1313
from jupyter_client.multikernelmanager import MultiKernelManager, AsyncMultiKernelManager
14-
from .utils import skip_win32
14+
from .utils import skip_win32, SyncMKMSubclass, AsyncMKMSubclass, SyncKMSubclass, AsyncKMSubclass
1515
from ..localinterfaces import localhost
1616

1717
TIMEOUT = 30
@@ -26,6 +26,12 @@ def _get_tcp_km():
2626
km = MultiKernelManager(config=c)
2727
return km
2828

29+
@staticmethod
30+
def _get_tcp_km_sub():
31+
c = Config()
32+
km = SyncMKMSubclass(config=c)
33+
return km
34+
2935
# static so picklable for multiprocessing on Windows
3036
@staticmethod
3137
def _get_ipc_km():
@@ -153,6 +159,61 @@ def test_start_parallel_process_kernels(self):
153159

154160
assert proc.exitcode == 0
155161

162+
def test_subclass_callables(self):
163+
km = self._get_tcp_km_sub()
164+
165+
km.reset_counts()
166+
kid = km.start_kernel(stdout=PIPE, stderr=PIPE)
167+
assert km.call_count('start_kernel') == 1
168+
assert isinstance(km.get_kernel(kid), SyncKMSubclass)
169+
assert km.get_kernel(kid).call_count('start_kernel') == 1
170+
assert km.get_kernel(kid).call_count('_launch_kernel') == 1
171+
172+
assert km.is_alive(kid)
173+
assert kid in km
174+
assert kid in km.list_kernel_ids()
175+
assert len(km) == 1, f'{len(km)} != {1}'
176+
177+
km.get_kernel(kid).reset_counts()
178+
km.reset_counts()
179+
km.restart_kernel(kid, now=True)
180+
assert km.call_count('restart_kernel') == 1
181+
assert km.call_count('get_kernel') == 1
182+
assert km.get_kernel(kid).call_count('restart_kernel') == 1
183+
assert km.get_kernel(kid).call_count('shutdown_kernel') == 1
184+
assert km.get_kernel(kid).call_count('interrupt_kernel') == 1
185+
assert km.get_kernel(kid).call_count('_kill_kernel') == 1
186+
assert km.get_kernel(kid).call_count('cleanup_resources') == 1
187+
assert km.get_kernel(kid).call_count('start_kernel') == 1
188+
assert km.get_kernel(kid).call_count('_launch_kernel') == 1
189+
190+
assert km.is_alive(kid)
191+
assert kid in km.list_kernel_ids()
192+
193+
km.get_kernel(kid).reset_counts()
194+
km.reset_counts()
195+
km.interrupt_kernel(kid)
196+
assert km.call_count('interrupt_kernel') == 1
197+
assert km.call_count('get_kernel') == 1
198+
assert km.get_kernel(kid).call_count('interrupt_kernel') == 1
199+
200+
km.get_kernel(kid).reset_counts()
201+
km.reset_counts()
202+
k = km.get_kernel(kid)
203+
assert isinstance(k, SyncKMSubclass)
204+
assert km.call_count('get_kernel') == 1
205+
206+
km.get_kernel(kid).reset_counts()
207+
km.reset_counts()
208+
km.shutdown_all(now=True)
209+
assert km.call_count('shutdown_kernel') == 0
210+
assert km.call_count('remove_kernel') == 1
211+
assert km.call_count('request_shutdown') == 1
212+
assert km.call_count('finish_shutdown') == 1
213+
assert km.call_count('cleanup_resources') == 0
214+
215+
assert kid not in km, f'{kid} not in {km}'
216+
156217

157218
class TestAsyncKernelManager(AsyncTestCase):
158219

@@ -163,6 +224,12 @@ def _get_tcp_km():
163224
km = AsyncMultiKernelManager(config=c)
164225
return km
165226

227+
@staticmethod
228+
def _get_tcp_km_sub():
229+
c = Config()
230+
km = AsyncMKMSubclass(config=c)
231+
return km
232+
166233
# static so picklable for multiprocessing on Windows
167234
@staticmethod
168235
def _get_ipc_km():
@@ -347,3 +414,59 @@ async def test_start_parallel_process_kernels(self):
347414
thread.join()
348415

349416
assert proc.exitcode == 0
417+
418+
@gen_test
419+
async def test_subclass_callables(self):
420+
mkm = self._get_tcp_km_sub()
421+
422+
mkm.reset_counts()
423+
kid = await mkm.start_kernel(stdout=PIPE, stderr=PIPE)
424+
assert mkm.call_count('start_kernel') == 1
425+
assert isinstance(mkm.get_kernel(kid), AsyncKMSubclass)
426+
assert mkm.get_kernel(kid).call_count('start_kernel') == 1
427+
assert mkm.get_kernel(kid).call_count('_launch_kernel') == 1
428+
429+
assert await mkm.is_alive(kid)
430+
assert kid in mkm
431+
assert kid in mkm.list_kernel_ids()
432+
assert len(mkm) == 1, f'{len(mkm)} != {1}'
433+
434+
mkm.get_kernel(kid).reset_counts()
435+
mkm.reset_counts()
436+
await mkm.restart_kernel(kid, now=True)
437+
assert mkm.call_count('restart_kernel') == 1
438+
assert mkm.call_count('get_kernel') == 1
439+
assert mkm.get_kernel(kid).call_count('restart_kernel') == 1
440+
assert mkm.get_kernel(kid).call_count('shutdown_kernel') == 1
441+
assert mkm.get_kernel(kid).call_count('interrupt_kernel') == 1
442+
assert mkm.get_kernel(kid).call_count('_kill_kernel') == 1
443+
assert mkm.get_kernel(kid).call_count('cleanup_resources') == 1
444+
assert mkm.get_kernel(kid).call_count('start_kernel') == 1
445+
assert mkm.get_kernel(kid).call_count('_launch_kernel') == 1
446+
447+
assert await mkm.is_alive(kid)
448+
assert kid in mkm.list_kernel_ids()
449+
450+
mkm.get_kernel(kid).reset_counts()
451+
mkm.reset_counts()
452+
await mkm.interrupt_kernel(kid)
453+
assert mkm.call_count('interrupt_kernel') == 1
454+
assert mkm.call_count('get_kernel') == 1
455+
assert mkm.get_kernel(kid).call_count('interrupt_kernel') == 1
456+
457+
mkm.get_kernel(kid).reset_counts()
458+
mkm.reset_counts()
459+
k = mkm.get_kernel(kid)
460+
assert isinstance(k, AsyncKMSubclass)
461+
assert mkm.call_count('get_kernel') == 1
462+
463+
mkm.get_kernel(kid).reset_counts()
464+
mkm.reset_counts()
465+
await mkm.shutdown_all(now=True)
466+
assert mkm.call_count('shutdown_kernel') == 1
467+
assert mkm.call_count('remove_kernel') == 1
468+
assert mkm.call_count('request_shutdown') == 0
469+
assert mkm.call_count('finish_shutdown') == 0
470+
assert mkm.call_count('cleanup_resources') == 0
471+
472+
assert kid not in mkm, f'{kid} not in {mkm}'

0 commit comments

Comments
 (0)