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