##// END OF EJS Templates
explicit dtype for str in recarray test...
MinRK -
Show More
@@ -1,589 +1,589 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """test View objects
2 """test View objects
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 sys
19 import sys
20 import time
20 import time
21 from tempfile import mktemp
21 from tempfile import mktemp
22 from StringIO import StringIO
22 from StringIO import StringIO
23
23
24 import zmq
24 import zmq
25 from nose import SkipTest
25 from nose import SkipTest
26
26
27 from IPython.testing import decorators as dec
27 from IPython.testing import decorators as dec
28 from IPython.testing.ipunittest import ParametricTestCase
28 from IPython.testing.ipunittest import ParametricTestCase
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, AsyncHubResult, AsyncMapResult
32 from IPython.parallel import AsyncResult, AsyncHubResult, AsyncMapResult
33 from IPython.parallel import DirectView
33 from IPython.parallel import DirectView
34 from IPython.parallel.util import interactive
34 from IPython.parallel.util import interactive
35
35
36 from IPython.parallel.tests import add_engines
36 from IPython.parallel.tests import add_engines
37
37
38 from .clienttest import ClusterTestCase, crash, wait, skip_without
38 from .clienttest import ClusterTestCase, crash, wait, skip_without
39
39
40 def setup():
40 def setup():
41 add_engines(3, total=True)
41 add_engines(3, total=True)
42
42
43 class TestView(ClusterTestCase, ParametricTestCase):
43 class TestView(ClusterTestCase, ParametricTestCase):
44
44
45 def test_z_crash_mux(self):
45 def test_z_crash_mux(self):
46 """test graceful handling of engine death (direct)"""
46 """test graceful handling of engine death (direct)"""
47 raise SkipTest("crash tests disabled, due to undesirable crash reports")
47 raise SkipTest("crash tests disabled, due to undesirable crash reports")
48 # self.add_engines(1)
48 # self.add_engines(1)
49 eid = self.client.ids[-1]
49 eid = self.client.ids[-1]
50 ar = self.client[eid].apply_async(crash)
50 ar = self.client[eid].apply_async(crash)
51 self.assertRaisesRemote(error.EngineError, ar.get, 10)
51 self.assertRaisesRemote(error.EngineError, ar.get, 10)
52 eid = ar.engine_id
52 eid = ar.engine_id
53 tic = time.time()
53 tic = time.time()
54 while eid in self.client.ids and time.time()-tic < 5:
54 while eid in self.client.ids and time.time()-tic < 5:
55 time.sleep(.01)
55 time.sleep(.01)
56 self.client.spin()
56 self.client.spin()
57 self.assertFalse(eid in self.client.ids, "Engine should have died")
57 self.assertFalse(eid in self.client.ids, "Engine should have died")
58
58
59 def test_push_pull(self):
59 def test_push_pull(self):
60 """test pushing and pulling"""
60 """test pushing and pulling"""
61 data = dict(a=10, b=1.05, c=range(10), d={'e':(1,2),'f':'hi'})
61 data = dict(a=10, b=1.05, c=range(10), d={'e':(1,2),'f':'hi'})
62 t = self.client.ids[-1]
62 t = self.client.ids[-1]
63 v = self.client[t]
63 v = self.client[t]
64 push = v.push
64 push = v.push
65 pull = v.pull
65 pull = v.pull
66 v.block=True
66 v.block=True
67 nengines = len(self.client)
67 nengines = len(self.client)
68 push({'data':data})
68 push({'data':data})
69 d = pull('data')
69 d = pull('data')
70 self.assertEquals(d, data)
70 self.assertEquals(d, data)
71 self.client[:].push({'data':data})
71 self.client[:].push({'data':data})
72 d = self.client[:].pull('data', block=True)
72 d = self.client[:].pull('data', block=True)
73 self.assertEquals(d, nengines*[data])
73 self.assertEquals(d, nengines*[data])
74 ar = push({'data':data}, block=False)
74 ar = push({'data':data}, block=False)
75 self.assertTrue(isinstance(ar, AsyncResult))
75 self.assertTrue(isinstance(ar, AsyncResult))
76 r = ar.get()
76 r = ar.get()
77 ar = self.client[:].pull('data', block=False)
77 ar = self.client[:].pull('data', block=False)
78 self.assertTrue(isinstance(ar, AsyncResult))
78 self.assertTrue(isinstance(ar, AsyncResult))
79 r = ar.get()
79 r = ar.get()
80 self.assertEquals(r, nengines*[data])
80 self.assertEquals(r, nengines*[data])
81 self.client[:].push(dict(a=10,b=20))
81 self.client[:].push(dict(a=10,b=20))
82 r = self.client[:].pull(('a','b'), block=True)
82 r = self.client[:].pull(('a','b'), block=True)
83 self.assertEquals(r, nengines*[[10,20]])
83 self.assertEquals(r, nengines*[[10,20]])
84
84
85 def test_push_pull_function(self):
85 def test_push_pull_function(self):
86 "test pushing and pulling functions"
86 "test pushing and pulling functions"
87 def testf(x):
87 def testf(x):
88 return 2.0*x
88 return 2.0*x
89
89
90 t = self.client.ids[-1]
90 t = self.client.ids[-1]
91 v = self.client[t]
91 v = self.client[t]
92 v.block=True
92 v.block=True
93 push = v.push
93 push = v.push
94 pull = v.pull
94 pull = v.pull
95 execute = v.execute
95 execute = v.execute
96 push({'testf':testf})
96 push({'testf':testf})
97 r = pull('testf')
97 r = pull('testf')
98 self.assertEqual(r(1.0), testf(1.0))
98 self.assertEqual(r(1.0), testf(1.0))
99 execute('r = testf(10)')
99 execute('r = testf(10)')
100 r = pull('r')
100 r = pull('r')
101 self.assertEquals(r, testf(10))
101 self.assertEquals(r, testf(10))
102 ar = self.client[:].push({'testf':testf}, block=False)
102 ar = self.client[:].push({'testf':testf}, block=False)
103 ar.get()
103 ar.get()
104 ar = self.client[:].pull('testf', block=False)
104 ar = self.client[:].pull('testf', block=False)
105 rlist = ar.get()
105 rlist = ar.get()
106 for r in rlist:
106 for r in rlist:
107 self.assertEqual(r(1.0), testf(1.0))
107 self.assertEqual(r(1.0), testf(1.0))
108 execute("def g(x): return x*x")
108 execute("def g(x): return x*x")
109 r = pull(('testf','g'))
109 r = pull(('testf','g'))
110 self.assertEquals((r[0](10),r[1](10)), (testf(10), 100))
110 self.assertEquals((r[0](10),r[1](10)), (testf(10), 100))
111
111
112 def test_push_function_globals(self):
112 def test_push_function_globals(self):
113 """test that pushed functions have access to globals"""
113 """test that pushed functions have access to globals"""
114 @interactive
114 @interactive
115 def geta():
115 def geta():
116 return a
116 return a
117 # self.add_engines(1)
117 # self.add_engines(1)
118 v = self.client[-1]
118 v = self.client[-1]
119 v.block=True
119 v.block=True
120 v['f'] = geta
120 v['f'] = geta
121 self.assertRaisesRemote(NameError, v.execute, 'b=f()')
121 self.assertRaisesRemote(NameError, v.execute, 'b=f()')
122 v.execute('a=5')
122 v.execute('a=5')
123 v.execute('b=f()')
123 v.execute('b=f()')
124 self.assertEquals(v['b'], 5)
124 self.assertEquals(v['b'], 5)
125
125
126 def test_push_function_defaults(self):
126 def test_push_function_defaults(self):
127 """test that pushed functions preserve default args"""
127 """test that pushed functions preserve default args"""
128 def echo(a=10):
128 def echo(a=10):
129 return a
129 return a
130 v = self.client[-1]
130 v = self.client[-1]
131 v.block=True
131 v.block=True
132 v['f'] = echo
132 v['f'] = echo
133 v.execute('b=f()')
133 v.execute('b=f()')
134 self.assertEquals(v['b'], 10)
134 self.assertEquals(v['b'], 10)
135
135
136 def test_get_result(self):
136 def test_get_result(self):
137 """test getting results from the Hub."""
137 """test getting results from the Hub."""
138 c = pmod.Client(profile='iptest')
138 c = pmod.Client(profile='iptest')
139 # self.add_engines(1)
139 # self.add_engines(1)
140 t = c.ids[-1]
140 t = c.ids[-1]
141 v = c[t]
141 v = c[t]
142 v2 = self.client[t]
142 v2 = self.client[t]
143 ar = v.apply_async(wait, 1)
143 ar = v.apply_async(wait, 1)
144 # give the monitor time to notice the message
144 # give the monitor time to notice the message
145 time.sleep(.25)
145 time.sleep(.25)
146 ahr = v2.get_result(ar.msg_ids)
146 ahr = v2.get_result(ar.msg_ids)
147 self.assertTrue(isinstance(ahr, AsyncHubResult))
147 self.assertTrue(isinstance(ahr, AsyncHubResult))
148 self.assertEquals(ahr.get(), ar.get())
148 self.assertEquals(ahr.get(), ar.get())
149 ar2 = v2.get_result(ar.msg_ids)
149 ar2 = v2.get_result(ar.msg_ids)
150 self.assertFalse(isinstance(ar2, AsyncHubResult))
150 self.assertFalse(isinstance(ar2, AsyncHubResult))
151 c.spin()
151 c.spin()
152 c.close()
152 c.close()
153
153
154 def test_run_newline(self):
154 def test_run_newline(self):
155 """test that run appends newline to files"""
155 """test that run appends newline to files"""
156 tmpfile = mktemp()
156 tmpfile = mktemp()
157 with open(tmpfile, 'w') as f:
157 with open(tmpfile, 'w') as f:
158 f.write("""def g():
158 f.write("""def g():
159 return 5
159 return 5
160 """)
160 """)
161 v = self.client[-1]
161 v = self.client[-1]
162 v.run(tmpfile, block=True)
162 v.run(tmpfile, block=True)
163 self.assertEquals(v.apply_sync(lambda f: f(), pmod.Reference('g')), 5)
163 self.assertEquals(v.apply_sync(lambda f: f(), pmod.Reference('g')), 5)
164
164
165 def test_apply_tracked(self):
165 def test_apply_tracked(self):
166 """test tracking for apply"""
166 """test tracking for apply"""
167 # self.add_engines(1)
167 # self.add_engines(1)
168 t = self.client.ids[-1]
168 t = self.client.ids[-1]
169 v = self.client[t]
169 v = self.client[t]
170 v.block=False
170 v.block=False
171 def echo(n=1024*1024, **kwargs):
171 def echo(n=1024*1024, **kwargs):
172 with v.temp_flags(**kwargs):
172 with v.temp_flags(**kwargs):
173 return v.apply(lambda x: x, 'x'*n)
173 return v.apply(lambda x: x, 'x'*n)
174 ar = echo(1, track=False)
174 ar = echo(1, track=False)
175 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
175 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
176 self.assertTrue(ar.sent)
176 self.assertTrue(ar.sent)
177 ar = echo(track=True)
177 ar = echo(track=True)
178 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
178 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
179 self.assertEquals(ar.sent, ar._tracker.done)
179 self.assertEquals(ar.sent, ar._tracker.done)
180 ar._tracker.wait()
180 ar._tracker.wait()
181 self.assertTrue(ar.sent)
181 self.assertTrue(ar.sent)
182
182
183 def test_push_tracked(self):
183 def test_push_tracked(self):
184 t = self.client.ids[-1]
184 t = self.client.ids[-1]
185 ns = dict(x='x'*1024*1024)
185 ns = dict(x='x'*1024*1024)
186 v = self.client[t]
186 v = self.client[t]
187 ar = v.push(ns, block=False, track=False)
187 ar = v.push(ns, block=False, track=False)
188 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
188 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
189 self.assertTrue(ar.sent)
189 self.assertTrue(ar.sent)
190
190
191 ar = v.push(ns, block=False, track=True)
191 ar = v.push(ns, block=False, track=True)
192 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
192 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
193 ar._tracker.wait()
193 ar._tracker.wait()
194 self.assertEquals(ar.sent, ar._tracker.done)
194 self.assertEquals(ar.sent, ar._tracker.done)
195 self.assertTrue(ar.sent)
195 self.assertTrue(ar.sent)
196 ar.get()
196 ar.get()
197
197
198 def test_scatter_tracked(self):
198 def test_scatter_tracked(self):
199 t = self.client.ids
199 t = self.client.ids
200 x='x'*1024*1024
200 x='x'*1024*1024
201 ar = self.client[t].scatter('x', x, block=False, track=False)
201 ar = self.client[t].scatter('x', x, block=False, track=False)
202 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
202 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
203 self.assertTrue(ar.sent)
203 self.assertTrue(ar.sent)
204
204
205 ar = self.client[t].scatter('x', x, block=False, track=True)
205 ar = self.client[t].scatter('x', x, block=False, track=True)
206 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
206 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
207 self.assertEquals(ar.sent, ar._tracker.done)
207 self.assertEquals(ar.sent, ar._tracker.done)
208 ar._tracker.wait()
208 ar._tracker.wait()
209 self.assertTrue(ar.sent)
209 self.assertTrue(ar.sent)
210 ar.get()
210 ar.get()
211
211
212 def test_remote_reference(self):
212 def test_remote_reference(self):
213 v = self.client[-1]
213 v = self.client[-1]
214 v['a'] = 123
214 v['a'] = 123
215 ra = pmod.Reference('a')
215 ra = pmod.Reference('a')
216 b = v.apply_sync(lambda x: x, ra)
216 b = v.apply_sync(lambda x: x, ra)
217 self.assertEquals(b, 123)
217 self.assertEquals(b, 123)
218
218
219
219
220 def test_scatter_gather(self):
220 def test_scatter_gather(self):
221 view = self.client[:]
221 view = self.client[:]
222 seq1 = range(16)
222 seq1 = range(16)
223 view.scatter('a', seq1)
223 view.scatter('a', seq1)
224 seq2 = view.gather('a', block=True)
224 seq2 = view.gather('a', block=True)
225 self.assertEquals(seq2, seq1)
225 self.assertEquals(seq2, seq1)
226 self.assertRaisesRemote(NameError, view.gather, 'asdf', block=True)
226 self.assertRaisesRemote(NameError, view.gather, 'asdf', block=True)
227
227
228 @skip_without('numpy')
228 @skip_without('numpy')
229 def test_scatter_gather_numpy(self):
229 def test_scatter_gather_numpy(self):
230 import numpy
230 import numpy
231 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
231 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
232 view = self.client[:]
232 view = self.client[:]
233 a = numpy.arange(64)
233 a = numpy.arange(64)
234 view.scatter('a', a)
234 view.scatter('a', a)
235 b = view.gather('a', block=True)
235 b = view.gather('a', block=True)
236 assert_array_equal(b, a)
236 assert_array_equal(b, a)
237
237
238 def test_scatter_gather_lazy(self):
238 def test_scatter_gather_lazy(self):
239 """scatter/gather with targets='all'"""
239 """scatter/gather with targets='all'"""
240 view = self.client.direct_view(targets='all')
240 view = self.client.direct_view(targets='all')
241 x = range(64)
241 x = range(64)
242 view.scatter('x', x)
242 view.scatter('x', x)
243 gathered = view.gather('x', block=True)
243 gathered = view.gather('x', block=True)
244 self.assertEquals(gathered, x)
244 self.assertEquals(gathered, x)
245
245
246
246
247 @dec.known_failure_py3
247 @dec.known_failure_py3
248 @skip_without('numpy')
248 @skip_without('numpy')
249 def test_push_numpy_nocopy(self):
249 def test_push_numpy_nocopy(self):
250 import numpy
250 import numpy
251 view = self.client[:]
251 view = self.client[:]
252 a = numpy.arange(64)
252 a = numpy.arange(64)
253 view['A'] = a
253 view['A'] = a
254 @interactive
254 @interactive
255 def check_writeable(x):
255 def check_writeable(x):
256 return x.flags.writeable
256 return x.flags.writeable
257
257
258 for flag in view.apply_sync(check_writeable, pmod.Reference('A')):
258 for flag in view.apply_sync(check_writeable, pmod.Reference('A')):
259 self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
259 self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
260
260
261 view.push(dict(B=a))
261 view.push(dict(B=a))
262 for flag in view.apply_sync(check_writeable, pmod.Reference('B')):
262 for flag in view.apply_sync(check_writeable, pmod.Reference('B')):
263 self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
263 self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
264
264
265 @skip_without('numpy')
265 @skip_without('numpy')
266 def test_apply_numpy(self):
266 def test_apply_numpy(self):
267 """view.apply(f, ndarray)"""
267 """view.apply(f, ndarray)"""
268 import numpy
268 import numpy
269 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
269 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
270
270
271 A = numpy.random.random((100,100))
271 A = numpy.random.random((100,100))
272 view = self.client[-1]
272 view = self.client[-1]
273 for dt in [ 'int32', 'uint8', 'float32', 'float64' ]:
273 for dt in [ 'int32', 'uint8', 'float32', 'float64' ]:
274 B = A.astype(dt)
274 B = A.astype(dt)
275 C = view.apply_sync(lambda x:x, B)
275 C = view.apply_sync(lambda x:x, B)
276 assert_array_equal(B,C)
276 assert_array_equal(B,C)
277
277
278 @skip_without('numpy')
278 @skip_without('numpy')
279 def test_push_pull_recarray(self):
279 def test_push_pull_recarray(self):
280 """push/pull recarrays"""
280 """push/pull recarrays"""
281 import numpy
281 import numpy
282 from numpy.testing.utils import assert_array_equal
282 from numpy.testing.utils import assert_array_equal
283
283
284 view = self.client[-1]
284 view = self.client[-1]
285
285
286 R = numpy.array([
286 R = numpy.array([
287 (1, 'hi', 0.),
287 (1, 'hi', 0.),
288 (2**30, 'there', 2.5),
288 (2**30, 'there', 2.5),
289 (-99999, 'world', -12345.6789),
289 (-99999, 'world', -12345.6789),
290 ], [('n', int), ('s', str), ('f', float)])
290 ], [('n', int), ('s', '|S10'), ('f', float)])
291
291
292 view['RR'] = R
292 view['RR'] = R
293 R2 = view['RR']
293 R2 = view['RR']
294
294
295 r_dtype, r_shape = view.apply_sync(interactive(lambda : (RR.dtype, RR.shape)))
295 r_dtype, r_shape = view.apply_sync(interactive(lambda : (RR.dtype, RR.shape)))
296 self.assertEquals(r_dtype, R.dtype)
296 self.assertEquals(r_dtype, R.dtype)
297 self.assertEquals(r_shape, R.shape)
297 self.assertEquals(r_shape, R.shape)
298 self.assertEquals(R2.dtype, R.dtype)
298 self.assertEquals(R2.dtype, R.dtype)
299 self.assertEquals(R2.shape, R.shape)
299 self.assertEquals(R2.shape, R.shape)
300 assert_array_equal(R2, R)
300 assert_array_equal(R2, R)
301
301
302 def test_map(self):
302 def test_map(self):
303 view = self.client[:]
303 view = self.client[:]
304 def f(x):
304 def f(x):
305 return x**2
305 return x**2
306 data = range(16)
306 data = range(16)
307 r = view.map_sync(f, data)
307 r = view.map_sync(f, data)
308 self.assertEquals(r, map(f, data))
308 self.assertEquals(r, map(f, data))
309
309
310 def test_map_iterable(self):
310 def test_map_iterable(self):
311 """test map on iterables (direct)"""
311 """test map on iterables (direct)"""
312 view = self.client[:]
312 view = self.client[:]
313 # 101 is prime, so it won't be evenly distributed
313 # 101 is prime, so it won't be evenly distributed
314 arr = range(101)
314 arr = range(101)
315 # ensure it will be an iterator, even in Python 3
315 # ensure it will be an iterator, even in Python 3
316 it = iter(arr)
316 it = iter(arr)
317 r = view.map_sync(lambda x:x, arr)
317 r = view.map_sync(lambda x:x, arr)
318 self.assertEquals(r, list(arr))
318 self.assertEquals(r, list(arr))
319
319
320 def test_scatterGatherNonblocking(self):
320 def test_scatterGatherNonblocking(self):
321 data = range(16)
321 data = range(16)
322 view = self.client[:]
322 view = self.client[:]
323 view.scatter('a', data, block=False)
323 view.scatter('a', data, block=False)
324 ar = view.gather('a', block=False)
324 ar = view.gather('a', block=False)
325 self.assertEquals(ar.get(), data)
325 self.assertEquals(ar.get(), data)
326
326
327 @skip_without('numpy')
327 @skip_without('numpy')
328 def test_scatter_gather_numpy_nonblocking(self):
328 def test_scatter_gather_numpy_nonblocking(self):
329 import numpy
329 import numpy
330 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
330 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
331 a = numpy.arange(64)
331 a = numpy.arange(64)
332 view = self.client[:]
332 view = self.client[:]
333 ar = view.scatter('a', a, block=False)
333 ar = view.scatter('a', a, block=False)
334 self.assertTrue(isinstance(ar, AsyncResult))
334 self.assertTrue(isinstance(ar, AsyncResult))
335 amr = view.gather('a', block=False)
335 amr = view.gather('a', block=False)
336 self.assertTrue(isinstance(amr, AsyncMapResult))
336 self.assertTrue(isinstance(amr, AsyncMapResult))
337 assert_array_equal(amr.get(), a)
337 assert_array_equal(amr.get(), a)
338
338
339 def test_execute(self):
339 def test_execute(self):
340 view = self.client[:]
340 view = self.client[:]
341 # self.client.debug=True
341 # self.client.debug=True
342 execute = view.execute
342 execute = view.execute
343 ar = execute('c=30', block=False)
343 ar = execute('c=30', block=False)
344 self.assertTrue(isinstance(ar, AsyncResult))
344 self.assertTrue(isinstance(ar, AsyncResult))
345 ar = execute('d=[0,1,2]', block=False)
345 ar = execute('d=[0,1,2]', block=False)
346 self.client.wait(ar, 1)
346 self.client.wait(ar, 1)
347 self.assertEquals(len(ar.get()), len(self.client))
347 self.assertEquals(len(ar.get()), len(self.client))
348 for c in view['c']:
348 for c in view['c']:
349 self.assertEquals(c, 30)
349 self.assertEquals(c, 30)
350
350
351 def test_abort(self):
351 def test_abort(self):
352 view = self.client[-1]
352 view = self.client[-1]
353 ar = view.execute('import time; time.sleep(1)', block=False)
353 ar = view.execute('import time; time.sleep(1)', block=False)
354 ar2 = view.apply_async(lambda : 2)
354 ar2 = view.apply_async(lambda : 2)
355 ar3 = view.apply_async(lambda : 3)
355 ar3 = view.apply_async(lambda : 3)
356 view.abort(ar2)
356 view.abort(ar2)
357 view.abort(ar3.msg_ids)
357 view.abort(ar3.msg_ids)
358 self.assertRaises(error.TaskAborted, ar2.get)
358 self.assertRaises(error.TaskAborted, ar2.get)
359 self.assertRaises(error.TaskAborted, ar3.get)
359 self.assertRaises(error.TaskAborted, ar3.get)
360
360
361 def test_abort_all(self):
361 def test_abort_all(self):
362 """view.abort() aborts all outstanding tasks"""
362 """view.abort() aborts all outstanding tasks"""
363 view = self.client[-1]
363 view = self.client[-1]
364 ars = [ view.apply_async(time.sleep, 0.25) for i in range(10) ]
364 ars = [ view.apply_async(time.sleep, 0.25) for i in range(10) ]
365 view.abort()
365 view.abort()
366 view.wait(timeout=5)
366 view.wait(timeout=5)
367 for ar in ars[5:]:
367 for ar in ars[5:]:
368 self.assertRaises(error.TaskAborted, ar.get)
368 self.assertRaises(error.TaskAborted, ar.get)
369
369
370 def test_temp_flags(self):
370 def test_temp_flags(self):
371 view = self.client[-1]
371 view = self.client[-1]
372 view.block=True
372 view.block=True
373 with view.temp_flags(block=False):
373 with view.temp_flags(block=False):
374 self.assertFalse(view.block)
374 self.assertFalse(view.block)
375 self.assertTrue(view.block)
375 self.assertTrue(view.block)
376
376
377 @dec.known_failure_py3
377 @dec.known_failure_py3
378 def test_importer(self):
378 def test_importer(self):
379 view = self.client[-1]
379 view = self.client[-1]
380 view.clear(block=True)
380 view.clear(block=True)
381 with view.importer:
381 with view.importer:
382 import re
382 import re
383
383
384 @interactive
384 @interactive
385 def findall(pat, s):
385 def findall(pat, s):
386 # this globals() step isn't necessary in real code
386 # this globals() step isn't necessary in real code
387 # only to prevent a closure in the test
387 # only to prevent a closure in the test
388 re = globals()['re']
388 re = globals()['re']
389 return re.findall(pat, s)
389 return re.findall(pat, s)
390
390
391 self.assertEquals(view.apply_sync(findall, '\w+', 'hello world'), 'hello world'.split())
391 self.assertEquals(view.apply_sync(findall, '\w+', 'hello world'), 'hello world'.split())
392
392
393 def test_unicode_execute(self):
393 def test_unicode_execute(self):
394 """test executing unicode strings"""
394 """test executing unicode strings"""
395 v = self.client[-1]
395 v = self.client[-1]
396 v.block=True
396 v.block=True
397 if sys.version_info[0] >= 3:
397 if sys.version_info[0] >= 3:
398 code="a='é'"
398 code="a='é'"
399 else:
399 else:
400 code=u"a=u'é'"
400 code=u"a=u'é'"
401 v.execute(code)
401 v.execute(code)
402 self.assertEquals(v['a'], u'é')
402 self.assertEquals(v['a'], u'é')
403
403
404 def test_unicode_apply_result(self):
404 def test_unicode_apply_result(self):
405 """test unicode apply results"""
405 """test unicode apply results"""
406 v = self.client[-1]
406 v = self.client[-1]
407 r = v.apply_sync(lambda : u'é')
407 r = v.apply_sync(lambda : u'é')
408 self.assertEquals(r, u'é')
408 self.assertEquals(r, u'é')
409
409
410 def test_unicode_apply_arg(self):
410 def test_unicode_apply_arg(self):
411 """test passing unicode arguments to apply"""
411 """test passing unicode arguments to apply"""
412 v = self.client[-1]
412 v = self.client[-1]
413
413
414 @interactive
414 @interactive
415 def check_unicode(a, check):
415 def check_unicode(a, check):
416 assert isinstance(a, unicode), "%r is not unicode"%a
416 assert isinstance(a, unicode), "%r is not unicode"%a
417 assert isinstance(check, bytes), "%r is not bytes"%check
417 assert isinstance(check, bytes), "%r is not bytes"%check
418 assert a.encode('utf8') == check, "%s != %s"%(a,check)
418 assert a.encode('utf8') == check, "%s != %s"%(a,check)
419
419
420 for s in [ u'é', u'ßø®∫',u'asdf' ]:
420 for s in [ u'é', u'ßø®∫',u'asdf' ]:
421 try:
421 try:
422 v.apply_sync(check_unicode, s, s.encode('utf8'))
422 v.apply_sync(check_unicode, s, s.encode('utf8'))
423 except error.RemoteError as e:
423 except error.RemoteError as e:
424 if e.ename == 'AssertionError':
424 if e.ename == 'AssertionError':
425 self.fail(e.evalue)
425 self.fail(e.evalue)
426 else:
426 else:
427 raise e
427 raise e
428
428
429 def test_map_reference(self):
429 def test_map_reference(self):
430 """view.map(<Reference>, *seqs) should work"""
430 """view.map(<Reference>, *seqs) should work"""
431 v = self.client[:]
431 v = self.client[:]
432 v.scatter('n', self.client.ids, flatten=True)
432 v.scatter('n', self.client.ids, flatten=True)
433 v.execute("f = lambda x,y: x*y")
433 v.execute("f = lambda x,y: x*y")
434 rf = pmod.Reference('f')
434 rf = pmod.Reference('f')
435 nlist = list(range(10))
435 nlist = list(range(10))
436 mlist = nlist[::-1]
436 mlist = nlist[::-1]
437 expected = [ m*n for m,n in zip(mlist, nlist) ]
437 expected = [ m*n for m,n in zip(mlist, nlist) ]
438 result = v.map_sync(rf, mlist, nlist)
438 result = v.map_sync(rf, mlist, nlist)
439 self.assertEquals(result, expected)
439 self.assertEquals(result, expected)
440
440
441 def test_apply_reference(self):
441 def test_apply_reference(self):
442 """view.apply(<Reference>, *args) should work"""
442 """view.apply(<Reference>, *args) should work"""
443 v = self.client[:]
443 v = self.client[:]
444 v.scatter('n', self.client.ids, flatten=True)
444 v.scatter('n', self.client.ids, flatten=True)
445 v.execute("f = lambda x: n*x")
445 v.execute("f = lambda x: n*x")
446 rf = pmod.Reference('f')
446 rf = pmod.Reference('f')
447 result = v.apply_sync(rf, 5)
447 result = v.apply_sync(rf, 5)
448 expected = [ 5*id for id in self.client.ids ]
448 expected = [ 5*id for id in self.client.ids ]
449 self.assertEquals(result, expected)
449 self.assertEquals(result, expected)
450
450
451 def test_eval_reference(self):
451 def test_eval_reference(self):
452 v = self.client[self.client.ids[0]]
452 v = self.client[self.client.ids[0]]
453 v['g'] = range(5)
453 v['g'] = range(5)
454 rg = pmod.Reference('g[0]')
454 rg = pmod.Reference('g[0]')
455 echo = lambda x:x
455 echo = lambda x:x
456 self.assertEquals(v.apply_sync(echo, rg), 0)
456 self.assertEquals(v.apply_sync(echo, rg), 0)
457
457
458 def test_reference_nameerror(self):
458 def test_reference_nameerror(self):
459 v = self.client[self.client.ids[0]]
459 v = self.client[self.client.ids[0]]
460 r = pmod.Reference('elvis_has_left')
460 r = pmod.Reference('elvis_has_left')
461 echo = lambda x:x
461 echo = lambda x:x
462 self.assertRaisesRemote(NameError, v.apply_sync, echo, r)
462 self.assertRaisesRemote(NameError, v.apply_sync, echo, r)
463
463
464 def test_single_engine_map(self):
464 def test_single_engine_map(self):
465 e0 = self.client[self.client.ids[0]]
465 e0 = self.client[self.client.ids[0]]
466 r = range(5)
466 r = range(5)
467 check = [ -1*i for i in r ]
467 check = [ -1*i for i in r ]
468 result = e0.map_sync(lambda x: -1*x, r)
468 result = e0.map_sync(lambda x: -1*x, r)
469 self.assertEquals(result, check)
469 self.assertEquals(result, check)
470
470
471 def test_len(self):
471 def test_len(self):
472 """len(view) makes sense"""
472 """len(view) makes sense"""
473 e0 = self.client[self.client.ids[0]]
473 e0 = self.client[self.client.ids[0]]
474 yield self.assertEquals(len(e0), 1)
474 yield self.assertEquals(len(e0), 1)
475 v = self.client[:]
475 v = self.client[:]
476 yield self.assertEquals(len(v), len(self.client.ids))
476 yield self.assertEquals(len(v), len(self.client.ids))
477 v = self.client.direct_view('all')
477 v = self.client.direct_view('all')
478 yield self.assertEquals(len(v), len(self.client.ids))
478 yield self.assertEquals(len(v), len(self.client.ids))
479 v = self.client[:2]
479 v = self.client[:2]
480 yield self.assertEquals(len(v), 2)
480 yield self.assertEquals(len(v), 2)
481 v = self.client[:1]
481 v = self.client[:1]
482 yield self.assertEquals(len(v), 1)
482 yield self.assertEquals(len(v), 1)
483 v = self.client.load_balanced_view()
483 v = self.client.load_balanced_view()
484 yield self.assertEquals(len(v), len(self.client.ids))
484 yield self.assertEquals(len(v), len(self.client.ids))
485 # parametric tests seem to require manual closing?
485 # parametric tests seem to require manual closing?
486 self.client.close()
486 self.client.close()
487
487
488
488
489 # begin execute tests
489 # begin execute tests
490
490
491 def test_execute_reply(self):
491 def test_execute_reply(self):
492 e0 = self.client[self.client.ids[0]]
492 e0 = self.client[self.client.ids[0]]
493 e0.block = True
493 e0.block = True
494 ar = e0.execute("5", silent=False)
494 ar = e0.execute("5", silent=False)
495 er = ar.get()
495 er = ar.get()
496 self.assertEquals(str(er), "<ExecuteReply[%i]: 5>" % er.execution_count)
496 self.assertEquals(str(er), "<ExecuteReply[%i]: 5>" % er.execution_count)
497 self.assertEquals(er.pyout['data']['text/plain'], '5')
497 self.assertEquals(er.pyout['data']['text/plain'], '5')
498
498
499 def test_execute_reply_stdout(self):
499 def test_execute_reply_stdout(self):
500 e0 = self.client[self.client.ids[0]]
500 e0 = self.client[self.client.ids[0]]
501 e0.block = True
501 e0.block = True
502 ar = e0.execute("print (5)", silent=False)
502 ar = e0.execute("print (5)", silent=False)
503 er = ar.get()
503 er = ar.get()
504 self.assertEquals(er.stdout.strip(), '5')
504 self.assertEquals(er.stdout.strip(), '5')
505
505
506 def test_execute_pyout(self):
506 def test_execute_pyout(self):
507 """execute triggers pyout with silent=False"""
507 """execute triggers pyout with silent=False"""
508 view = self.client[:]
508 view = self.client[:]
509 ar = view.execute("5", silent=False, block=True)
509 ar = view.execute("5", silent=False, block=True)
510
510
511 expected = [{'text/plain' : '5'}] * len(view)
511 expected = [{'text/plain' : '5'}] * len(view)
512 mimes = [ out['data'] for out in ar.pyout ]
512 mimes = [ out['data'] for out in ar.pyout ]
513 self.assertEquals(mimes, expected)
513 self.assertEquals(mimes, expected)
514
514
515 def test_execute_silent(self):
515 def test_execute_silent(self):
516 """execute does not trigger pyout with silent=True"""
516 """execute does not trigger pyout with silent=True"""
517 view = self.client[:]
517 view = self.client[:]
518 ar = view.execute("5", block=True)
518 ar = view.execute("5", block=True)
519 expected = [None] * len(view)
519 expected = [None] * len(view)
520 self.assertEquals(ar.pyout, expected)
520 self.assertEquals(ar.pyout, expected)
521
521
522 def test_execute_magic(self):
522 def test_execute_magic(self):
523 """execute accepts IPython commands"""
523 """execute accepts IPython commands"""
524 view = self.client[:]
524 view = self.client[:]
525 view.execute("a = 5")
525 view.execute("a = 5")
526 ar = view.execute("%whos", block=True)
526 ar = view.execute("%whos", block=True)
527 # this will raise, if that failed
527 # this will raise, if that failed
528 ar.get(5)
528 ar.get(5)
529 for stdout in ar.stdout:
529 for stdout in ar.stdout:
530 lines = stdout.splitlines()
530 lines = stdout.splitlines()
531 self.assertEquals(lines[0].split(), ['Variable', 'Type', 'Data/Info'])
531 self.assertEquals(lines[0].split(), ['Variable', 'Type', 'Data/Info'])
532 found = False
532 found = False
533 for line in lines[2:]:
533 for line in lines[2:]:
534 split = line.split()
534 split = line.split()
535 if split == ['a', 'int', '5']:
535 if split == ['a', 'int', '5']:
536 found = True
536 found = True
537 break
537 break
538 self.assertTrue(found, "whos output wrong: %s" % stdout)
538 self.assertTrue(found, "whos output wrong: %s" % stdout)
539
539
540 def test_execute_displaypub(self):
540 def test_execute_displaypub(self):
541 """execute tracks display_pub output"""
541 """execute tracks display_pub output"""
542 view = self.client[:]
542 view = self.client[:]
543 view.execute("from IPython.core.display import *")
543 view.execute("from IPython.core.display import *")
544 ar = view.execute("[ display(i) for i in range(5) ]", block=True)
544 ar = view.execute("[ display(i) for i in range(5) ]", block=True)
545
545
546 expected = [ {u'text/plain' : unicode(j)} for j in range(5) ]
546 expected = [ {u'text/plain' : unicode(j)} for j in range(5) ]
547 for outputs in ar.outputs:
547 for outputs in ar.outputs:
548 mimes = [ out['data'] for out in outputs ]
548 mimes = [ out['data'] for out in outputs ]
549 self.assertEquals(mimes, expected)
549 self.assertEquals(mimes, expected)
550
550
551 def test_apply_displaypub(self):
551 def test_apply_displaypub(self):
552 """apply tracks display_pub output"""
552 """apply tracks display_pub output"""
553 view = self.client[:]
553 view = self.client[:]
554 view.execute("from IPython.core.display import *")
554 view.execute("from IPython.core.display import *")
555
555
556 @interactive
556 @interactive
557 def publish():
557 def publish():
558 [ display(i) for i in range(5) ]
558 [ display(i) for i in range(5) ]
559
559
560 ar = view.apply_async(publish)
560 ar = view.apply_async(publish)
561 ar.get(5)
561 ar.get(5)
562 expected = [ {u'text/plain' : unicode(j)} for j in range(5) ]
562 expected = [ {u'text/plain' : unicode(j)} for j in range(5) ]
563 for outputs in ar.outputs:
563 for outputs in ar.outputs:
564 mimes = [ out['data'] for out in outputs ]
564 mimes = [ out['data'] for out in outputs ]
565 self.assertEquals(mimes, expected)
565 self.assertEquals(mimes, expected)
566
566
567 def test_execute_raises(self):
567 def test_execute_raises(self):
568 """exceptions in execute requests raise appropriately"""
568 """exceptions in execute requests raise appropriately"""
569 view = self.client[-1]
569 view = self.client[-1]
570 ar = view.execute("1/0")
570 ar = view.execute("1/0")
571 self.assertRaisesRemote(ZeroDivisionError, ar.get, 2)
571 self.assertRaisesRemote(ZeroDivisionError, ar.get, 2)
572
572
573 @dec.skipif_not_matplotlib
573 @dec.skipif_not_matplotlib
574 def test_magic_pylab(self):
574 def test_magic_pylab(self):
575 """%pylab works on engines"""
575 """%pylab works on engines"""
576 view = self.client[-1]
576 view = self.client[-1]
577 ar = view.execute("%pylab inline")
577 ar = view.execute("%pylab inline")
578 # at least check if this raised:
578 # at least check if this raised:
579 reply = ar.get(5)
579 reply = ar.get(5)
580 # include imports, in case user config
580 # include imports, in case user config
581 ar = view.execute("plot(rand(100))", silent=False)
581 ar = view.execute("plot(rand(100))", silent=False)
582 reply = ar.get(5)
582 reply = ar.get(5)
583 self.assertEquals(len(reply.outputs), 1)
583 self.assertEquals(len(reply.outputs), 1)
584 output = reply.outputs[0]
584 output = reply.outputs[0]
585 self.assertTrue("data" in output)
585 self.assertTrue("data" in output)
586 data = output['data']
586 data = output['data']
587 self.assertTrue("image/png" in data)
587 self.assertTrue("image/png" in data)
588
588
589
589
General Comments 0
You need to be logged in to leave comments. Login now