-
-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathconftest.py
More file actions
255 lines (202 loc) · 7.37 KB
/
conftest.py
File metadata and controls
255 lines (202 loc) · 7.37 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
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
import asyncio
import logging
import os
import warnings
from math import inf
from typing import Any, Callable, no_type_check
from unittest.mock import MagicMock
import pytest
import zmq
import zmq.asyncio
from anyio import create_memory_object_stream, create_task_group
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from IPython.core.history import HistoryManager
from jupyter_client.session import Session
from ipykernel.ipkernel import IPythonKernel
from ipykernel.kernelbase import Kernel
from ipykernel.zmqshell import ZMQInteractiveShell
try:
import resource
except ImportError:
# Windows
resource = None # type:ignore
try:
import tracemalloc
except ModuleNotFoundError:
tracemalloc = None
# ensure we don't leak history managers
HistoryManager._max_inst = 1
@pytest.fixture()
def anyio_backend():
return "asyncio"
pytestmark = pytest.mark.anyio
# Handle resource limit
# Ensure a minimal soft limit of DEFAULT_SOFT if the current hard limit is at least that much.
if resource is not None:
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
DEFAULT_SOFT = 4096
if hard >= DEFAULT_SOFT:
soft = DEFAULT_SOFT
if hard < soft:
hard = soft
resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))
# Enforce selector event loop on Windows.
if os.name == "nt":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type:ignore
class TestSession(Session):
"""A session that copies sent messages to an internal stream, so that
they can be accessed later.
"""
def __init__(self, sockets, *args, **kwargs):
super().__init__(*args, **kwargs)
self._streams = {}
for socket in sockets:
send_stream, receive_stream = create_memory_object_stream(max_buffer_size=inf)
self._streams[socket] = {"send": send_stream, "receive": receive_stream}
def close(self):
for streams in self._streams.values():
for stream in streams.values():
stream.close()
self._streams.clear()
def send(self, socket, *args, **kwargs):
msg = super().send(socket, *args, **kwargs)
send_stream: MemoryObjectSendStream[Any] = self._streams[socket]["send"]
send_stream.send_nowait(msg)
return msg
class KernelMixin:
shell_socket: zmq.asyncio.Socket
control_socket: zmq.asyncio.Socket
stop: Callable[[], None]
log = logging.getLogger()
def _initialize(self):
self._is_test = True
self.context = context = zmq.asyncio.Context()
self.iopub_socket = context.socket(zmq.PUB)
self.stdin_socket = context.socket(zmq.ROUTER)
self.test_sockets = [self.iopub_socket]
for name in ["shell", "control"]:
socket = context.socket(zmq.ROUTER)
self.test_sockets.append(socket)
setattr(self, f"{name}_socket", socket)
self.session = TestSession(
[
self.shell_socket,
self.control_socket,
self.iopub_socket,
]
)
async def do_debug_request(self, msg):
return {}
def destroy(self):
self.stop()
self.session.close()
for socket in self.test_sockets:
socket.close()
self.context.destroy()
@no_type_check
async def test_shell_message(self, *args, **kwargs):
msg_list = self._prep_msg(*args, **kwargs)
await self.process_shell_message(msg_list)
receive_stream: MemoryObjectReceiveStream[Any] = self.session._streams[self.shell_socket][
"receive"
]
return await receive_stream.receive()
@no_type_check
async def test_control_message(self, *args, **kwargs):
msg_list = self._prep_msg(*args, **kwargs)
await self.process_control_message(msg_list)
receive_stream: MemoryObjectReceiveStream[Any] = self.session._streams[self.control_socket][
"receive"
]
return await receive_stream.receive()
def _on_send(self, msg, *args, **kwargs):
self._reply = msg
def _prep_msg(self, *args, **kwargs):
self._reply = None
raw_msg = self.session.msg(*args, **kwargs)
msg = self.session.serialize(raw_msg)
return msg
async def _wait_for_msg(self):
while not self._reply:
await asyncio.sleep(0.1)
_, msg = self.session.feed_identities(self._reply)
return self.session.deserialize(msg)
def _send_interrupt_children(self):
# override to prevent deadlock
pass
class MockKernel(KernelMixin, Kernel): # type:ignore
implementation = "test"
implementation_version = "1.0"
language = "no-op"
language_version = "0.1"
language_info = {
"name": "test",
"mimetype": "text/plain",
"file_extension": ".txt",
}
banner = "test kernel"
def __init__(self, *args, **kwargs):
self._initialize()
self.shell = MagicMock()
super().__init__(*args, **kwargs)
async def do_execute(
self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
):
if not silent:
stream_content = {"name": "stdout", "text": code}
self.send_response(self.iopub_socket, "stream", stream_content)
return {
"status": "ok",
# The base class increments the execution count
"execution_count": self.execution_count,
"payload": [],
"user_expressions": {},
}
class MockIPyKernel(KernelMixin, IPythonKernel): # type:ignore
def __init__(self, *args, **kwargs):
self._initialize()
super().__init__(*args, **kwargs)
@pytest.fixture()
async def kernel(anyio_backend):
async with create_task_group() as tg:
kernel = MockKernel()
tg.start_soon(kernel.start)
yield kernel
kernel.destroy()
@pytest.fixture()
async def ipkernel(anyio_backend):
async with create_task_group() as tg:
kernel = MockIPyKernel()
tg.start_soon(kernel.start)
yield kernel
kernel.destroy()
ZMQInteractiveShell.clear_instance()
@pytest.fixture()
def tracemalloc_resource_warning(recwarn, N=10):
"""fixture to enable tracemalloc for a single test, and report the
location of the leaked resource
We cannot only enable tracemalloc, as otherwise it is stopped just after the
test, the frame cache is cleared by tracemalloc.stop() and thus the warning
printing code get None when doing
`tracemalloc.get_object_traceback(r.source)`.
So we need to both filter the warnings to enable ResourceWarning, and loop
through it print the stack before we stop tracemalloc and continue.
"""
if tracemalloc is None:
yield
return
tracemalloc.start(N)
with warnings.catch_warnings():
warnings.simplefilter("always", category=ResourceWarning)
yield None
try:
for r in recwarn:
if r.category is ResourceWarning and r.source is not None:
tb = tracemalloc.get_object_traceback(r.source)
if tb:
info = f"Leaking resource:{r}\n |" + "\n |".join(tb.format())
# technically an Error and not a failure as we fail in the fixture
# and not the test
pytest.fail(info)
finally:
tracemalloc.stop()