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