test_kernelmanager.py
112 lines
| 4.0 KiB
| text/x-python
|
PythonLexer
epatters
|
r8475 | #------------------------------------------------------------------------------- | ||
# Copyright (C) 2012 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#------------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
from __future__ import print_function | ||||
# Standard library imports | ||||
import unittest | ||||
# Local imports | ||||
MinRK
|
r10298 | from IPython.kernel.inprocess.blocking import BlockingInProcessKernelClient | ||
MinRK
|
r9375 | from IPython.kernel.inprocess.ipkernel import InProcessKernel | ||
MinRK
|
r10298 | from IPython.kernel.inprocess.manager import InProcessKernelManager | ||
epatters
|
r8475 | |||
#----------------------------------------------------------------------------- | ||||
# Test case | ||||
#----------------------------------------------------------------------------- | ||||
class InProcessKernelManagerTestCase(unittest.TestCase): | ||||
MinRK
|
r10298 | def test_interface(self): | ||
epatters
|
r8475 | """ Does the in-process kernel manager implement the basic KM interface? | ||
""" | ||||
MinRK
|
r10298 | km = InProcessKernelManager() | ||
epatters
|
r8475 | self.assert_(not km.has_kernel) | ||
km.start_kernel() | ||||
self.assert_(km.has_kernel) | ||||
self.assert_(km.kernel is not None) | ||||
MinRK
|
r10298 | kc = BlockingInProcessKernelClient(kernel=km.kernel) | ||
self.assert_(not kc.channels_running) | ||||
kc.start_channels() | ||||
self.assert_(kc.channels_running) | ||||
epatters
|
r8475 | old_kernel = km.kernel | ||
km.restart_kernel() | ||||
self.assert_(km.kernel is not None) | ||||
self.assertNotEquals(km.kernel, old_kernel) | ||||
km.shutdown_kernel() | ||||
self.assert_(not km.has_kernel) | ||||
self.assertRaises(NotImplementedError, km.interrupt_kernel) | ||||
self.assertRaises(NotImplementedError, km.signal_kernel, 9) | ||||
MinRK
|
r10298 | kc.stop_channels() | ||
self.assert_(not kc.channels_running) | ||||
epatters
|
r8475 | |||
def test_execute(self): | ||||
""" Does executing code in an in-process kernel work? | ||||
""" | ||||
MinRK
|
r10298 | km = InProcessKernelManager() | ||
epatters
|
r8475 | km.start_kernel() | ||
MinRK
|
r10298 | kc = BlockingInProcessKernelClient(kernel=km.kernel) | ||
kc.start_channels() | ||||
kc.execute('foo = 1') | ||||
epatters
|
r8475 | self.assertEquals(km.kernel.shell.user_ns['foo'], 1) | ||
epatters
|
r8481 | def test_complete(self): | ||
""" Does requesting completion from an in-process kernel work? | ||||
""" | ||||
MinRK
|
r10298 | km = InProcessKernelManager() | ||
epatters
|
r8481 | km.start_kernel() | ||
MinRK
|
r10298 | kc = BlockingInProcessKernelClient(kernel=km.kernel) | ||
kc.start_channels() | ||||
epatters
|
r8481 | km.kernel.shell.push({'my_bar': 0, 'my_baz': 1}) | ||
MinRK
|
r10298 | kc.complete('my_ba', 'my_ba', 5) | ||
msg = kc.get_shell_msg() | ||||
self.assertEqual(msg['header']['msg_type'], 'complete_reply') | ||||
self.assertEqual(sorted(msg['content']['matches']), | ||||
epatters
|
r8481 | ['my_bar', 'my_baz']) | ||
epatters
|
r8475 | def test_object_info(self): | ||
""" Does requesting object information from an in-process kernel work? | ||||
""" | ||||
MinRK
|
r10298 | km = InProcessKernelManager() | ||
epatters
|
r8475 | km.start_kernel() | ||
MinRK
|
r10298 | kc = BlockingInProcessKernelClient(kernel=km.kernel) | ||
kc.start_channels() | ||||
epatters
|
r8475 | km.kernel.shell.user_ns['foo'] = 1 | ||
MinRK
|
r10298 | kc.object_info('foo') | ||
msg = kc.get_shell_msg() | ||||
epatters
|
r8475 | self.assertEquals(msg['header']['msg_type'], 'object_info_reply') | ||
self.assertEquals(msg['content']['name'], 'foo') | ||||
self.assertEquals(msg['content']['type_name'], 'int') | ||||
epatters
|
r8481 | def test_history(self): | ||
""" Does requesting history from an in-process kernel work? | ||||
""" | ||||
MinRK
|
r10298 | km = InProcessKernelManager() | ||
epatters
|
r8481 | km.start_kernel() | ||
MinRK
|
r10298 | kc = BlockingInProcessKernelClient(kernel=km.kernel) | ||
kc.start_channels() | ||||
kc.execute('%who') | ||||
kc.history(hist_access_type='tail', n=1) | ||||
msg = kc.shell_channel.get_msgs()[-1] | ||||
epatters
|
r8481 | self.assertEquals(msg['header']['msg_type'], 'history_reply') | ||
history = msg['content']['history'] | ||||
self.assertEquals(len(history), 1) | ||||
self.assertEquals(history[0][2], '%who') | ||||
epatters
|
r8475 | |||
if __name__ == '__main__': | ||||
unittest.main() | ||||