test_multikernelmanager.py
78 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r4344 | """Tests for the notebook kernel and session manager.""" | ||
Brian E. Granger
|
r4343 | |||
Brian E. Granger
|
r9132 | from subprocess import PIPE | ||
Brian E. Granger
|
r9131 | import time | ||
Brian E. Granger
|
r4343 | from unittest import TestCase | ||
MinRK
|
r9156 | from IPython.testing import decorators as dec | ||
Brian E. Granger
|
r9116 | from IPython.config.loader import Config | ||
MinRK
|
r12591 | from IPython.utils.localinterfaces import localhost | ||
MinRK
|
r10284 | from IPython.kernel import KernelManager | ||
MinRK
|
r9371 | from IPython.kernel.multikernelmanager import MultiKernelManager | ||
Brian E. Granger
|
r4343 | |||
class TestKernelManager(TestCase): | ||||
Brian E. Granger
|
r9116 | def _get_tcp_km(self): | ||
Brian Granger
|
r10282 | c = Config() | ||
km = MultiKernelManager(config=c) | ||||
return km | ||||
Brian E. Granger
|
r9116 | |||
def _get_ipc_km(self): | ||||
c = Config() | ||||
c.KernelManager.transport = 'ipc' | ||||
c.KernelManager.ip = 'test' | ||||
km = MultiKernelManager(config=c) | ||||
return km | ||||
def _run_lifecycle(self, km): | ||||
Brian E. Granger
|
r9132 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) | ||
Brian E. Granger
|
r10275 | self.assertTrue(km.is_alive(kid)) | ||
Bradley M. Froehle
|
r7876 | self.assertTrue(kid in km) | ||
Brian E. Granger
|
r9112 | self.assertTrue(kid in km.list_kernel_ids()) | ||
Bradley M. Froehle
|
r7874 | self.assertEqual(len(km),1) | ||
MinRK
|
r12416 | km.restart_kernel(kid, now=True) | ||
Brian E. Granger
|
r10275 | self.assertTrue(km.is_alive(kid)) | ||
Brian E. Granger
|
r9113 | self.assertTrue(kid in km.list_kernel_ids()) | ||
Brian E. Granger
|
r9112 | km.interrupt_kernel(kid) | ||
Brian E. Granger
|
r9116 | k = km.get_kernel(kid) | ||
self.assertTrue(isinstance(k, KernelManager)) | ||||
MinRK
|
r12416 | km.shutdown_kernel(kid, now=True) | ||
Bradley M. Froehle
|
r7876 | self.assertTrue(not kid in km) | ||
Brian E. Granger
|
r4343 | |||
Brian Granger
|
r9124 | def _run_cinfo(self, km, transport, ip): | ||
Brian E. Granger
|
r9132 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) | ||
Brian E. Granger
|
r9116 | k = km.get_kernel(kid) | ||
cinfo = km.get_connection_info(kid) | ||||
Brian Granger
|
r9124 | self.assertEqual(transport, cinfo['transport']) | ||
self.assertEqual(ip, cinfo['ip']) | ||||
Brian E. Granger
|
r9116 | self.assertTrue('stdin_port' in cinfo) | ||
self.assertTrue('iopub_port' in cinfo) | ||||
MinRK
|
r10295 | stream = km.connect_iopub(kid) | ||
MinRK
|
r9158 | stream.close() | ||
Brian E. Granger
|
r9116 | self.assertTrue('shell_port' in cinfo) | ||
MinRK
|
r10295 | stream = km.connect_shell(kid) | ||
MinRK
|
r9158 | stream.close() | ||
Brian E. Granger
|
r9116 | self.assertTrue('hb_port' in cinfo) | ||
MinRK
|
r10295 | stream = km.connect_hb(kid) | ||
MinRK
|
r9158 | stream.close() | ||
MinRK
|
r12416 | km.shutdown_kernel(kid, now=True) | ||
Brian E. Granger
|
r4343 | |||
Brian E. Granger
|
r9131 | def test_tcp_lifecycle(self): | ||
Brian Granger
|
r9124 | km = self._get_tcp_km() | ||
self._run_lifecycle(km) | ||||
def test_tcp_cinfo(self): | ||||
km = self._get_tcp_km() | ||||
MinRK
|
r12591 | self._run_cinfo(km, 'tcp', localhost()) | ||
Brian Granger
|
r9124 | |||
MinRK
|
r9518 | @dec.skip_win32 | ||
Brian E. Granger
|
r9131 | def test_ipc_lifecycle(self): | ||
Brian Granger
|
r9124 | km = self._get_ipc_km() | ||
self._run_lifecycle(km) | ||||
MinRK
|
r9518 | @dec.skip_win32 | ||
Brian E. Granger
|
r9116 | def test_ipc_cinfo(self): | ||
km = self._get_ipc_km() | ||||
Brian Granger
|
r9124 | self._run_cinfo(km, 'ipc', 'test') | ||
Brian E. Granger
|
r9131 | |||