-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathtest_client.py
301 lines (241 loc) · 9.75 KB
/
test_client.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""Tests for the KernelClient"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import platform
import sys
from threading import Event
from unittest import TestCase, mock
import pytest
from IPython.utils.capture import capture_output
from traitlets import DottedObjectName, Type
from jupyter_client.client import validate_string_dict
from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel
from jupyter_client.manager import KernelManager, start_new_async_kernel, start_new_kernel
from jupyter_client.threaded import ThreadedKernelClient, ThreadedZMQSocketChannel
TIMEOUT = 30
pjoin = os.path.join
class TestKernelClient(TestCase):
def setUp(self):
try:
KernelSpecManager().get_kernel_spec("echo")
except NoSuchKernel:
pytest.skip()
self.km, self.kc = start_new_kernel(kernel_name="echo")
def tearDown(self):
self.km.shutdown_kernel()
self.kc.stop_channels()
return super().tearDown()
def test_execute_interactive(self):
kc = self.kc
with capture_output() as io:
reply = kc.execute_interactive("print('hello')", timeout=TIMEOUT)
assert "hello" in io.stdout
assert reply["content"]["status"] == "ok"
def _check_reply(self, reply_type, reply):
self.assertIsInstance(reply, dict)
self.assertEqual(reply["header"]["msg_type"], reply_type + "_reply")
self.assertEqual(reply["parent_header"]["msg_type"], reply_type + "_request")
def test_history(self):
kc = self.kc
msg_id = kc.history(session=0)
self.assertIsInstance(msg_id, str)
reply = kc.history(session=0, reply=True, timeout=TIMEOUT)
self._check_reply("history", reply)
def test_inspect(self):
kc = self.kc
msg_id = kc.inspect("who cares")
self.assertIsInstance(msg_id, str)
reply = kc.inspect("code", reply=True, timeout=TIMEOUT)
self._check_reply("inspect", reply)
def test_complete(self):
kc = self.kc
msg_id = kc.complete("who cares")
self.assertIsInstance(msg_id, str)
reply = kc.complete("code", reply=True, timeout=TIMEOUT)
self._check_reply("complete", reply)
def test_kernel_info(self):
kc = self.kc
msg_id = kc.kernel_info()
self.assertIsInstance(msg_id, str)
reply = kc.kernel_info(reply=True, timeout=TIMEOUT)
self._check_reply("kernel_info", reply)
def test_comm_info(self):
kc = self.kc
msg_id = kc.comm_info()
self.assertIsInstance(msg_id, str)
reply = kc.comm_info(reply=True, timeout=TIMEOUT)
self._check_reply("comm_info", reply)
def test_shutdown(self):
kc = self.kc
reply = kc.shutdown(reply=True, timeout=TIMEOUT)
self._check_reply("shutdown", reply)
def test_shutdown_id(self):
kc = self.kc
msg_id = kc.shutdown()
self.assertIsInstance(msg_id, str)
@pytest.fixture
def kc(jp_asyncio_loop):
try:
KernelSpecManager().get_kernel_spec("echo")
except NoSuchKernel:
pytest.skip()
async def start():
return await start_new_async_kernel(kernel_name="echo")
km, kc = jp_asyncio_loop.run_until_complete(start())
yield kc
async def stop():
await km.shutdown_kernel()
jp_asyncio_loop.run_until_complete(stop())
kc.stop_channels()
class TestAsyncKernelClient:
async def test_execute_interactive(self, kc):
reply = await kc.execute_interactive("hello", timeout=TIMEOUT)
assert reply["content"]["status"] == "ok"
def _check_reply(self, reply_type, reply):
assert isinstance(reply, dict)
assert reply["header"]["msg_type"] == reply_type + "_reply"
assert reply["parent_header"]["msg_type"] == reply_type + "_request"
@pytest.mark.skipif(
sys.platform != "linux" or platform.python_implementation().lower() == "pypy",
reason="only works with cpython on ubuntu in ci",
)
async def test_input_request(self, kc):
with mock.patch("builtins.input", return_value="test\n"):
reply = await kc.execute_interactive("a = input()", timeout=TIMEOUT)
assert reply["content"]["status"] == "ok"
async def test_output_hook(self, kc):
called = False
def output_hook(msg):
nonlocal called
if msg["header"]["msg_type"] == "stream":
called = True
reply = await kc.execute_interactive(
"print('hello')", timeout=TIMEOUT, output_hook=output_hook
)
assert reply["content"]["status"] == "ok"
assert called
async def test_history(self, kc):
msg_id = kc.history(session=0)
assert isinstance(msg_id, str)
reply = await kc.history(session=0, reply=True, timeout=TIMEOUT)
self._check_reply("history", reply)
async def test_inspect(self, kc):
msg_id = kc.inspect("who cares")
assert isinstance(msg_id, str)
reply = await kc.inspect("code", reply=True, timeout=TIMEOUT)
self._check_reply("inspect", reply)
async def test_complete(self, kc):
msg_id = kc.complete("who cares")
assert isinstance(msg_id, str)
reply = await kc.complete("code", reply=True, timeout=TIMEOUT)
self._check_reply("complete", reply)
async def test_is_complete(self, kc):
msg_id = kc.is_complete("who cares")
assert isinstance(msg_id, str)
reply = await kc.is_complete("code", reply=True, timeout=TIMEOUT)
self._check_reply("is_complete", reply)
async def test_kernel_info(self, kc):
msg_id = kc.kernel_info()
assert isinstance(msg_id, str)
reply = await kc.kernel_info(reply=True, timeout=TIMEOUT)
self._check_reply("kernel_info", reply)
async def test_comm_info(self, kc):
msg_id = kc.comm_info()
assert isinstance(msg_id, str)
reply = await kc.comm_info(reply=True, timeout=TIMEOUT)
self._check_reply("comm_info", reply)
async def test_shutdown(self, kc):
reply = await kc.shutdown(reply=True, timeout=TIMEOUT)
self._check_reply("shutdown", reply)
async def test_shutdown_id(self, kc):
msg_id = kc.shutdown()
assert isinstance(msg_id, str)
class ThreadedKernelManager(KernelManager):
client_class = DottedObjectName("tests.test_client.CustomThreadedKernelClient")
class CustomThreadedZMQSocketChannel(ThreadedZMQSocketChannel):
last_msg = None
def __init__(self, *args, **kwargs):
self.msg_recv = Event()
super().__init__(*args, **kwargs)
def call_handlers(self, msg):
self.last_msg = msg
self.msg_recv.set()
class CustomThreadedKernelClient(ThreadedKernelClient):
iopub_channel_class = Type(CustomThreadedZMQSocketChannel) # type:ignore[arg-type]
shell_channel_class = Type(CustomThreadedZMQSocketChannel) # type:ignore[arg-type]
stdin_channel_class = Type(CustomThreadedZMQSocketChannel) # type:ignore[arg-type]
control_channel_class = Type(CustomThreadedZMQSocketChannel) # type:ignore[arg-type]
class TestThreadedKernelClient(TestKernelClient):
def setUp(self):
try:
KernelSpecManager().get_kernel_spec("echo")
except NoSuchKernel:
pytest.skip()
self.km = km = ThreadedKernelManager(kernel_name="echo")
km.start_kernel()
self.kc = kc = km.client()
kc.start_channels()
def tearDown(self):
self.km.shutdown_kernel()
self.kc.stop_channels()
def _check_reply(self, reply_type, reply):
self.assertIsInstance(reply, dict)
self.assertEqual(reply["header"]["msg_type"], reply_type + "_reply")
self.assertEqual(reply["parent_header"]["msg_type"], reply_type + "_request")
def test_execute_interactive(self):
pytest.skip("Not supported")
def test_history(self):
kc = self.kc
msg_id = kc.history(session=0)
self.assertIsInstance(msg_id, str)
kc.history(session=0)
kc.shell_channel.msg_recv.wait()
reply = kc.shell_channel.last_msg
self._check_reply("history", reply)
def test_inspect(self):
kc = self.kc
msg_id = kc.inspect("who cares")
self.assertIsInstance(msg_id, str)
kc.inspect("code")
kc.shell_channel.msg_recv.wait()
reply = kc.shell_channel.last_msg
self._check_reply("inspect", reply)
def test_complete(self):
kc = self.kc
msg_id = kc.complete("who cares")
self.assertIsInstance(msg_id, str)
kc.complete("code")
kc.shell_channel.msg_recv.wait()
reply = kc.shell_channel.last_msg
self._check_reply("complete", reply)
def test_kernel_info(self):
kc = self.kc
msg_id = kc.kernel_info()
self.assertIsInstance(msg_id, str)
kc.kernel_info()
kc.shell_channel.msg_recv.wait()
reply = kc.shell_channel.last_msg
self._check_reply("kernel_info", reply)
def test_comm_info(self):
kc = self.kc
msg_id = kc.comm_info()
self.assertIsInstance(msg_id, str)
kc.shell_channel.msg_recv.wait()
reply = kc.shell_channel.last_msg
self._check_reply("comm_info", reply)
def test_shutdown(self):
kc = self.kc
kc.shutdown()
kc.control_channel.msg_recv.wait()
reply = kc.control_channel.last_msg
self._check_reply("shutdown", reply)
def test_shutdown_id(self):
kc = self.kc
msg_id = kc.shutdown()
self.assertIsInstance(msg_id, str)
def test_validate_string_dict():
with pytest.raises(ValueError):
validate_string_dict(dict(a=1)) # type:ignore
with pytest.raises(ValueError):
validate_string_dict({1: "a"}) # type:ignore