##// END OF EJS Templates
Merge pull request #4121 from minrk/move-kernel-test...
Thomas Kluyver -
r12319:86e137ec merge
parent child Browse files
Show More
@@ -16,20 +16,22 b' import shutil'
16 import sys
16 import sys
17 import tempfile
17 import tempfile
18
18
19 from Queue import Empty
20 from contextlib import contextmanager
19 from contextlib import contextmanager
21 from subprocess import PIPE
20 from subprocess import PIPE
22
21
23 import nose.tools as nt
22 import nose.tools as nt
24
23
25 from IPython.zmq.blockingkernelmanager import BlockingKernelManager
24 from IPython.kernel import KernelManager
26 from IPython.zmq.tests.test_message_spec import execute, flush_channels
25 from IPython.kernel.tests.test_message_spec import execute, flush_channels
27 from IPython.testing import decorators as dec
26 from IPython.testing import decorators as dec
28 from IPython.utils import path, py3compat
27 from IPython.utils import path
29
28
30 #-------------------------------------------------------------------------------
29 #-------------------------------------------------------------------------------
31 # Tests
30 # Tests
32 #-------------------------------------------------------------------------------
31 #-------------------------------------------------------------------------------
32 IPYTHONDIR = None
33 save_env = None
34 save_get_ipython_dir = None
33
35
34 def setup():
36 def setup():
35 """setup temporary IPYTHONDIR for tests"""
37 """setup temporary IPYTHONDIR for tests"""
@@ -65,19 +67,20 b' def new_kernel():'
65 -------
67 -------
66 kernel_manager: connected KernelManager instance
68 kernel_manager: connected KernelManager instance
67 """
69 """
68 KM = BlockingKernelManager()
70 KM = KernelManager()
69
71
70 KM.start_kernel(stdout=PIPE, stderr=PIPE)
72 KM.start_kernel(stdout=PIPE, stderr=PIPE)
71 KM.start_channels()
73 KC = KM.client()
74 KC.start_channels()
72
75
73 # wait for kernel to be ready
76 # wait for kernel to be ready
74 KM.shell_channel.execute("import sys")
77 KC.shell_channel.execute("import sys")
75 KM.shell_channel.get_msg(block=True, timeout=5)
78 KC.shell_channel.get_msg(block=True, timeout=5)
76 flush_channels(KM)
79 flush_channels(KC)
77 try:
80 try:
78 yield KM
81 yield KC
79 finally:
82 finally:
80 KM.stop_channels()
83 KC.stop_channels()
81 KM.shutdown_kernel()
84 KM.shutdown_kernel()
82
85
83
86
@@ -105,34 +108,34 b' def assemble_output(iopub):'
105 return stdout, stderr
108 return stdout, stderr
106
109
107
110
108 def _check_mp_mode(km, expected=False, stream="stdout"):
111 def _check_mp_mode(kc, expected=False, stream="stdout"):
109 execute(km=km, code="import sys")
112 execute(kc=kc, code="import sys")
110 flush_channels(km)
113 flush_channels(kc)
111 msg_id, content = execute(km=km, code="print (sys.%s._check_mp_mode())" % stream)
114 msg_id, content = execute(kc=kc, code="print (sys.%s._check_mp_mode())" % stream)
112 stdout, stderr = assemble_output(km.iopub_channel)
115 stdout, stderr = assemble_output(kc.iopub_channel)
113 nt.assert_equal(eval(stdout.strip()), expected)
116 nt.assert_equal(eval(stdout.strip()), expected)
114
117
115
118
116 def test_simple_print():
119 def test_simple_print():
117 """simple print statement in kernel"""
120 """simple print statement in kernel"""
118 with new_kernel() as km:
121 with new_kernel() as kc:
119 iopub = km.iopub_channel
122 iopub = kc.iopub_channel
120 msg_id, content = execute(km=km, code="print ('hi')")
123 msg_id, content = execute(kc=kc, code="print ('hi')")
121 stdout, stderr = assemble_output(iopub)
124 stdout, stderr = assemble_output(iopub)
122 nt.assert_equal(stdout, 'hi\n')
125 nt.assert_equal(stdout, 'hi\n')
123 nt.assert_equal(stderr, '')
126 nt.assert_equal(stderr, '')
124 _check_mp_mode(km, expected=False)
127 _check_mp_mode(kc, expected=False)
125 print ('hello')
128 print ('hello')
126
129
127
130
128 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
131 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
129 def test_subprocess_print():
132 def test_subprocess_print():
130 """printing from forked mp.Process"""
133 """printing from forked mp.Process"""
131 with new_kernel() as km:
134 with new_kernel() as kc:
132 iopub = km.iopub_channel
135 iopub = kc.iopub_channel
133
136
134 _check_mp_mode(km, expected=False)
137 _check_mp_mode(kc, expected=False)
135 flush_channels(km)
138 flush_channels(kc)
136 np = 5
139 np = 5
137 code = '\n'.join([
140 code = '\n'.join([
138 "from __future__ import print_function",
141 "from __future__ import print_function",
@@ -146,20 +149,20 b' def test_subprocess_print():'
146 "hello %s" % i for i in range(np)
149 "hello %s" % i for i in range(np)
147 ]) + '\n'
150 ]) + '\n'
148
151
149 msg_id, content = execute(km=km, code=code)
152 msg_id, content = execute(kc=kc, code=code)
150 stdout, stderr = assemble_output(iopub)
153 stdout, stderr = assemble_output(iopub)
151 nt.assert_equal(stdout.count("hello"), np, stdout)
154 nt.assert_equal(stdout.count("hello"), np, stdout)
152 for n in range(np):
155 for n in range(np):
153 nt.assert_equal(stdout.count(str(n)), 1, stdout)
156 nt.assert_equal(stdout.count(str(n)), 1, stdout)
154 nt.assert_equal(stderr, '')
157 nt.assert_equal(stderr, '')
155 _check_mp_mode(km, expected=False)
158 _check_mp_mode(kc, expected=False)
156 _check_mp_mode(km, expected=False, stream="stderr")
159 _check_mp_mode(kc, expected=False, stream="stderr")
157
160
158
161
159 def test_subprocess_noprint():
162 def test_subprocess_noprint():
160 """mp.Process without print doesn't trigger iostream mp_mode"""
163 """mp.Process without print doesn't trigger iostream mp_mode"""
161 with new_kernel() as km:
164 with new_kernel() as kc:
162 iopub = km.iopub_channel
165 iopub = kc.iopub_channel
163
166
164 np = 5
167 np = 5
165 code = '\n'.join([
168 code = '\n'.join([
@@ -169,20 +172,20 b' def test_subprocess_noprint():'
169 "for p in pool: p.join()"
172 "for p in pool: p.join()"
170 ])
173 ])
171
174
172 msg_id, content = execute(km=km, code=code)
175 msg_id, content = execute(kc=kc, code=code)
173 stdout, stderr = assemble_output(iopub)
176 stdout, stderr = assemble_output(iopub)
174 nt.assert_equal(stdout, '')
177 nt.assert_equal(stdout, '')
175 nt.assert_equal(stderr, '')
178 nt.assert_equal(stderr, '')
176
179
177 _check_mp_mode(km, expected=False)
180 _check_mp_mode(kc, expected=False)
178 _check_mp_mode(km, expected=False, stream="stderr")
181 _check_mp_mode(kc, expected=False, stream="stderr")
179
182
180
183
181 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
184 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
182 def test_subprocess_error():
185 def test_subprocess_error():
183 """error in mp.Process doesn't crash"""
186 """error in mp.Process doesn't crash"""
184 with new_kernel() as km:
187 with new_kernel() as kc:
185 iopub = km.iopub_channel
188 iopub = kc.iopub_channel
186
189
187 code = '\n'.join([
190 code = '\n'.join([
188 "import multiprocessing as mp",
191 "import multiprocessing as mp",
@@ -191,11 +194,11 b' def test_subprocess_error():'
191 "p.join()",
194 "p.join()",
192 ])
195 ])
193
196
194 msg_id, content = execute(km=km, code=code)
197 msg_id, content = execute(kc=kc, code=code)
195 stdout, stderr = assemble_output(iopub)
198 stdout, stderr = assemble_output(iopub)
196 nt.assert_equal(stdout, '')
199 nt.assert_equal(stdout, '')
197 nt.assert_true("ValueError" in stderr, stderr)
200 nt.assert_true("ValueError" in stderr, stderr)
198
201
199 _check_mp_mode(km, expected=False)
202 _check_mp_mode(kc, expected=False)
200 _check_mp_mode(km, expected=False, stream="stderr")
203 _check_mp_mode(kc, expected=False, stream="stderr")
201
204
@@ -65,15 +65,17 b' def flush_channels(kc=None):'
65
65
66 def execute(code='', kc=None, **kwargs):
66 def execute(code='', kc=None, **kwargs):
67 """wrapper for doing common steps for validating an execution request"""
67 """wrapper for doing common steps for validating an execution request"""
68 msg_id = KC.execute(code=code, **kwargs)
68 if kc is None:
69 reply = KC.get_shell_msg(timeout=TIMEOUT)
69 kc = KC
70 msg_id = kc.execute(code=code, **kwargs)
71 reply = kc.get_shell_msg(timeout=TIMEOUT)
70 list(validate_message(reply, 'execute_reply', msg_id))
72 list(validate_message(reply, 'execute_reply', msg_id))
71 busy = KC.get_iopub_msg(timeout=TIMEOUT)
73 busy = kc.get_iopub_msg(timeout=TIMEOUT)
72 list(validate_message(busy, 'status', msg_id))
74 list(validate_message(busy, 'status', msg_id))
73 nt.assert_equal(busy['content']['execution_state'], 'busy')
75 nt.assert_equal(busy['content']['execution_state'], 'busy')
74
76
75 if not kwargs.get('silent'):
77 if not kwargs.get('silent'):
76 pyin = KC.get_iopub_msg(timeout=TIMEOUT)
78 pyin = kc.get_iopub_msg(timeout=TIMEOUT)
77 list(validate_message(pyin, 'pyin', msg_id))
79 list(validate_message(pyin, 'pyin', msg_id))
78 nt.assert_equal(pyin['content']['code'], code)
80 nt.assert_equal(pyin['content']['code'], code)
79
81
General Comments 0
You need to be logged in to leave comments. Login now