-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathtest_io.py
255 lines (218 loc) · 7.52 KB
/
test_io.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
"""Test IO capturing functionality"""
import io
import os
import subprocess
import sys
import threading
import time
import warnings
from concurrent.futures import Future, ThreadPoolExecutor
from unittest import mock
import pytest
import zmq
import zmq.asyncio
from jupyter_client.session import Session
from ipykernel.iostream import _PARENT, BackgroundSocket, IOPubThread, OutStream
@pytest.fixture()
def ctx():
ctx = zmq.asyncio.Context()
yield ctx
ctx.destroy()
@pytest.fixture()
def iopub_thread(ctx):
with ctx.socket(zmq.PUB) as pub:
thread = IOPubThread(pub)
thread.start()
yield thread
thread.stop()
thread.close()
def test_io_api(iopub_thread):
"""Test that wrapped stdout has the same API as a normal TextIO object"""
session = Session()
stream = OutStream(session, iopub_thread, "stdout")
assert stream.errors is None
assert not stream.isatty()
with pytest.raises(io.UnsupportedOperation):
stream.detach()
with pytest.raises(io.UnsupportedOperation):
next(stream)
with pytest.raises(io.UnsupportedOperation):
stream.read()
with pytest.raises(io.UnsupportedOperation):
stream.readline()
with pytest.raises(io.UnsupportedOperation):
stream.seek(0)
with pytest.raises(io.UnsupportedOperation):
stream.tell()
with pytest.raises(TypeError):
stream.write(b"") # type:ignore
def test_io_isatty(iopub_thread):
session = Session()
stream = OutStream(session, iopub_thread, "stdout", isatty=True)
assert stream.isatty()
async def test_io_thread(anyio_backend, iopub_thread):
thread = iopub_thread
thread._setup_pipe_in()
msg = [thread._pipe_uuid, b"a"]
await thread._handle_pipe_msg(msg)
ctx1, pipe = thread._setup_pipe_out()
pipe.close()
thread._pipe_in1.close()
thread._check_mp_mode = lambda: _PARENT
thread._really_send([b"hi"])
ctx1.destroy()
thread.stop()
thread.close()
thread._really_send(None)
async def test_background_socket(anyio_backend, iopub_thread):
sock = BackgroundSocket(iopub_thread)
assert sock.__class__ == BackgroundSocket
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
sock.linger = 101
assert iopub_thread.socket.linger == 101
assert sock.io_thread == iopub_thread
sock.send(b"hi")
async def test_outstream(anyio_backend, iopub_thread):
session = Session()
pub = iopub_thread.socket
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
stream = OutStream(session, pub, "stdout")
stream.close()
stream = OutStream(session, iopub_thread, "stdout", pipe=object())
stream.close()
stream = OutStream(session, iopub_thread, "stdout", watchfd=False)
stream.close()
stream = OutStream(session, iopub_thread, "stdout", isatty=True, echo=io.StringIO())
with stream:
with pytest.raises(io.UnsupportedOperation):
stream.fileno()
stream._watch_pipe_fd()
stream.flush()
stream.write("hi")
stream.writelines(["ab", "cd"])
assert stream.writable()
@pytest.mark.anyio()
async def test_event_pipe_gc(iopub_thread):
session = Session(key=b"abc")
stream = OutStream(
session,
iopub_thread,
"stdout",
isatty=True,
watchfd=False,
)
assert iopub_thread._event_pipes == {}
with stream, mock.patch.object(sys, "stdout", stream), ThreadPoolExecutor(1) as pool:
pool.submit(print, "x").result()
pool_thread = pool.submit(threading.current_thread).result()
threads = list(iopub_thread._event_pipes)
assert threads[0] == pool_thread
# run gc once in the iopub thread
f: Future = Future()
try:
await iopub_thread._event_pipe_gc()
except Exception as e:
f.set_exception(e)
else:
f.set_result(None)
# wait for call to finish in iopub thread
f.result()
# assert iopub_thread._event_pipes == {}
def subprocess_test_echo_watch():
# handshake Pub subscription
session = Session(key=b"abc")
# use PUSH socket to avoid subscription issues
with zmq.asyncio.Context() as ctx, ctx.socket(zmq.PUSH) as pub:
pub.connect(os.environ["IOPUB_URL"])
iopub_thread = IOPubThread(pub)
iopub_thread.start()
stdout_fd = sys.stdout.fileno()
sys.stdout.flush()
stream = OutStream(
session,
iopub_thread,
"stdout",
isatty=True,
echo=sys.stdout,
watchfd="force",
)
save_stdout = sys.stdout
with stream, mock.patch.object(sys, "stdout", stream):
# write to low-level FD
os.write(stdout_fd, b"fd\n")
# print (writes to stream)
print("print\n", end="")
sys.stdout.flush()
# write to unwrapped __stdout__ (should also go to original FD)
sys.__stdout__.write("__stdout__\n")
sys.__stdout__.flush()
# write to original sys.stdout (should be the same as __stdout__)
save_stdout.write("stdout\n")
save_stdout.flush()
# is there another way to flush on the FD?
fd_file = os.fdopen(stdout_fd, "w")
fd_file.flush()
# we don't have a sync flush on _reading_ from the watched pipe
time.sleep(1)
stream.flush()
iopub_thread.stop()
iopub_thread.close()
@pytest.mark.anyio()
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Windows")
async def test_echo_watch(ctx):
"""Test echo on underlying FD while capturing the same FD
Test runs in a subprocess to avoid messing with pytest output capturing.
"""
s = ctx.socket(zmq.PULL)
port = s.bind_to_random_port("tcp://127.0.0.1")
url = f"tcp://127.0.0.1:{port}"
session = Session(key=b"abc")
stdout_chunks = []
with s:
env = dict(os.environ)
env["IOPUB_URL"] = url
env["PYTHONUNBUFFERED"] = "1"
env.pop("PYTEST_CURRENT_TEST", None)
p = subprocess.run(
[
sys.executable,
"-c",
f"import {__name__}; {__name__}.subprocess_test_echo_watch()",
],
env=env,
capture_output=True,
check=True,
text=True,
timeout=10,
)
print(f"{p.stdout=}")
print(f"{p.stderr}=", file=sys.stderr)
assert p.returncode == 0
while await s.poll(timeout=100):
msg = await s.recv_multipart()
ident, msg = session.feed_identities(msg, copy=True)
msg = session.deserialize(msg, content=True, copy=True)
assert msg is not None # for type narrowing
if msg["header"]["msg_type"] == "stream" and msg["content"]["name"] == "stdout":
stdout_chunks.append(msg["content"]["text"])
# check outputs
# use sets of lines to ignore ordering issues with
# async flush and watchfd thread
# Check the stream output forwarded over zmq
zmq_stdout = "".join(stdout_chunks)
assert set(zmq_stdout.strip().splitlines()) == {
"fd",
"print",
"stdout",
"__stdout__",
}
# Check what was written to the process stdout (kernel terminal)
# just check that each output source went to the terminal
assert set(p.stdout.strip().splitlines()) == {
"fd",
"print",
"stdout",
"__stdout__",
}