-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathtest_restarter.py
285 lines (230 loc) · 8.21 KB
/
test_restarter.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
"""Tests for the KernelManager"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import asyncio
import json
import os
import sys
from concurrent.futures import Future
import pytest
from jupyter_core import paths
from traitlets.config.loader import Config
from traitlets.log import get_logger
from jupyter_client.ioloop import AsyncIOLoopKernelManager, IOLoopKernelManager
pjoin = os.path.join
def _install_kernel(name="problemtest", extra_env=None):
if extra_env is None:
extra_env = {}
kernel_dir = pjoin(paths.jupyter_data_dir(), "kernels", name)
os.makedirs(kernel_dir)
with open(pjoin(kernel_dir, "kernel.json"), "w") as f:
f.write(
json.dumps(
{
"argv": [
sys.executable,
"-m",
"tests.problemkernel",
"-f",
"{connection_file}",
],
"display_name": "Problematic Test Kernel",
"env": {"TEST_VARS": "${TEST_VARS}:test_var_2", **extra_env},
}
)
)
return name
@pytest.fixture
def install_kernel():
return _install_kernel("problemtest")
@pytest.fixture
def install_fail_kernel():
return _install_kernel("problemtest-fail", extra_env={"FAIL_ON_START": "1"})
@pytest.fixture
def install_slow_fail_kernel():
return _install_kernel(
"problemtest-slow", extra_env={"STARTUP_DELAY": "5", "FAIL_ON_START": "1"}
)
@pytest.fixture(params=["tcp", "ipc"])
def transport(request):
if sys.platform == "win32" and request.param == "ipc": #
pytest.skip("Transport 'ipc' not supported on Windows.")
return request.param
@pytest.fixture
def config(transport):
c = Config()
c.KernelManager.transport = transport
if transport == "ipc":
c.KernelManager.ip = "test"
return c
@pytest.fixture
def debug_logging():
get_logger().setLevel("DEBUG")
win_skip = pytest.mark.skipif(
os.name == "nt",
reason='"RuntimeError: Cannot run the event loop while another loop is running" error on Windows',
)
@win_skip
async def test_restart_check(config, install_kernel, debug_logging):
"""Test that the kernel is restarted and recovers"""
# If this test fails, run it with --log-cli-level=DEBUG to inspect
N_restarts = 1
config.KernelRestarter.restart_limit = N_restarts
config.KernelRestarter.debug = True
km = IOLoopKernelManager(kernel_name=install_kernel, config=config)
cbs = 0
restarts: list = [Future() for i in range(N_restarts)]
def cb():
nonlocal cbs
if cbs >= N_restarts:
raise RuntimeError("Kernel restarted more than %d times!" % N_restarts)
restarts[cbs].set_result(True)
cbs += 1
try:
km.start_kernel()
km.add_restart_callback(cb, "restart")
except BaseException:
if km.has_kernel:
km.shutdown_kernel()
raise
try:
for i in range(N_restarts + 1):
kc = km.client()
kc.start_channels()
kc.wait_for_ready(timeout=60)
kc.stop_channels()
if i < N_restarts:
# Kill without cleanup to simulate crash:
assert km.provisioner is not None
await km.provisioner.kill()
restarts[i].result()
# Wait for kill + restart
max_wait = 10.0
waited = 0.0
while waited < max_wait and km.is_alive():
await asyncio.sleep(0.1)
waited += 0.1
while waited < max_wait and not km.is_alive():
await asyncio.sleep(0.1)
waited += 0.1
assert cbs == N_restarts
assert km.is_alive()
finally:
km.shutdown_kernel(now=True)
assert km.context.closed
@win_skip
async def test_restarter_gives_up(config, install_fail_kernel, debug_logging):
"""Test that the restarter gives up after reaching the restart limit"""
# If this test fails, run it with --log-cli-level=DEBUG to inspect
N_restarts = 1
config.KernelRestarter.restart_limit = N_restarts
config.KernelRestarter.debug = True
km = IOLoopKernelManager(kernel_name=install_fail_kernel, config=config)
cbs = 0
restarts: list = [Future() for i in range(N_restarts)]
def cb():
nonlocal cbs
if cbs >= N_restarts:
raise RuntimeError("Kernel restarted more than %d times!" % N_restarts)
restarts[cbs].set_result(True)
cbs += 1
died: Future = Future()
def on_death():
died.set_result(True)
try:
km.start_kernel()
km.add_restart_callback(cb, "restart")
km.add_restart_callback(on_death, "dead")
except BaseException:
if km.has_kernel:
km.shutdown_kernel()
raise
try:
for i in range(N_restarts):
restarts[i].result()
assert died.result()
assert cbs == N_restarts
finally:
km.shutdown_kernel(now=True)
assert km.context.closed
async def test_async_restart_check(config, install_kernel, debug_logging):
"""Test that the kernel is restarted and recovers"""
# If this test fails, run it with --log-cli-level=DEBUG to inspect
N_restarts = 1
config.KernelRestarter.restart_limit = N_restarts
config.KernelRestarter.debug = True
km = AsyncIOLoopKernelManager(kernel_name=install_kernel, config=config)
cbs = 0
restarts: list = [asyncio.Future() for i in range(N_restarts)]
def cb():
nonlocal cbs
if cbs >= N_restarts:
raise RuntimeError("Kernel restarted more than %d times!" % N_restarts)
restarts[cbs].set_result(True)
cbs += 1
try:
await km.start_kernel()
km.add_restart_callback(cb, "restart")
except BaseException:
if km.has_kernel:
await km.shutdown_kernel()
raise
try:
for i in range(N_restarts + 1):
kc = km.client()
kc.start_channels()
await kc.wait_for_ready(timeout=60)
kc.stop_channels()
if i < N_restarts:
# Kill without cleanup to simulate crash:
assert km.provisioner is not None
await km.provisioner.kill()
await restarts[i]
# Wait for kill + restart
max_wait = 10.0
waited = 0.0
while waited < max_wait and await km.is_alive():
await asyncio.sleep(0.1)
waited += 0.1
while waited < max_wait and not await km.is_alive():
await asyncio.sleep(0.1)
waited += 0.1
assert cbs == N_restarts
assert await km.is_alive()
finally:
await km.shutdown_kernel(now=True)
assert km.context.closed
async def test_async_restarter_gives_up(config, install_slow_fail_kernel, debug_logging):
"""Test that the restarter gives up after reaching the restart limit"""
# If this test fails, run it with --log-cli-level=DEBUG to inspect
N_restarts = 2
config.KernelRestarter.restart_limit = N_restarts
config.KernelRestarter.debug = True
config.KernelRestarter.stable_start_time = 30.0
km = AsyncIOLoopKernelManager(kernel_name=install_slow_fail_kernel, config=config)
cbs = 0
restarts: list = [asyncio.Future() for i in range(N_restarts)]
def cb():
nonlocal cbs
if cbs >= N_restarts:
raise RuntimeError("Kernel restarted more than %d times!" % N_restarts)
restarts[cbs].set_result(True)
cbs += 1
died: asyncio.Future = asyncio.Future()
def on_death():
died.set_result(True)
try:
await km.start_kernel()
km.add_restart_callback(cb, "restart")
km.add_restart_callback(on_death, "dead")
except BaseException:
if km.has_kernel:
await km.shutdown_kernel()
raise
try:
await asyncio.gather(*restarts)
assert await died
assert cbs == N_restarts
finally:
await km.shutdown_kernel(now=True)
assert km.context.closed