##// END OF EJS Templates
update parallel magic test for exception output with recent change
MinRK -
Show More
@@ -1,340 +1,340
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Test Parallel magics
2 """Test Parallel magics
3
3
4 Authors:
4 Authors:
5
5
6 * Min RK
6 * Min RK
7 """
7 """
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2011 The IPython Development Team
9 # Copyright (C) 2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14
14
15 #-------------------------------------------------------------------------------
15 #-------------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-------------------------------------------------------------------------------
17 #-------------------------------------------------------------------------------
18
18
19 import re
19 import re
20 import sys
20 import sys
21 import time
21 import time
22
22
23 import zmq
23 import zmq
24 from nose import SkipTest
24 from nose import SkipTest
25
25
26 from IPython.testing import decorators as dec
26 from IPython.testing import decorators as dec
27 from IPython.testing.ipunittest import ParametricTestCase
27 from IPython.testing.ipunittest import ParametricTestCase
28 from IPython.utils.io import capture_output
28 from IPython.utils.io import capture_output
29
29
30 from IPython import parallel as pmod
30 from IPython import parallel as pmod
31 from IPython.parallel import error
31 from IPython.parallel import error
32 from IPython.parallel import AsyncResult
32 from IPython.parallel import AsyncResult
33 from IPython.parallel.util import interactive
33 from IPython.parallel.util import interactive
34
34
35 from IPython.parallel.tests import add_engines
35 from IPython.parallel.tests import add_engines
36
36
37 from .clienttest import ClusterTestCase, generate_output
37 from .clienttest import ClusterTestCase, generate_output
38
38
39 def setup():
39 def setup():
40 add_engines(3, total=True)
40 add_engines(3, total=True)
41
41
42 class TestParallelMagics(ClusterTestCase, ParametricTestCase):
42 class TestParallelMagics(ClusterTestCase, ParametricTestCase):
43
43
44 def test_px_blocking(self):
44 def test_px_blocking(self):
45 ip = get_ipython()
45 ip = get_ipython()
46 v = self.client[-1:]
46 v = self.client[-1:]
47 v.activate()
47 v.activate()
48 v.block=True
48 v.block=True
49
49
50 ip.magic('px a=5')
50 ip.magic('px a=5')
51 self.assertEquals(v['a'], [5])
51 self.assertEquals(v['a'], [5])
52 ip.magic('px a=10')
52 ip.magic('px a=10')
53 self.assertEquals(v['a'], [10])
53 self.assertEquals(v['a'], [10])
54 # just 'print a' works ~99% of the time, but this ensures that
54 # just 'print a' works ~99% of the time, but this ensures that
55 # the stdout message has arrived when the result is finished:
55 # the stdout message has arrived when the result is finished:
56 with capture_output() as io:
56 with capture_output() as io:
57 ip.magic(
57 ip.magic(
58 'px import sys,time;print(a);sys.stdout.flush();time.sleep(0.2)'
58 'px import sys,time;print(a);sys.stdout.flush();time.sleep(0.2)'
59 )
59 )
60 out = io.stdout
60 out = io.stdout
61 self.assertTrue('[stdout:' in out, out)
61 self.assertTrue('[stdout:' in out, out)
62 self.assertFalse('\n\n' in out)
62 self.assertFalse('\n\n' in out)
63 self.assertTrue(out.rstrip().endswith('10'))
63 self.assertTrue(out.rstrip().endswith('10'))
64 self.assertRaisesRemote(ZeroDivisionError, ip.magic, 'px 1/0')
64 self.assertRaisesRemote(ZeroDivisionError, ip.magic, 'px 1/0')
65
65
66 def _check_generated_stderr(self, stderr, n):
66 def _check_generated_stderr(self, stderr, n):
67 expected = [
67 expected = [
68 r'\[stderr:\d+\]',
68 r'\[stderr:\d+\]',
69 '^stderr$',
69 '^stderr$',
70 '^stderr2$',
70 '^stderr2$',
71 ] * n
71 ] * n
72
72
73 self.assertFalse('\n\n' in stderr, stderr)
73 self.assertFalse('\n\n' in stderr, stderr)
74 lines = stderr.splitlines()
74 lines = stderr.splitlines()
75 self.assertEquals(len(lines), len(expected), stderr)
75 self.assertEquals(len(lines), len(expected), stderr)
76 for line,expect in zip(lines, expected):
76 for line,expect in zip(lines, expected):
77 if isinstance(expect, str):
77 if isinstance(expect, str):
78 expect = [expect]
78 expect = [expect]
79 for ex in expect:
79 for ex in expect:
80 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
80 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
81
81
82 def test_cellpx_block_args(self):
82 def test_cellpx_block_args(self):
83 """%%px --[no]block flags work"""
83 """%%px --[no]block flags work"""
84 ip = get_ipython()
84 ip = get_ipython()
85 v = self.client[-1:]
85 v = self.client[-1:]
86 v.activate()
86 v.activate()
87 v.block=False
87 v.block=False
88
88
89 for block in (True, False):
89 for block in (True, False):
90 v.block = block
90 v.block = block
91
91
92 with capture_output() as io:
92 with capture_output() as io:
93 ip.run_cell_magic("px", "", "1")
93 ip.run_cell_magic("px", "", "1")
94 if block:
94 if block:
95 self.assertTrue(io.stdout.startswith("Parallel"), io.stdout)
95 self.assertTrue(io.stdout.startswith("Parallel"), io.stdout)
96 else:
96 else:
97 self.assertTrue(io.stdout.startswith("Async"), io.stdout)
97 self.assertTrue(io.stdout.startswith("Async"), io.stdout)
98
98
99 with capture_output() as io:
99 with capture_output() as io:
100 ip.run_cell_magic("px", "--block", "1")
100 ip.run_cell_magic("px", "--block", "1")
101 self.assertTrue(io.stdout.startswith("Parallel"), io.stdout)
101 self.assertTrue(io.stdout.startswith("Parallel"), io.stdout)
102
102
103 with capture_output() as io:
103 with capture_output() as io:
104 ip.run_cell_magic("px", "--noblock", "1")
104 ip.run_cell_magic("px", "--noblock", "1")
105 self.assertTrue(io.stdout.startswith("Async"), io.stdout)
105 self.assertTrue(io.stdout.startswith("Async"), io.stdout)
106
106
107 def test_cellpx_groupby_engine(self):
107 def test_cellpx_groupby_engine(self):
108 """%%px --group-outputs=engine"""
108 """%%px --group-outputs=engine"""
109 ip = get_ipython()
109 ip = get_ipython()
110 v = self.client[:]
110 v = self.client[:]
111 v.block = True
111 v.block = True
112 v.activate()
112 v.activate()
113
113
114 v['generate_output'] = generate_output
114 v['generate_output'] = generate_output
115
115
116 with capture_output() as io:
116 with capture_output() as io:
117 ip.run_cell_magic('px', '--group-outputs=engine', 'generate_output()')
117 ip.run_cell_magic('px', '--group-outputs=engine', 'generate_output()')
118
118
119 self.assertFalse('\n\n' in io.stdout)
119 self.assertFalse('\n\n' in io.stdout)
120 lines = io.stdout.splitlines()[1:]
120 lines = io.stdout.splitlines()[1:]
121 expected = [
121 expected = [
122 r'\[stdout:\d+\]',
122 r'\[stdout:\d+\]',
123 'stdout',
123 'stdout',
124 'stdout2',
124 'stdout2',
125 r'\[output:\d+\]',
125 r'\[output:\d+\]',
126 r'IPython\.core\.display\.HTML',
126 r'IPython\.core\.display\.HTML',
127 r'IPython\.core\.display\.Math',
127 r'IPython\.core\.display\.Math',
128 r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math',
128 r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math',
129 ] * len(v)
129 ] * len(v)
130
130
131 self.assertEquals(len(lines), len(expected), io.stdout)
131 self.assertEquals(len(lines), len(expected), io.stdout)
132 for line,expect in zip(lines, expected):
132 for line,expect in zip(lines, expected):
133 if isinstance(expect, str):
133 if isinstance(expect, str):
134 expect = [expect]
134 expect = [expect]
135 for ex in expect:
135 for ex in expect:
136 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
136 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
137
137
138 self._check_generated_stderr(io.stderr, len(v))
138 self._check_generated_stderr(io.stderr, len(v))
139
139
140
140
141 def test_cellpx_groupby_order(self):
141 def test_cellpx_groupby_order(self):
142 """%%px --group-outputs=order"""
142 """%%px --group-outputs=order"""
143 ip = get_ipython()
143 ip = get_ipython()
144 v = self.client[:]
144 v = self.client[:]
145 v.block = True
145 v.block = True
146 v.activate()
146 v.activate()
147
147
148 v['generate_output'] = generate_output
148 v['generate_output'] = generate_output
149
149
150 with capture_output() as io:
150 with capture_output() as io:
151 ip.run_cell_magic('px', '--group-outputs=order', 'generate_output()')
151 ip.run_cell_magic('px', '--group-outputs=order', 'generate_output()')
152
152
153 self.assertFalse('\n\n' in io.stdout)
153 self.assertFalse('\n\n' in io.stdout)
154 lines = io.stdout.splitlines()[1:]
154 lines = io.stdout.splitlines()[1:]
155 expected = []
155 expected = []
156 expected.extend([
156 expected.extend([
157 r'\[stdout:\d+\]',
157 r'\[stdout:\d+\]',
158 'stdout',
158 'stdout',
159 'stdout2',
159 'stdout2',
160 ] * len(v))
160 ] * len(v))
161 expected.extend([
161 expected.extend([
162 r'\[output:\d+\]',
162 r'\[output:\d+\]',
163 'IPython.core.display.HTML',
163 'IPython.core.display.HTML',
164 ] * len(v))
164 ] * len(v))
165 expected.extend([
165 expected.extend([
166 r'\[output:\d+\]',
166 r'\[output:\d+\]',
167 'IPython.core.display.Math',
167 'IPython.core.display.Math',
168 ] * len(v))
168 ] * len(v))
169 expected.extend([
169 expected.extend([
170 r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math'
170 r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math'
171 ] * len(v))
171 ] * len(v))
172
172
173 self.assertEquals(len(lines), len(expected), io.stdout)
173 self.assertEquals(len(lines), len(expected), io.stdout)
174 for line,expect in zip(lines, expected):
174 for line,expect in zip(lines, expected):
175 if isinstance(expect, str):
175 if isinstance(expect, str):
176 expect = [expect]
176 expect = [expect]
177 for ex in expect:
177 for ex in expect:
178 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
178 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
179
179
180 self._check_generated_stderr(io.stderr, len(v))
180 self._check_generated_stderr(io.stderr, len(v))
181
181
182 def test_cellpx_groupby_type(self):
182 def test_cellpx_groupby_type(self):
183 """%%px --group-outputs=type"""
183 """%%px --group-outputs=type"""
184 ip = get_ipython()
184 ip = get_ipython()
185 v = self.client[:]
185 v = self.client[:]
186 v.block = True
186 v.block = True
187 v.activate()
187 v.activate()
188
188
189 v['generate_output'] = generate_output
189 v['generate_output'] = generate_output
190
190
191 with capture_output() as io:
191 with capture_output() as io:
192 ip.run_cell_magic('px', '--group-outputs=type', 'generate_output()')
192 ip.run_cell_magic('px', '--group-outputs=type', 'generate_output()')
193
193
194 self.assertFalse('\n\n' in io.stdout)
194 self.assertFalse('\n\n' in io.stdout)
195 lines = io.stdout.splitlines()[1:]
195 lines = io.stdout.splitlines()[1:]
196
196
197 expected = []
197 expected = []
198 expected.extend([
198 expected.extend([
199 r'\[stdout:\d+\]',
199 r'\[stdout:\d+\]',
200 'stdout',
200 'stdout',
201 'stdout2',
201 'stdout2',
202 ] * len(v))
202 ] * len(v))
203 expected.extend([
203 expected.extend([
204 r'\[output:\d+\]',
204 r'\[output:\d+\]',
205 r'IPython\.core\.display\.HTML',
205 r'IPython\.core\.display\.HTML',
206 r'IPython\.core\.display\.Math',
206 r'IPython\.core\.display\.Math',
207 ] * len(v))
207 ] * len(v))
208 expected.extend([
208 expected.extend([
209 (r'Out\[\d+:\d+\]', r'IPython\.core\.display\.Math')
209 (r'Out\[\d+:\d+\]', r'IPython\.core\.display\.Math')
210 ] * len(v))
210 ] * len(v))
211
211
212 self.assertEquals(len(lines), len(expected), io.stdout)
212 self.assertEquals(len(lines), len(expected), io.stdout)
213 for line,expect in zip(lines, expected):
213 for line,expect in zip(lines, expected):
214 if isinstance(expect, str):
214 if isinstance(expect, str):
215 expect = [expect]
215 expect = [expect]
216 for ex in expect:
216 for ex in expect:
217 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
217 self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line))
218
218
219 self._check_generated_stderr(io.stderr, len(v))
219 self._check_generated_stderr(io.stderr, len(v))
220
220
221
221
222 def test_px_nonblocking(self):
222 def test_px_nonblocking(self):
223 ip = get_ipython()
223 ip = get_ipython()
224 v = self.client[-1:]
224 v = self.client[-1:]
225 v.activate()
225 v.activate()
226 v.block=False
226 v.block=False
227
227
228 ip.magic('px a=5')
228 ip.magic('px a=5')
229 self.assertEquals(v['a'], [5])
229 self.assertEquals(v['a'], [5])
230 ip.magic('px a=10')
230 ip.magic('px a=10')
231 self.assertEquals(v['a'], [10])
231 self.assertEquals(v['a'], [10])
232 with capture_output() as io:
232 with capture_output() as io:
233 ar = ip.magic('px print (a)')
233 ar = ip.magic('px print (a)')
234 self.assertTrue(isinstance(ar, AsyncResult))
234 self.assertTrue(isinstance(ar, AsyncResult))
235 self.assertTrue('Async' in io.stdout)
235 self.assertTrue('Async' in io.stdout)
236 self.assertFalse('[stdout:' in io.stdout)
236 self.assertFalse('[stdout:' in io.stdout)
237 self.assertFalse('\n\n' in io.stdout)
237 self.assertFalse('\n\n' in io.stdout)
238
238
239 ar = ip.magic('px 1/0')
239 ar = ip.magic('px 1/0')
240 self.assertRaisesRemote(ZeroDivisionError, ar.get)
240 self.assertRaisesRemote(ZeroDivisionError, ar.get)
241
241
242 def test_autopx_blocking(self):
242 def test_autopx_blocking(self):
243 ip = get_ipython()
243 ip = get_ipython()
244 v = self.client[-1]
244 v = self.client[-1]
245 v.activate()
245 v.activate()
246 v.block=True
246 v.block=True
247
247
248 with capture_output() as io:
248 with capture_output() as io:
249 ip.magic('autopx')
249 ip.magic('autopx')
250 ip.run_cell('\n'.join(('a=5','b=12345','c=0')))
250 ip.run_cell('\n'.join(('a=5','b=12345','c=0')))
251 ip.run_cell('b*=2')
251 ip.run_cell('b*=2')
252 ip.run_cell('print (b)')
252 ip.run_cell('print (b)')
253 ip.run_cell('b')
253 ip.run_cell('b')
254 ip.run_cell("b/c")
254 ip.run_cell("b/c")
255 ip.magic('autopx')
255 ip.magic('autopx')
256
256
257 output = io.stdout
257 output = io.stdout
258
258
259 self.assertTrue(output.startswith('%autopx enabled'), output)
259 self.assertTrue(output.startswith('%autopx enabled'), output)
260 self.assertTrue(output.rstrip().endswith('%autopx disabled'), output)
260 self.assertTrue(output.rstrip().endswith('%autopx disabled'), output)
261 self.assertTrue('RemoteError: ZeroDivisionError' in output, output)
261 self.assertTrue('ZeroDivisionError' in output, output)
262 self.assertTrue('\nOut[' in output, output)
262 self.assertTrue('\nOut[' in output, output)
263 self.assertTrue(': 24690' in output, output)
263 self.assertTrue(': 24690' in output, output)
264 ar = v.get_result(-1)
264 ar = v.get_result(-1)
265 self.assertEquals(v['a'], 5)
265 self.assertEquals(v['a'], 5)
266 self.assertEquals(v['b'], 24690)
266 self.assertEquals(v['b'], 24690)
267 self.assertRaisesRemote(ZeroDivisionError, ar.get)
267 self.assertRaisesRemote(ZeroDivisionError, ar.get)
268
268
269 def test_autopx_nonblocking(self):
269 def test_autopx_nonblocking(self):
270 ip = get_ipython()
270 ip = get_ipython()
271 v = self.client[-1]
271 v = self.client[-1]
272 v.activate()
272 v.activate()
273 v.block=False
273 v.block=False
274
274
275 with capture_output() as io:
275 with capture_output() as io:
276 ip.magic('autopx')
276 ip.magic('autopx')
277 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
277 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
278 ip.run_cell('print (b)')
278 ip.run_cell('print (b)')
279 ip.run_cell('import time; time.sleep(0.1)')
279 ip.run_cell('import time; time.sleep(0.1)')
280 ip.run_cell("b/c")
280 ip.run_cell("b/c")
281 ip.run_cell('b*=2')
281 ip.run_cell('b*=2')
282 ip.magic('autopx')
282 ip.magic('autopx')
283
283
284 output = io.stdout.rstrip()
284 output = io.stdout.rstrip()
285
285
286 self.assertTrue(output.startswith('%autopx enabled'))
286 self.assertTrue(output.startswith('%autopx enabled'))
287 self.assertTrue(output.endswith('%autopx disabled'))
287 self.assertTrue(output.endswith('%autopx disabled'))
288 self.assertFalse('ZeroDivisionError' in output)
288 self.assertFalse('ZeroDivisionError' in output)
289 ar = v.get_result(-2)
289 ar = v.get_result(-2)
290 self.assertRaisesRemote(ZeroDivisionError, ar.get)
290 self.assertRaisesRemote(ZeroDivisionError, ar.get)
291 # prevent TaskAborted on pulls, due to ZeroDivisionError
291 # prevent TaskAborted on pulls, due to ZeroDivisionError
292 time.sleep(0.5)
292 time.sleep(0.5)
293 self.assertEquals(v['a'], 5)
293 self.assertEquals(v['a'], 5)
294 # b*=2 will not fire, due to abort
294 # b*=2 will not fire, due to abort
295 self.assertEquals(v['b'], 10)
295 self.assertEquals(v['b'], 10)
296
296
297 def test_result(self):
297 def test_result(self):
298 ip = get_ipython()
298 ip = get_ipython()
299 v = self.client[-1]
299 v = self.client[-1]
300 v.activate()
300 v.activate()
301 data = dict(a=111,b=222)
301 data = dict(a=111,b=222)
302 v.push(data, block=True)
302 v.push(data, block=True)
303
303
304 ip.magic('px a')
304 ip.magic('px a')
305 ip.magic('px b')
305 ip.magic('px b')
306 for idx, name in [
306 for idx, name in [
307 ('', 'b'),
307 ('', 'b'),
308 ('-1', 'b'),
308 ('-1', 'b'),
309 ('2', 'b'),
309 ('2', 'b'),
310 ('1', 'a'),
310 ('1', 'a'),
311 ('-2', 'a'),
311 ('-2', 'a'),
312 ]:
312 ]:
313 with capture_output() as io:
313 with capture_output() as io:
314 ip.magic('result ' + idx)
314 ip.magic('result ' + idx)
315 output = io.stdout
315 output = io.stdout
316 msg = "expected %s output to include %s, but got: %s" % \
316 msg = "expected %s output to include %s, but got: %s" % \
317 ('%result '+idx, str(data[name]), output)
317 ('%result '+idx, str(data[name]), output)
318 self.assertTrue(str(data[name]) in output, msg)
318 self.assertTrue(str(data[name]) in output, msg)
319
319
320 @dec.skipif_not_matplotlib
320 @dec.skipif_not_matplotlib
321 def test_px_pylab(self):
321 def test_px_pylab(self):
322 """%pylab works on engines"""
322 """%pylab works on engines"""
323 ip = get_ipython()
323 ip = get_ipython()
324 v = self.client[-1]
324 v = self.client[-1]
325 v.block = True
325 v.block = True
326 v.activate()
326 v.activate()
327
327
328 with capture_output() as io:
328 with capture_output() as io:
329 ip.magic("px %pylab inline")
329 ip.magic("px %pylab inline")
330
330
331 self.assertTrue("Welcome to pylab" in io.stdout, io.stdout)
331 self.assertTrue("Welcome to pylab" in io.stdout, io.stdout)
332 self.assertTrue("backend_inline" in io.stdout, io.stdout)
332 self.assertTrue("backend_inline" in io.stdout, io.stdout)
333
333
334 with capture_output() as io:
334 with capture_output() as io:
335 ip.magic("px plot(rand(100))")
335 ip.magic("px plot(rand(100))")
336
336
337 self.assertTrue('Out[' in io.stdout, io.stdout)
337 self.assertTrue('Out[' in io.stdout, io.stdout)
338 self.assertTrue('matplotlib.lines' in io.stdout, io.stdout)
338 self.assertTrue('matplotlib.lines' in io.stdout, io.stdout)
339
339
340
340
General Comments 0
You need to be logged in to leave comments. Login now