| 1 |
# Copyright (c) IPython Development Team. |
| 2 |
# Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from ipykernel.inprocess.manager import InProcessKernelManager |
| 9 |
|
| 10 |
# ----------------------------------------------------------------------------- |
| 11 |
# Test case |
| 12 |
# ----------------------------------------------------------------------------- |
| 13 |
|
| 14 |
|
| 15 |
class InProcessKernelManagerTestCase(unittest.TestCase): |
| 16 |
def setUp(self): |
| 17 |
self.km = InProcessKernelManager() |
| 18 |
|
| 19 |
def tearDown(self): |
| 20 |
if self.km.has_kernel: |
| 21 |
self.km.shutdown_kernel() |
| 22 |
|
| 23 |
@pytest.mark.flaky |
| 24 |
def test_interface(self): |
| 25 |
"""Does the in-process kernel manager implement the basic KM interface?""" |
| 26 |
km = self.km |
| 27 |
assert not km.has_kernel |
| 28 |
|
| 29 |
km.start_kernel() |
| 30 |
assert km.has_kernel |
| 31 |
assert km.kernel is not None |
| 32 |
|
| 33 |
kc = km.client() |
| 34 |
assert not kc.channels_running |
| 35 |
|
| 36 |
kc.start_channels() |
| 37 |
assert kc.channels_running |
| 38 |
|
| 39 |
old_kernel = km.kernel |
| 40 |
km.restart_kernel() |
| 41 |
assert km.kernel is not None |
| 42 |
assert km.kernel != old_kernel |
| 43 |
|
| 44 |
km.shutdown_kernel() |
| 45 |
assert not km.has_kernel |
| 46 |
|
| 47 |
with pytest.raises(NotImplementedError): |
| 48 |
km.interrupt_kernel() |
| 49 |
|
| 50 |
with pytest.raises(NotImplementedError): |
| 51 |
km.signal_kernel(9) |
| 52 |
|
| 53 |
kc.stop_channels() |
| 54 |
assert not kc.channels_running |
| 55 |
|
| 56 |
def test_execute(self): |
| 57 |
"""Does executing code in an in-process kernel work?""" |
| 58 |
km = self.km |
| 59 |
km.start_kernel() |
| 60 |
kc = km.client() |
| 61 |
kc.start_channels() |
| 62 |
kc.wait_for_ready() |
| 63 |
kc.execute("foo = 1") |
| 64 |
assert km.kernel.shell.user_ns["foo"] == 1 |
| 65 |
|
| 66 |
def test_complete(self): |
| 67 |
"""Does requesting completion from an in-process kernel work?""" |
| 68 |
km = self.km |
| 69 |
km.start_kernel() |
| 70 |
kc = km.client() |
| 71 |
kc.start_channels() |
| 72 |
kc.wait_for_ready() |
| 73 |
km.kernel.shell.push({"my_bar": 0, "my_baz": 1}) |
| 74 |
kc.complete("my_ba", 5) |
| 75 |
msg = kc.get_shell_msg() |
| 76 |
assert msg["header"]["msg_type"] == "complete_reply" |
| 77 |
assert sorted(msg["content"]["matches"]) == ["my_bar", "my_baz"] |
| 78 |
|
| 79 |
def test_inspect(self): |
| 80 |
"""Does requesting object information from an in-process kernel work?""" |
| 81 |
km = self.km |
| 82 |
km.start_kernel() |
| 83 |
kc = km.client() |
| 84 |
kc.start_channels() |
| 85 |
kc.wait_for_ready() |
| 86 |
km.kernel.shell.user_ns["foo"] = 1 |
| 87 |
kc.inspect("foo") |
| 88 |
msg = kc.get_shell_msg() |
| 89 |
assert msg["header"]["msg_type"] == "inspect_reply" |
| 90 |
content = msg["content"] |
| 91 |
assert content["found"] |
| 92 |
text = content["data"]["text/plain"] |
| 93 |
assert "int" in text |
| 94 |
|
| 95 |
def test_history(self): |
| 96 |
"""Does requesting history from an in-process kernel work?""" |
| 97 |
km = self.km |
| 98 |
km.start_kernel() |
| 99 |
kc = km.client() |
| 100 |
kc.start_channels() |
| 101 |
kc.wait_for_ready() |
| 102 |
kc.execute("1") |
| 103 |
kc.history(hist_access_type="tail", n=1) |
| 104 |
msg = kc.shell_channel.get_msgs()[-1] |
| 105 |
assert msg["header"]["msg_type"] == "history_reply" |
| 106 |
history = msg["content"]["history"] |
| 107 |
assert len(history) == 1 |
| 108 |
assert history[0][2] == "1" |
| 109 |
|
| 110 |
|
| 111 |
if __name__ == "__main__": |
| 112 |
unittest.main() |