99
1010from contextlib import contextmanager
1111from subprocess import Popen , PIPE
12+ from flaky import flaky
1213
1314from jupyter_client import BlockingKernelClient
1415from jupyter_core import paths
@@ -29,38 +30,41 @@ def setup_kernel(cmd):
2930 kernel_manager: connected KernelManager instance
3031 """
3132 kernel = Popen ([sys .executable , '-c' , cmd ], stdout = PIPE , stderr = PIPE )
32- connection_file = os .path .join (
33- paths .jupyter_runtime_dir (),
34- 'kernel-%i.json' % kernel .pid ,
35- )
36- # wait for connection file to exist, timeout after 5s
37- tic = time .time ()
38- while not os .path .exists (connection_file ) \
39- and kernel .poll () is None \
40- and time .time () < tic + SETUP_TIMEOUT :
41- time .sleep (0.1 )
42-
43- if kernel .poll () is not None :
44- o ,e = kernel .communicate ()
45- e = py3compat .cast_unicode (e )
46- raise IOError ("Kernel failed to start:\n %s" % e )
47-
48- if not os .path .exists (connection_file ):
49- if kernel .poll () is None :
50- kernel .terminate ()
51- raise IOError ("Connection file %r never arrived" % connection_file )
52-
53- client = BlockingKernelClient (connection_file = connection_file )
54- client .load_connection_file ()
55- client .start_channels ()
56- client .wait_for_ready ()
57-
5833 try :
59- yield client
34+ connection_file = os .path .join (
35+ paths .jupyter_runtime_dir (),
36+ 'kernel-%i.json' % kernel .pid ,
37+ )
38+ # wait for connection file to exist, timeout after 5s
39+ tic = time .time ()
40+ while not os .path .exists (connection_file ) \
41+ and kernel .poll () is None \
42+ and time .time () < tic + SETUP_TIMEOUT :
43+ time .sleep (0.1 )
44+
45+ if kernel .poll () is not None :
46+ o ,e = kernel .communicate ()
47+ e = py3compat .cast_unicode (e )
48+ raise IOError ("Kernel failed to start:\n %s" % e )
49+
50+ if not os .path .exists (connection_file ):
51+ if kernel .poll () is None :
52+ kernel .terminate ()
53+ raise IOError ("Connection file %r never arrived" % connection_file )
54+
55+ client = BlockingKernelClient (connection_file = connection_file )
56+ client .load_connection_file ()
57+ client .start_channels ()
58+ client .wait_for_ready ()
59+ try :
60+ yield client
61+ finally :
62+ client .stop_channels ()
6063 finally :
61- client .stop_channels ()
6264 kernel .terminate ()
6365
66+
67+ @flaky (max_runs = 3 )
6468def test_embed_kernel_basic ():
6569 """IPython.embed_kernel() is basically functional"""
6670 cmd = '\n ' .join ([
@@ -93,6 +97,8 @@ def test_embed_kernel_basic():
9397 text = content ['data' ]['text/plain' ]
9498 assert '10' in text
9599
100+
101+ @flaky (max_runs = 3 )
96102def test_embed_kernel_namespace ():
97103 """IPython.embed_kernel() inherits calling namespace"""
98104 cmd = '\n ' .join ([
@@ -128,6 +134,7 @@ def test_embed_kernel_namespace():
128134 content = msg ['content' ]
129135 assert not content ['found' ]
130136
137+ @flaky (max_runs = 3 )
131138def test_embed_kernel_reentrant ():
132139 """IPython.embed_kernel() can be called multiple times"""
133140 cmd = '\n ' .join ([
0 commit comments