1
1
"""Tests for the KernelManager"""
2
2
# Copyright (c) Jupyter Development Team.
3
3
# Distributed under the terms of the Modified BSD License.
4
-
5
4
import asyncio
6
- import concurrent .futures
7
5
import json
8
6
import os
9
- import signal
10
7
import sys
11
- import time
12
- from subprocess import PIPE
13
8
14
9
import pytest
15
10
from jupyter_core import paths
16
11
from traitlets .config .loader import Config
17
12
from traitlets .log import get_logger
18
13
19
- from jupyter_client import AsyncKernelManager
20
- from jupyter_client .ioloop import AsyncIOLoopKernelManager , IOLoopKernelManager
21
- from ..manager import start_new_async_kernel
22
- from ..manager import start_new_kernel
14
+ from jupyter_client .ioloop import AsyncIOLoopKernelManager
15
+ from jupyter_client .ioloop import IOLoopKernelManager
23
16
24
17
pjoin = os .path .join
25
18
19
+
26
20
def _install_kernel (name = "problemtest" , extra_env = None ):
27
21
if extra_env is None :
28
22
extra_env = dict ()
@@ -46,29 +40,31 @@ def _install_kernel(name="problemtest", extra_env=None):
46
40
)
47
41
return name
48
42
43
+
49
44
@pytest .fixture
50
45
def install_kernel ():
51
46
return _install_kernel ("problemtest" )
52
47
48
+
53
49
@pytest .fixture
54
50
def install_fail_kernel ():
55
- return _install_kernel ("problemtest-fail" , extra_env = {
56
- "FAIL_ON_START" : "1"
57
- })
51
+ return _install_kernel ("problemtest-fail" , extra_env = {"FAIL_ON_START" : "1" })
52
+
58
53
59
54
@pytest .fixture
60
55
def install_slow_fail_kernel ():
61
- return _install_kernel ("problemtest-slow" , extra_env = {
62
- "STARTUP_DELAY" : "5" ,
63
- "FAIL_ON_START" : "1"
64
- })
56
+ return _install_kernel (
57
+ "problemtest-slow" , extra_env = { " STARTUP_DELAY" : "5" , "FAIL_ON_START" : "1" }
58
+ )
59
+
65
60
66
61
@pytest .fixture (params = ["tcp" , "ipc" ])
67
62
def transport (request ):
68
63
if sys .platform == "win32" and request .param == "ipc" : #
69
64
pytest .skip ("Transport 'ipc' not supported on Windows." )
70
65
return request .param
71
66
67
+
72
68
@pytest .fixture
73
69
def config (transport ):
74
70
c = Config ()
@@ -77,6 +73,7 @@ def config(transport):
77
73
c .KernelManager .ip = "test"
78
74
return c
79
75
76
+
80
77
@pytest .fixture
81
78
def debug_logging ():
82
79
get_logger ().setLevel ("DEBUG" )
@@ -93,6 +90,7 @@ async def test_restart_check(config, install_kernel):
93
90
94
91
cbs = 0
95
92
restarts = [asyncio .Future () for i in range (N_restarts )]
93
+
96
94
def cb ():
97
95
nonlocal cbs
98
96
if cbs >= N_restarts :
@@ -103,7 +101,7 @@ def cb():
103
101
try :
104
102
km .start_kernel ()
105
103
km .add_restart_callback (cb , 'restart' )
106
- except :
104
+ except BaseException :
107
105
if km .has_kernel :
108
106
km .shutdown_kernel ()
109
107
raise
@@ -127,6 +125,7 @@ def cb():
127
125
km .shutdown_kernel (now = True )
128
126
assert km .context .closed
129
127
128
+
130
129
@pytest .mark .asyncio
131
130
async def test_restarter_gives_up (config , install_fail_kernel ):
132
131
"""Test that the restarter gives up after reaching the restart limit"""
@@ -138,6 +137,7 @@ async def test_restarter_gives_up(config, install_fail_kernel):
138
137
139
138
cbs = 0
140
139
restarts = [asyncio .Future () for i in range (N_restarts )]
140
+
141
141
def cb ():
142
142
nonlocal cbs
143
143
if cbs >= N_restarts :
@@ -146,14 +146,15 @@ def cb():
146
146
cbs += 1
147
147
148
148
died = asyncio .Future ()
149
+
149
150
def on_death ():
150
151
died .set_result (True )
151
152
152
153
try :
153
154
km .start_kernel ()
154
155
km .add_restart_callback (cb , 'restart' )
155
156
km .add_restart_callback (on_death , 'dead' )
156
- except :
157
+ except BaseException :
157
158
if km .has_kernel :
158
159
km .shutdown_kernel ()
159
160
raise
@@ -182,6 +183,7 @@ async def test_async_restart_check(config, install_kernel):
182
183
183
184
cbs = 0
184
185
restarts = [asyncio .Future () for i in range (N_restarts )]
186
+
185
187
def cb ():
186
188
nonlocal cbs
187
189
if cbs >= N_restarts :
@@ -192,7 +194,7 @@ def cb():
192
194
try :
193
195
await km .start_kernel ()
194
196
km .add_restart_callback (cb , 'restart' )
195
- except :
197
+ except BaseException :
196
198
if km .has_kernel :
197
199
await km .shutdown_kernel ()
198
200
raise
@@ -216,18 +218,20 @@ def cb():
216
218
await km .shutdown_kernel (now = True )
217
219
assert km .context .closed
218
220
221
+
219
222
@pytest .mark .asyncio
220
223
async def test_async_restarter_gives_up (config , install_slow_fail_kernel ):
221
224
"""Test that the restarter gives up after reaching the restart limit"""
222
225
# If this test failes, run it with --log-cli-level=DEBUG to inspect
223
226
N_restarts = 2
224
227
config .KernelRestarter .restart_limit = N_restarts
225
228
config .KernelRestarter .debug = True
226
- config .KernelRestarter .stable_start_time = 30.
229
+ config .KernelRestarter .stable_start_time = 30.0
227
230
km = AsyncIOLoopKernelManager (kernel_name = install_slow_fail_kernel , config = config )
228
231
229
232
cbs = 0
230
233
restarts = [asyncio .Future () for i in range (N_restarts )]
234
+
231
235
def cb ():
232
236
nonlocal cbs
233
237
if cbs >= N_restarts :
@@ -236,14 +240,15 @@ def cb():
236
240
cbs += 1
237
241
238
242
died = asyncio .Future ()
243
+
239
244
def on_death ():
240
245
died .set_result (True )
241
246
242
247
try :
243
248
await km .start_kernel ()
244
249
km .add_restart_callback (cb , 'restart' )
245
250
km .add_restart_callback (on_death , 'dead' )
246
- except :
251
+ except BaseException :
247
252
if km .has_kernel :
248
253
await km .shutdown_kernel ()
249
254
raise
@@ -258,4 +263,3 @@ def on_death():
258
263
259
264
await km .shutdown_kernel (now = True )
260
265
assert km .context .closed
261
-
0 commit comments