Show More
@@ -1,78 +1,87 b'' | |||||
1 | """Tests for the notebook kernel and session manager.""" |
|
1 | """Tests for the notebook kernel and session manager.""" | |
2 |
|
2 | |||
3 | from subprocess import PIPE |
|
3 | from subprocess import PIPE | |
4 | import time |
|
4 | import time | |
5 | from unittest import TestCase |
|
5 | from unittest import TestCase | |
6 |
|
6 | |||
7 | from IPython.testing import decorators as dec |
|
7 | from IPython.testing import decorators as dec | |
8 |
|
8 | |||
9 | from IPython.config.loader import Config |
|
9 | from IPython.config.loader import Config | |
10 | from IPython.utils.localinterfaces import localhost |
|
10 | from IPython.utils.localinterfaces import localhost | |
11 | from IPython.kernel import KernelManager |
|
11 | from IPython.kernel import KernelManager | |
12 | from IPython.kernel.multikernelmanager import MultiKernelManager |
|
12 | from IPython.kernel.multikernelmanager import MultiKernelManager | |
13 |
|
13 | |||
14 | class TestKernelManager(TestCase): |
|
14 | class TestKernelManager(TestCase): | |
15 |
|
15 | |||
16 | def _get_tcp_km(self): |
|
16 | def _get_tcp_km(self): | |
17 | c = Config() |
|
17 | c = Config() | |
18 | km = MultiKernelManager(config=c) |
|
18 | km = MultiKernelManager(config=c) | |
19 | return km |
|
19 | return km | |
20 |
|
20 | |||
21 | def _get_ipc_km(self): |
|
21 | def _get_ipc_km(self): | |
22 | c = Config() |
|
22 | c = Config() | |
23 | c.KernelManager.transport = 'ipc' |
|
23 | c.KernelManager.transport = 'ipc' | |
24 | c.KernelManager.ip = 'test' |
|
24 | c.KernelManager.ip = 'test' | |
25 | km = MultiKernelManager(config=c) |
|
25 | km = MultiKernelManager(config=c) | |
26 | return km |
|
26 | return km | |
27 |
|
27 | |||
28 | def _run_lifecycle(self, km): |
|
28 | def _run_lifecycle(self, km): | |
29 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) |
|
29 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) | |
30 | self.assertTrue(km.is_alive(kid)) |
|
30 | self.assertTrue(km.is_alive(kid)) | |
31 | self.assertTrue(kid in km) |
|
31 | self.assertTrue(kid in km) | |
32 | self.assertTrue(kid in km.list_kernel_ids()) |
|
32 | self.assertTrue(kid in km.list_kernel_ids()) | |
33 | self.assertEqual(len(km),1) |
|
33 | self.assertEqual(len(km),1) | |
34 | km.restart_kernel(kid, now=True) |
|
34 | km.restart_kernel(kid, now=True) | |
35 | self.assertTrue(km.is_alive(kid)) |
|
35 | self.assertTrue(km.is_alive(kid)) | |
36 | self.assertTrue(kid in km.list_kernel_ids()) |
|
36 | self.assertTrue(kid in km.list_kernel_ids()) | |
37 | km.interrupt_kernel(kid) |
|
37 | km.interrupt_kernel(kid) | |
38 | k = km.get_kernel(kid) |
|
38 | k = km.get_kernel(kid) | |
39 | self.assertTrue(isinstance(k, KernelManager)) |
|
39 | self.assertTrue(isinstance(k, KernelManager)) | |
40 | km.shutdown_kernel(kid, now=True) |
|
40 | km.shutdown_kernel(kid, now=True) | |
41 | self.assertTrue(not kid in km) |
|
41 | self.assertTrue(not kid in km) | |
42 |
|
42 | |||
43 | def _run_cinfo(self, km, transport, ip): |
|
43 | def _run_cinfo(self, km, transport, ip): | |
44 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) |
|
44 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) | |
45 | k = km.get_kernel(kid) |
|
45 | k = km.get_kernel(kid) | |
46 | cinfo = km.get_connection_info(kid) |
|
46 | cinfo = km.get_connection_info(kid) | |
47 | self.assertEqual(transport, cinfo['transport']) |
|
47 | self.assertEqual(transport, cinfo['transport']) | |
48 | self.assertEqual(ip, cinfo['ip']) |
|
48 | self.assertEqual(ip, cinfo['ip']) | |
49 | self.assertTrue('stdin_port' in cinfo) |
|
49 | self.assertTrue('stdin_port' in cinfo) | |
50 | self.assertTrue('iopub_port' in cinfo) |
|
50 | self.assertTrue('iopub_port' in cinfo) | |
51 | stream = km.connect_iopub(kid) |
|
51 | stream = km.connect_iopub(kid) | |
52 | stream.close() |
|
52 | stream.close() | |
53 | self.assertTrue('shell_port' in cinfo) |
|
53 | self.assertTrue('shell_port' in cinfo) | |
54 | stream = km.connect_shell(kid) |
|
54 | stream = km.connect_shell(kid) | |
55 | stream.close() |
|
55 | stream.close() | |
56 | self.assertTrue('hb_port' in cinfo) |
|
56 | self.assertTrue('hb_port' in cinfo) | |
57 | stream = km.connect_hb(kid) |
|
57 | stream = km.connect_hb(kid) | |
58 | stream.close() |
|
58 | stream.close() | |
59 | km.shutdown_kernel(kid, now=True) |
|
59 | km.shutdown_kernel(kid, now=True) | |
60 |
|
60 | |||
61 | def test_tcp_lifecycle(self): |
|
61 | def test_tcp_lifecycle(self): | |
62 | km = self._get_tcp_km() |
|
62 | km = self._get_tcp_km() | |
63 | self._run_lifecycle(km) |
|
63 | self._run_lifecycle(km) | |
64 |
|
64 | |||
|
65 | def test_shutdown_all(self): | |||
|
66 | km = self._get_tcp_km() | |||
|
67 | kid = km.start_kernel(stdout=PIPE, stderr=PIPE) | |||
|
68 | self.assertIn(kid, km) | |||
|
69 | km.shutdown_all() | |||
|
70 | self.assertNotIn(kid, km) | |||
|
71 | # shutdown again is okay, because we have no kernels | |||
|
72 | km.shutdown_all() | |||
|
73 | ||||
65 | def test_tcp_cinfo(self): |
|
74 | def test_tcp_cinfo(self): | |
66 | km = self._get_tcp_km() |
|
75 | km = self._get_tcp_km() | |
67 | self._run_cinfo(km, 'tcp', localhost()) |
|
76 | self._run_cinfo(km, 'tcp', localhost()) | |
68 |
|
77 | |||
69 | @dec.skip_win32 |
|
78 | @dec.skip_win32 | |
70 | def test_ipc_lifecycle(self): |
|
79 | def test_ipc_lifecycle(self): | |
71 | km = self._get_ipc_km() |
|
80 | km = self._get_ipc_km() | |
72 | self._run_lifecycle(km) |
|
81 | self._run_lifecycle(km) | |
73 |
|
82 | |||
74 | @dec.skip_win32 |
|
83 | @dec.skip_win32 | |
75 | def test_ipc_cinfo(self): |
|
84 | def test_ipc_cinfo(self): | |
76 | km = self._get_ipc_km() |
|
85 | km = self._get_ipc_km() | |
77 | self._run_cinfo(km, 'ipc', 'test') |
|
86 | self._run_cinfo(km, 'ipc', 'test') | |
78 |
|
87 |
General Comments 0
You need to be logged in to leave comments.
Login now