Skip to content

Commit 28f908f

Browse files
committed
Workaround lack of timeout on Py2
1 parent 7e6d167 commit 28f908f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

jupyter_client/tests/test_kernelapp.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@
77
from tempfile import mkdtemp
88
import time
99

10+
PY3 = sys.version_info[0] >= 3
11+
1012
def _launch(extra_env):
1113
env = os.environ.copy()
1214
env.update(extra_env)
1315
return Popen([sys.executable, '-c',
1416
'from jupyter_client.kernelapp import main; main()'],
15-
env=env, stderr=PIPE)
17+
env=env, stderr=(PIPE if PY3 else None))
1618

1719
WAIT_TIME = 10
1820
POLL_FREQ = 10
1921

22+
def hacky_wait(p):
23+
"""Python 2 subprocess doesn't have timeouts :-("""
24+
for _ in range(WAIT_TIME * POLL_FREQ):
25+
if p.poll() is not None:
26+
return p.returncode
27+
time.sleep(1 / POLL_FREQ)
28+
else:
29+
raise AssertionError("Process didn't exit in {} seconds"
30+
.format(WAIT_TIME))
31+
2032
def test_kernelapp_lifecycle():
2133
# Check that 'jupyter kernel' starts and terminates OK.
2234
runtime_dir = mkdtemp()
@@ -42,15 +54,13 @@ def test_kernelapp_lifecycle():
4254
assert cf.startswith('kernel')
4355
assert cf.endswith('.json')
4456

45-
# Read the first three lines from stderr. This will hang if there are
46-
# fewer lines to read; I don't see any way to avoid that without lots
47-
# of extra complexity.
48-
b = b''.join(p.stderr.readline() for _ in range(2)).decode('utf-8', 'replace')
49-
assert cf in b
50-
5157
# Send SIGTERM to shut down
5258
p.terminate()
53-
p.wait(timeout=10)
59+
if PY3:
60+
_, stderr = p.communicate(timeout=WAIT_TIME)
61+
assert cf in stderr.decode('utf-8', 'replace')
62+
else:
63+
hacky_wait(p)
5464
finally:
5565
shutil.rmtree(runtime_dir)
5666
shutil.rmtree(startup_dir)

0 commit comments

Comments
 (0)