Show More
@@ -1,77 +1,102 b'' | |||||
1 | #------------------------------------------------------------------------------- |
|
1 | #------------------------------------------------------------------------------- | |
2 | # Copyright (C) 2012 The IPython Development Team |
|
2 | # Copyright (C) 2012 The IPython Development Team | |
3 | # |
|
3 | # | |
4 | # Distributed under the terms of the BSD License. The full license is in |
|
4 | # Distributed under the terms of the BSD License. The full license is in | |
5 | # the file COPYING, distributed as part of this software. |
|
5 | # the file COPYING, distributed as part of this software. | |
6 | #------------------------------------------------------------------------------- |
|
6 | #------------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Imports |
|
9 | # Imports | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | from __future__ import print_function |
|
11 | from __future__ import print_function | |
12 |
|
12 | |||
13 | # Standard library imports |
|
13 | # Standard library imports | |
14 | import unittest |
|
14 | import unittest | |
15 |
|
15 | |||
16 | # Local imports |
|
16 | # Local imports | |
17 | from IPython.inprocess.blockingkernelmanager import \ |
|
17 | from IPython.inprocess.blockingkernelmanager import \ | |
18 | BlockingInProcessKernelManager |
|
18 | BlockingInProcessKernelManager | |
19 | from IPython.inprocess.ipkernel import InProcessKernel |
|
19 | from IPython.inprocess.ipkernel import InProcessKernel | |
20 |
|
20 | |||
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Test case |
|
22 | # Test case | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | class InProcessKernelManagerTestCase(unittest.TestCase): |
|
25 | class InProcessKernelManagerTestCase(unittest.TestCase): | |
26 |
|
26 | |||
27 | def test_inteface(self): |
|
27 | def test_inteface(self): | |
28 | """ Does the in-process kernel manager implement the basic KM interface? |
|
28 | """ Does the in-process kernel manager implement the basic KM interface? | |
29 | """ |
|
29 | """ | |
30 | km = BlockingInProcessKernelManager() |
|
30 | km = BlockingInProcessKernelManager() | |
31 | self.assert_(not km.channels_running) |
|
31 | self.assert_(not km.channels_running) | |
32 | self.assert_(not km.has_kernel) |
|
32 | self.assert_(not km.has_kernel) | |
33 |
|
33 | |||
34 | km.start_channels() |
|
34 | km.start_channels() | |
35 | self.assert_(km.channels_running) |
|
35 | self.assert_(km.channels_running) | |
36 |
|
36 | |||
37 | km.start_kernel() |
|
37 | km.start_kernel() | |
38 | self.assert_(km.has_kernel) |
|
38 | self.assert_(km.has_kernel) | |
39 | self.assert_(km.kernel is not None) |
|
39 | self.assert_(km.kernel is not None) | |
40 |
|
40 | |||
41 | old_kernel = km.kernel |
|
41 | old_kernel = km.kernel | |
42 | km.restart_kernel() |
|
42 | km.restart_kernel() | |
43 | self.assert_(km.kernel is not None) |
|
43 | self.assert_(km.kernel is not None) | |
44 | self.assertNotEquals(km.kernel, old_kernel) |
|
44 | self.assertNotEquals(km.kernel, old_kernel) | |
45 |
|
45 | |||
46 | km.shutdown_kernel() |
|
46 | km.shutdown_kernel() | |
47 | self.assert_(not km.has_kernel) |
|
47 | self.assert_(not km.has_kernel) | |
48 |
|
48 | |||
49 | self.assertRaises(NotImplementedError, km.interrupt_kernel) |
|
49 | self.assertRaises(NotImplementedError, km.interrupt_kernel) | |
50 | self.assertRaises(NotImplementedError, km.signal_kernel, 9) |
|
50 | self.assertRaises(NotImplementedError, km.signal_kernel, 9) | |
51 |
|
51 | |||
52 | km.stop_channels() |
|
52 | km.stop_channels() | |
53 | self.assert_(not km.channels_running) |
|
53 | self.assert_(not km.channels_running) | |
54 |
|
54 | |||
55 | def test_execute(self): |
|
55 | def test_execute(self): | |
56 | """ Does executing code in an in-process kernel work? |
|
56 | """ Does executing code in an in-process kernel work? | |
57 | """ |
|
57 | """ | |
58 | km = BlockingInProcessKernelManager() |
|
58 | km = BlockingInProcessKernelManager() | |
59 | km.start_kernel() |
|
59 | km.start_kernel() | |
60 | km.shell_channel.execute('foo = 1') |
|
60 | km.shell_channel.execute('foo = 1') | |
61 | self.assertEquals(km.kernel.shell.user_ns['foo'], 1) |
|
61 | self.assertEquals(km.kernel.shell.user_ns['foo'], 1) | |
62 |
|
62 | |||
|
63 | def test_complete(self): | |||
|
64 | """ Does requesting completion from an in-process kernel work? | |||
|
65 | """ | |||
|
66 | km = BlockingInProcessKernelManager() | |||
|
67 | km.start_kernel() | |||
|
68 | km.kernel.shell.push({'my_bar': 0, 'my_baz': 1}) | |||
|
69 | km.shell_channel.complete('my_ba', 'my_ba', 5) | |||
|
70 | msg = km.shell_channel.get_msg() | |||
|
71 | self.assertEquals(msg['header']['msg_type'], 'complete_reply') | |||
|
72 | self.assertEquals(sorted(msg['content']['matches']), | |||
|
73 | ['my_bar', 'my_baz']) | |||
|
74 | ||||
63 | def test_object_info(self): |
|
75 | def test_object_info(self): | |
64 | """ Does requesting object information from an in-process kernel work? |
|
76 | """ Does requesting object information from an in-process kernel work? | |
65 | """ |
|
77 | """ | |
66 | km = BlockingInProcessKernelManager() |
|
78 | km = BlockingInProcessKernelManager() | |
67 | km.start_kernel() |
|
79 | km.start_kernel() | |
68 | km.kernel.shell.user_ns['foo'] = 1 |
|
80 | km.kernel.shell.user_ns['foo'] = 1 | |
69 | km.shell_channel.object_info('foo') |
|
81 | km.shell_channel.object_info('foo') | |
70 | msg = km.shell_channel.get_msg() |
|
82 | msg = km.shell_channel.get_msg() | |
71 | self.assertEquals(msg['header']['msg_type'], 'object_info_reply') |
|
83 | self.assertEquals(msg['header']['msg_type'], 'object_info_reply') | |
72 | self.assertEquals(msg['content']['name'], 'foo') |
|
84 | self.assertEquals(msg['content']['name'], 'foo') | |
73 | self.assertEquals(msg['content']['type_name'], 'int') |
|
85 | self.assertEquals(msg['content']['type_name'], 'int') | |
74 |
|
86 | |||
|
87 | def test_history(self): | |||
|
88 | """ Does requesting history from an in-process kernel work? | |||
|
89 | """ | |||
|
90 | km = BlockingInProcessKernelManager() | |||
|
91 | km.start_kernel() | |||
|
92 | km.shell_channel.execute('%who') | |||
|
93 | km.shell_channel.history(hist_access_type='tail', n=1) | |||
|
94 | msg = km.shell_channel.get_msgs()[-1] | |||
|
95 | self.assertEquals(msg['header']['msg_type'], 'history_reply') | |||
|
96 | history = msg['content']['history'] | |||
|
97 | self.assertEquals(len(history), 1) | |||
|
98 | self.assertEquals(history[0][2], '%who') | |||
|
99 | ||||
75 |
|
100 | |||
76 | if __name__ == '__main__': |
|
101 | if __name__ == '__main__': | |
77 | unittest.main() |
|
102 | unittest.main() |
General Comments 0
You need to be logged in to leave comments.
Login now