9
9
from unittest import TestCase
10
10
from tornado .testing import AsyncTestCase , gen_test
11
11
from traitlets .config .loader import Config
12
- from jupyter_client import KernelManager , AsyncKernelManager
12
+ from jupyter_client import KernelManager
13
13
from jupyter_client .multikernelmanager import MultiKernelManager , AsyncMultiKernelManager
14
- from .utils import skip_win32
14
+ from .utils import skip_win32 , SyncMKMSubclass , AsyncMKMSubclass , SyncKMSubclass , AsyncKMSubclass
15
15
from ..localinterfaces import localhost
16
16
17
17
TIMEOUT = 30
@@ -26,6 +26,12 @@ def _get_tcp_km():
26
26
km = MultiKernelManager (config = c )
27
27
return km
28
28
29
+ @staticmethod
30
+ def _get_tcp_km_sub ():
31
+ c = Config ()
32
+ km = SyncMKMSubclass (config = c )
33
+ return km
34
+
29
35
# static so picklable for multiprocessing on Windows
30
36
@staticmethod
31
37
def _get_ipc_km ():
@@ -153,6 +159,61 @@ def test_start_parallel_process_kernels(self):
153
159
154
160
assert proc .exitcode == 0
155
161
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
+
156
217
157
218
class TestAsyncKernelManager (AsyncTestCase ):
158
219
@@ -163,6 +224,12 @@ def _get_tcp_km():
163
224
km = AsyncMultiKernelManager (config = c )
164
225
return km
165
226
227
+ @staticmethod
228
+ def _get_tcp_km_sub ():
229
+ c = Config ()
230
+ km = AsyncMKMSubclass (config = c )
231
+ return km
232
+
166
233
# static so picklable for multiprocessing on Windows
167
234
@staticmethod
168
235
def _get_ipc_km ():
@@ -347,3 +414,59 @@ async def test_start_parallel_process_kernels(self):
347
414
thread .join ()
348
415
349
416
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