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