##// END OF EJS Templates
remove mktemp usage...
remove mktemp usage mktemp is insecure, by the time the generated filename is used someone else might have modified it.

File last commit:

r15372:5ce8451d
r15372:5ce8451d
Show More
test_view.py
849 lines | 28.3 KiB | text/x-python | PythonLexer
MinRK
move coding declaration above docstring in test_view...
r4034 # -*- coding: utf-8 -*-
MinRK
update recently changed modules with Authors in docstring
r4018 """test View objects
Authors:
* Min RK
"""
MinRK
update API after sagedays29...
r3664 #-------------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
MinRK
subclass RichOutput in ExecuteReply...
r12229 import base64
MinRK
add tests for parallel magics...
r3736 import sys
Jonathan March
Fix parallel test on WinXP - wait for resource cleanup....
r7682 import platform
MinRK
update API after sagedays29...
r3664 import time
MinRK
test namedtuple as return and apply arg
r9714 from collections import namedtuple
Julian Taylor
remove mktemp usage...
r15372 from tempfile import NamedTemporaryFile
MinRK
update API after sagedays29...
r3664
import zmq
MinRK
tag crash tests with @attr('crash')...
r8200 from nose.plugins.attrib import attr
MinRK
update API after sagedays29...
r3664
MinRK
minor py3 fixes in IPython.parallel...
r5626 from IPython.testing import decorators as dec
MinRK
test Composite/RemoteError tracebacks
r8228 from IPython.utils.io import capture_output
Thomas Kluyver
Replace references to unicode and basestring
r13353 from IPython.utils.py3compat import unicode_type
MinRK
minor py3 fixes in IPython.parallel...
r5626
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 from IPython import parallel as pmod
from IPython.parallel import error
MinRK
organize IPython.parallel into subpackages
r3673 from IPython.parallel import AsyncResult, AsyncHubResult, AsyncMapResult
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 from IPython.parallel.util import interactive
MinRK
update API after sagedays29...
r3664
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 from IPython.parallel.tests import add_engines
MinRK
update API after sagedays29...
r3664
MinRK
use crash that will also kill a Windows engine...
r3776 from .clienttest import ClusterTestCase, crash, wait, skip_without
MinRK
update API after sagedays29...
r3664
def setup():
MinRK
expedite IPython.parallel tests...
r6162 add_engines(3, total=True)
MinRK
update API after sagedays29...
r3664
MinRK
test namedtuple as return and apply arg
r9714 point = namedtuple("point", "x y")
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 class TestView(ClusterTestCase):
MinRK
update API after sagedays29...
r3664
Jonathan March
Fix parallel test on WinXP - wait for resource cleanup....
r7682 def setUp(self):
# On Win XP, wait for resource cleanup, else parallel test group fails
if platform.system() == "Windows" and platform.win32_ver()[0] == "XP":
# 1 sec fails. 1.5 sec seems ok. Using 2 sec for margin of safety
time.sleep(2)
Jonathan March
In parallel test fix, sleep before super instead of after...
r7686 super(TestView, self).setUp()
Jonathan March
Fix parallel test on WinXP - wait for resource cleanup....
r7682
MinRK
tag crash tests with @attr('crash')...
r8200 @attr('crash')
MinRK
add basic unicode tests in test_view...
r3783 def test_z_crash_mux(self):
MinRK
update API after sagedays29...
r3664 """test graceful handling of engine death (direct)"""
# self.add_engines(1)
eid = self.client.ids[-1]
MinRK
use crash that will also kill a Windows engine...
r3776 ar = self.client[eid].apply_async(crash)
MinRK
update parallel code for py3k...
r4155 self.assertRaisesRemote(error.EngineError, ar.get, 10)
MinRK
update API after sagedays29...
r3664 eid = ar.engine_id
MinRK
use crash that will also kill a Windows engine...
r3776 tic = time.time()
while eid in self.client.ids and time.time()-tic < 5:
MinRK
update API after sagedays29...
r3664 time.sleep(.01)
self.client.spin()
MinRK
use crash that will also kill a Windows engine...
r3776 self.assertFalse(eid in self.client.ids, "Engine should have died")
MinRK
update API after sagedays29...
r3664
def test_push_pull(self):
"""test pushing and pulling"""
Thomas Kluyver
Fix parallel test suite
r13383 data = dict(a=10, b=1.05, c=list(range(10)), d={'e':(1,2),'f':'hi'})
MinRK
update API after sagedays29...
r3664 t = self.client.ids[-1]
v = self.client[t]
push = v.push
pull = v.pull
v.block=True
nengines = len(self.client)
push({'data':data})
d = pull('data')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(d, data)
MinRK
update API after sagedays29...
r3664 self.client[:].push({'data':data})
d = self.client[:].pull('data', block=True)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(d, nengines*[data])
MinRK
update API after sagedays29...
r3664 ar = push({'data':data}, block=False)
self.assertTrue(isinstance(ar, AsyncResult))
r = ar.get()
ar = self.client[:].pull('data', block=False)
self.assertTrue(isinstance(ar, AsyncResult))
r = ar.get()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(r, nengines*[data])
MinRK
update API after sagedays29...
r3664 self.client[:].push(dict(a=10,b=20))
MinRK
fix out of sync parallel tests...
r3701 r = self.client[:].pull(('a','b'), block=True)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(r, nengines*[[10,20]])
MinRK
update API after sagedays29...
r3664
def test_push_pull_function(self):
"test pushing and pulling functions"
def testf(x):
return 2.0*x
t = self.client.ids[-1]
MinRK
fix out of sync parallel tests...
r3701 v = self.client[t]
v.block=True
push = v.push
pull = v.pull
execute = v.execute
MinRK
update API after sagedays29...
r3664 push({'testf':testf})
r = pull('testf')
self.assertEqual(r(1.0), testf(1.0))
execute('r = testf(10)')
r = pull('r')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(r, testf(10))
MinRK
update API after sagedays29...
r3664 ar = self.client[:].push({'testf':testf}, block=False)
ar.get()
ar = self.client[:].pull('testf', block=False)
rlist = ar.get()
for r in rlist:
self.assertEqual(r(1.0), testf(1.0))
execute("def g(x): return x*x")
r = pull(('testf','g'))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual((r[0](10),r[1](10)), (testf(10), 100))
MinRK
update API after sagedays29...
r3664
def test_push_function_globals(self):
"""test that pushed functions have access to globals"""
@interactive
def geta():
return a
# self.add_engines(1)
v = self.client[-1]
v.block=True
v['f'] = geta
self.assertRaisesRemote(NameError, v.execute, 'b=f()')
v.execute('a=5')
v.execute('b=f()')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(v['b'], 5)
MinRK
update API after sagedays29...
r3664
def test_push_function_defaults(self):
"""test that pushed functions preserve default args"""
def echo(a=10):
return a
v = self.client[-1]
v.block=True
v['f'] = echo
v.execute('b=f()')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(v['b'], 10)
MinRK
update API after sagedays29...
r3664
def test_get_result(self):
"""test getting results from the Hub."""
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 c = pmod.Client(profile='iptest')
MinRK
update API after sagedays29...
r3664 # self.add_engines(1)
t = c.ids[-1]
v = c[t]
v2 = self.client[t]
ar = v.apply_async(wait, 1)
# give the monitor time to notice the message
time.sleep(.25)
MinRK
update client.get_result to match AsyncResult behavior
r11416 ahr = v2.get_result(ar.msg_ids[0])
MinRK
update API after sagedays29...
r3664 self.assertTrue(isinstance(ahr, AsyncHubResult))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ahr.get(), ar.get())
MinRK
update client.get_result to match AsyncResult behavior
r11416 ar2 = v2.get_result(ar.msg_ids[0])
MinRK
update API after sagedays29...
r3664 self.assertFalse(isinstance(ar2, AsyncHubResult))
c.spin()
c.close()
def test_run_newline(self):
"""test that run appends newline to files"""
Julian Taylor
remove mktemp usage...
r15372 with NamedTemporaryFile('w', delete=False) as f:
MinRK
update API after sagedays29...
r3664 f.write("""def g():
return 5
""")
v = self.client[-1]
Julian Taylor
remove mktemp usage...
r15372 v.run(f.name, block=True)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(v.apply_sync(lambda f: f(), pmod.Reference('g')), 5)
MinRK
update API after sagedays29...
r3664
def test_apply_tracked(self):
"""test tracking for apply"""
# self.add_engines(1)
t = self.client.ids[-1]
v = self.client[t]
v.block=False
def echo(n=1024*1024, **kwargs):
with v.temp_flags(**kwargs):
return v.apply(lambda x: x, 'x'*n)
ar = echo(1, track=False)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
self.assertTrue(ar.sent)
ar = echo(track=True)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ar.sent, ar._tracker.done)
MinRK
update API after sagedays29...
r3664 ar._tracker.wait()
self.assertTrue(ar.sent)
def test_push_tracked(self):
t = self.client.ids[-1]
ns = dict(x='x'*1024*1024)
v = self.client[t]
ar = v.push(ns, block=False, track=False)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
self.assertTrue(ar.sent)
ar = v.push(ns, block=False, track=True)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
ar._tracker.wait()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ar.sent, ar._tracker.done)
MinRK
update API after sagedays29...
r3664 self.assertTrue(ar.sent)
ar.get()
def test_scatter_tracked(self):
t = self.client.ids
x='x'*1024*1024
ar = self.client[t].scatter('x', x, block=False, track=False)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
self.assertTrue(ar.sent)
ar = self.client[t].scatter('x', x, block=False, track=True)
self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ar.sent, ar._tracker.done)
MinRK
update API after sagedays29...
r3664 ar._tracker.wait()
self.assertTrue(ar.sent)
ar.get()
def test_remote_reference(self):
v = self.client[-1]
v['a'] = 123
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 ra = pmod.Reference('a')
MinRK
update API after sagedays29...
r3664 b = v.apply_sync(lambda x: x, ra)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(b, 123)
MinRK
update API after sagedays29...
r3664
def test_scatter_gather(self):
view = self.client[:]
Thomas Kluyver
Fix parallel test suite
r13383 seq1 = list(range(16))
MinRK
update API after sagedays29...
r3664 view.scatter('a', seq1)
seq2 = view.gather('a', block=True)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(seq2, seq1)
MinRK
update API after sagedays29...
r3664 self.assertRaisesRemote(NameError, view.gather, 'asdf', block=True)
@skip_without('numpy')
def test_scatter_gather_numpy(self):
import numpy
Paul Ivanov
clean up imports
r12400 from numpy.testing.utils import assert_array_equal
MinRK
update API after sagedays29...
r3664 view = self.client[:]
a = numpy.arange(64)
MinRK
better serialization for parallel code...
r7967 view.scatter('a', a, block=True)
MinRK
update API after sagedays29...
r3664 b = view.gather('a', block=True)
assert_array_equal(b, a)
MinRK
fix scatter/gather with targets='all'...
r6753
def test_scatter_gather_lazy(self):
"""scatter/gather with targets='all'"""
view = self.client.direct_view(targets='all')
Thomas Kluyver
Fix parallel test suite
r13383 x = list(range(64))
MinRK
fix scatter/gather with targets='all'...
r6753 view.scatter('x', x)
gathered = view.gather('x', block=True)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(gathered, x)
MinRK
fix scatter/gather with targets='all'...
r6753
MinRK
test that pushing numpy arrays is zero-copy
r6244
Thomas Kluyver
Mark test_push_numpy_nocopy as a known failure for Python 3 (gh-1699)
r6726 @dec.known_failure_py3
MinRK
test that pushing numpy arrays is zero-copy
r6244 @skip_without('numpy')
def test_push_numpy_nocopy(self):
import numpy
view = self.client[:]
a = numpy.arange(64)
view['A'] = a
@interactive
def check_writeable(x):
return x.flags.writeable
for flag in view.apply_sync(check_writeable, pmod.Reference('A')):
self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
view.push(dict(B=a))
for flag in view.apply_sync(check_writeable, pmod.Reference('B')):
self.assertFalse(flag, "array is writeable, push shouldn't have pickled it")
MinRK
update API after sagedays29...
r3664
MinRK
fix dangling `buffer` in IPython.parallel.util...
r6187 @skip_without('numpy')
def test_apply_numpy(self):
"""view.apply(f, ndarray)"""
import numpy
Paul Ivanov
clean up imports
r12400 from numpy.testing.utils import assert_array_equal
MinRK
fix dangling `buffer` in IPython.parallel.util...
r6187
A = numpy.random.random((100,100))
view = self.client[-1]
for dt in [ 'int32', 'uint8', 'float32', 'float64' ]:
B = A.astype(dt)
C = view.apply_sync(lambda x:x, B)
assert_array_equal(B,C)
MinRK
add failing test for push/pull recarrays
r7697 @skip_without('numpy')
MinRK
pickle arrays with dtype=object...
r14901 def test_apply_numpy_object_dtype(self):
"""view.apply(f, ndarray) with dtype=object"""
import numpy
from numpy.testing.utils import assert_array_equal
view = self.client[-1]
A = numpy.array([dict(a=5)])
B = view.apply_sync(lambda x:x, A)
assert_array_equal(A,B)
A = numpy.array([(0, dict(b=10))], dtype=[('i', int), ('o', object)])
B = view.apply_sync(lambda x:x, A)
assert_array_equal(A,B)
@skip_without('numpy')
MinRK
add failing test for push/pull recarrays
r7697 def test_push_pull_recarray(self):
"""push/pull recarrays"""
import numpy
from numpy.testing.utils import assert_array_equal
view = self.client[-1]
R = numpy.array([
(1, 'hi', 0.),
(2**30, 'there', 2.5),
(-99999, 'world', -12345.6789),
MinRK
explicit dtype for str in recarray test...
r7699 ], [('n', int), ('s', '|S10'), ('f', float)])
MinRK
add failing test for push/pull recarrays
r7697
view['RR'] = R
R2 = view['RR']
r_dtype, r_shape = view.apply_sync(interactive(lambda : (RR.dtype, RR.shape)))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(r_dtype, R.dtype)
self.assertEqual(r_shape, R.shape)
self.assertEqual(R2.dtype, R.dtype)
self.assertEqual(R2.shape, R.shape)
MinRK
add failing test for push/pull recarrays
r7697 assert_array_equal(R2, R)
MinRK
test push/pull of pandas TimeSeries
r9136
@skip_without('pandas')
def test_push_pull_timeseries(self):
"""push/pull pandas.TimeSeries"""
import pandas
Thomas Kluyver
Fix parallel test suite
r13383 ts = pandas.TimeSeries(list(range(10)))
MinRK
test push/pull of pandas TimeSeries
r9136
view = self.client[-1]
view.push(dict(ts=ts), block=True)
rts = view['ts']
self.assertEqual(type(rts), type(ts))
self.assertTrue((ts == rts).all())
MinRK
add failing test for push/pull recarrays
r7697
MinRK
update API after sagedays29...
r3664 def test_map(self):
view = self.client[:]
def f(x):
return x**2
Thomas Kluyver
Fix parallel test suite
r13383 data = list(range(16))
MinRK
update API after sagedays29...
r3664 r = view.map_sync(f, data)
Thomas Kluyver
Fix parallel test suite
r13383 self.assertEqual(r, list(map(f, data)))
MinRK
update API after sagedays29...
r3664
MinRK
support iterators in view.map...
r5560 def test_map_iterable(self):
"""test map on iterables (direct)"""
view = self.client[:]
# 101 is prime, so it won't be evenly distributed
arr = range(101)
# ensure it will be an iterator, even in Python 3
it = iter(arr)
Samuel Ainsworth
Fix test_map_iterable()
r12175 r = view.map_sync(lambda x: x, it)
Samuel Ainsworth
Fix test_map_iterable() again
r12181 self.assertEqual(r, list(arr))
Samuel Ainsworth
Fix parallel.client.View map() on numpy arrays
r12174
@skip_without('numpy')
def test_map_numpy(self):
"""test map on numpy arrays (direct)"""
import numpy
from numpy.testing.utils import assert_array_equal
view = self.client[:]
# 101 is prime, so it won't be evenly distributed
arr = numpy.arange(101)
r = view.map_sync(lambda x: x, arr)
assert_array_equal(r, arr)
MinRK
support iterators in view.map...
r5560
MinRK
better serialization for parallel code...
r7967 def test_scatter_gather_nonblocking(self):
Thomas Kluyver
Fix parallel test suite
r13383 data = list(range(16))
MinRK
update API after sagedays29...
r3664 view = self.client[:]
view.scatter('a', data, block=False)
ar = view.gather('a', block=False)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ar.get(), data)
MinRK
update API after sagedays29...
r3664
@skip_without('numpy')
def test_scatter_gather_numpy_nonblocking(self):
import numpy
Paul Ivanov
clean up imports
r12400 from numpy.testing.utils import assert_array_equal
MinRK
update API after sagedays29...
r3664 a = numpy.arange(64)
view = self.client[:]
ar = view.scatter('a', a, block=False)
self.assertTrue(isinstance(ar, AsyncResult))
amr = view.gather('a', block=False)
self.assertTrue(isinstance(amr, AsyncMapResult))
assert_array_equal(amr.get(), a)
def test_execute(self):
view = self.client[:]
# self.client.debug=True
execute = view.execute
ar = execute('c=30', block=False)
self.assertTrue(isinstance(ar, AsyncResult))
ar = execute('d=[0,1,2]', block=False)
self.client.wait(ar, 1)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(len(ar.get()), len(self.client))
MinRK
update API after sagedays29...
r3664 for c in view['c']:
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(c, 30)
MinRK
update API after sagedays29...
r3664
def test_abort(self):
view = self.client[-1]
MinRK
further tweaks to parallel tests...
r4580 ar = view.execute('import time; time.sleep(1)', block=False)
MinRK
update API after sagedays29...
r3664 ar2 = view.apply_async(lambda : 2)
ar3 = view.apply_async(lambda : 3)
view.abort(ar2)
view.abort(ar3.msg_ids)
self.assertRaises(error.TaskAborted, ar2.get)
self.assertRaises(error.TaskAborted, ar3.get)
MinRK
view.abort() aborts all outstanding tasks...
r5645
def test_abort_all(self):
"""view.abort() aborts all outstanding tasks"""
view = self.client[-1]
MinRK
expedite IPython.parallel tests...
r6162 ars = [ view.apply_async(time.sleep, 0.25) for i in range(10) ]
MinRK
view.abort() aborts all outstanding tasks...
r5645 view.abort()
view.wait(timeout=5)
for ar in ars[5:]:
self.assertRaises(error.TaskAborted, ar.get)
MinRK
update API after sagedays29...
r3664 def test_temp_flags(self):
view = self.client[-1]
view.block=True
with view.temp_flags(block=False):
self.assertFalse(view.block)
self.assertTrue(view.block)
MinRK
minor py3 fixes in IPython.parallel...
r5626 @dec.known_failure_py3
MinRK
add DirectView.importer contextmanager, demote targets to mutable flag...
r3665 def test_importer(self):
view = self.client[-1]
view.clear(block=True)
with view.importer:
import re
@interactive
def findall(pat, s):
# this globals() step isn't necessary in real code
# only to prevent a closure in the test
MinRK
Start each test with clear engine namespaces...
r3775 re = globals()['re']
return re.findall(pat, s)
MinRK
add DirectView.importer contextmanager, demote targets to mutable flag...
r3665
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(view.apply_sync(findall, '\w+', 'hello world'), 'hello world'.split())
MinRK
add DirectView.importer contextmanager, demote targets to mutable flag...
r3665
MinRK
add basic unicode tests in test_view...
r3783 def test_unicode_execute(self):
"""test executing unicode strings"""
v = self.client[-1]
v.block=True
MinRK
update parallel code for py3k...
r4155 if sys.version_info[0] >= 3:
code="a='é'"
else:
code=u"a=u'é'"
MinRK
add basic unicode tests in test_view...
r3783 v.execute(code)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(v['a'], u'é')
MinRK
add basic unicode tests in test_view...
r3783
def test_unicode_apply_result(self):
"""test unicode apply results"""
v = self.client[-1]
r = v.apply_sync(lambda : u'é')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(r, u'é')
MinRK
add basic unicode tests in test_view...
r3783
def test_unicode_apply_arg(self):
"""test passing unicode arguments to apply"""
v = self.client[-1]
@interactive
def check_unicode(a, check):
Thomas Kluyver
Fix parallel test suite
r13383 assert not isinstance(a, bytes), "%r is bytes, not unicode"%a
MinRK
add basic unicode tests in test_view...
r3783 assert isinstance(check, bytes), "%r is not bytes"%check
assert a.encode('utf8') == check, "%s != %s"%(a,check)
MinRK
update parallel code for py3k...
r4155 for s in [ u'é', u'ßø®∫',u'asdf' ]:
MinRK
add basic unicode tests in test_view...
r3783 try:
v.apply_sync(check_unicode, s, s.encode('utf8'))
except error.RemoteError as e:
if e.ename == 'AssertionError':
self.fail(e.evalue)
else:
raise e
MinRK
support iterators in view.map...
r5560
MinRK
allow Reference as callable in map/apply...
r5821 def test_map_reference(self):
"""view.map(<Reference>, *seqs) should work"""
v = self.client[:]
v.scatter('n', self.client.ids, flatten=True)
v.execute("f = lambda x,y: x*y")
rf = pmod.Reference('f')
nlist = list(range(10))
mlist = nlist[::-1]
expected = [ m*n for m,n in zip(mlist, nlist) ]
result = v.map_sync(rf, mlist, nlist)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(result, expected)
MinRK
allow Reference as callable in map/apply...
r5821
def test_apply_reference(self):
"""view.apply(<Reference>, *args) should work"""
v = self.client[:]
v.scatter('n', self.client.ids, flatten=True)
v.execute("f = lambda x: n*x")
rf = pmod.Reference('f')
result = v.apply_sync(rf, 5)
expected = [ 5*id for id in self.client.ids ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(result, expected)
MinRK
use eval to uncan References...
r6159
def test_eval_reference(self):
v = self.client[self.client.ids[0]]
Thomas Kluyver
Fix parallel test suite
r13383 v['g'] = list(range(5))
MinRK
use eval to uncan References...
r6159 rg = pmod.Reference('g[0]')
echo = lambda x:x
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(v.apply_sync(echo, rg), 0)
MinRK
use eval to uncan References...
r6159
def test_reference_nameerror(self):
v = self.client[self.client.ids[0]]
r = pmod.Reference('elvis_has_left')
echo = lambda x:x
self.assertRaisesRemote(NameError, v.apply_sync, echo, r)
MinRK
allow map / parallel function for single-engine views
r6513 def test_single_engine_map(self):
e0 = self.client[self.client.ids[0]]
Thomas Kluyver
Fix parallel test suite
r13383 r = list(range(5))
MinRK
allow map / parallel function for single-engine views
r6513 check = [ -1*i for i in r ]
result = e0.map_sync(lambda x: -1*x, r)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(result, check)
MinRK
add len(view)...
r6833
def test_len(self):
"""len(view) makes sense"""
e0 = self.client[self.client.ids[0]]
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(e0), 1)
MinRK
add len(view)...
r6833 v = self.client[:]
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(v), len(self.client.ids))
MinRK
add len(view)...
r6833 v = self.client.direct_view('all')
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(v), len(self.client.ids))
MinRK
add len(view)...
r6833 v = self.client[:2]
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(v), 2)
MinRK
add len(view)...
r6833 v = self.client[:1]
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(v), 1)
MinRK
add len(view)...
r6833 v = self.client.load_balanced_view()
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.assertEqual(len(v), len(self.client.ids))
MinRK
add tests for parallel magics...
r3736
MinRK
test new execute/output behaviors
r6835
# begin execute tests
def test_execute_reply(self):
e0 = self.client[self.client.ids[0]]
e0.block = True
ar = e0.execute("5", silent=False)
er = ar.get()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(str(er), "<ExecuteReply[%i]: 5>" % er.execution_count)
self.assertEqual(er.pyout['data']['text/plain'], '5')
MinRK
test new execute/output behaviors
r6835
MinRK
subclass RichOutput in ExecuteReply...
r12229 def test_execute_reply_rich(self):
e0 = self.client[self.client.ids[0]]
e0.block = True
e0.execute("from IPython.display import Image, HTML")
ar = e0.execute("Image(data=b'garbage', format='png', width=10)", silent=False)
er = ar.get()
b64data = base64.encodestring(b'garbage').decode('ascii')
self.assertEqual(er._repr_png_(), (b64data, dict(width=10)))
ar = e0.execute("HTML('<b>bold</b>')", silent=False)
er = ar.get()
self.assertEqual(er._repr_html_(), "<b>bold</b>")
MinRK
test new execute/output behaviors
r6835 def test_execute_reply_stdout(self):
e0 = self.client[self.client.ids[0]]
e0.block = True
ar = e0.execute("print (5)", silent=False)
er = ar.get()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(er.stdout.strip(), '5')
MinRK
test new execute/output behaviors
r6835
def test_execute_pyout(self):
"""execute triggers pyout with silent=False"""
view = self.client[:]
ar = view.execute("5", silent=False, block=True)
MinRK
relax some timing constraints in parallel tests
r6890
MinRK
test new execute/output behaviors
r6835 expected = [{'text/plain' : '5'}] * len(view)
MinRK
store whole content, instead of just display data in metadata.outputs
r6891 mimes = [ out['data'] for out in ar.pyout ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(mimes, expected)
MinRK
test new execute/output behaviors
r6835
def test_execute_silent(self):
"""execute does not trigger pyout with silent=True"""
view = self.client[:]
ar = view.execute("5", block=True)
expected = [None] * len(view)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ar.pyout, expected)
MinRK
test new execute/output behaviors
r6835
def test_execute_magic(self):
"""execute accepts IPython commands"""
view = self.client[:]
view.execute("a = 5")
ar = view.execute("%whos", block=True)
# this will raise, if that failed
ar.get(5)
for stdout in ar.stdout:
lines = stdout.splitlines()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(lines[0].split(), ['Variable', 'Type', 'Data/Info'])
MinRK
test new execute/output behaviors
r6835 found = False
for line in lines[2:]:
split = line.split()
if split == ['a', 'int', '5']:
found = True
break
self.assertTrue(found, "whos output wrong: %s" % stdout)
def test_execute_displaypub(self):
"""execute tracks display_pub output"""
view = self.client[:]
view.execute("from IPython.core.display import *")
ar = view.execute("[ display(i) for i in range(5) ]", block=True)
MinRK
relax some timing constraints in parallel tests
r6890
Thomas Kluyver
Replace references to unicode and basestring
r13353 expected = [ {u'text/plain' : unicode_type(j)} for j in range(5) ]
MinRK
store whole content, instead of just display data in metadata.outputs
r6891 for outputs in ar.outputs:
mimes = [ out['data'] for out in outputs ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(mimes, expected)
MinRK
test new execute/output behaviors
r6835
def test_apply_displaypub(self):
"""apply tracks display_pub output"""
view = self.client[:]
view.execute("from IPython.core.display import *")
@interactive
def publish():
[ display(i) for i in range(5) ]
ar = view.apply_async(publish)
ar.get(5)
Thomas Kluyver
Replace references to unicode and basestring
r13353 expected = [ {u'text/plain' : unicode_type(j)} for j in range(5) ]
MinRK
store whole content, instead of just display data in metadata.outputs
r6891 for outputs in ar.outputs:
mimes = [ out['data'] for out in outputs ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(mimes, expected)
MinRK
test new execute/output behaviors
r6835
def test_execute_raises(self):
"""exceptions in execute requests raise appropriately"""
view = self.client[-1]
ar = view.execute("1/0")
self.assertRaisesRemote(ZeroDivisionError, ar.get, 2)
MinRK
test %pylab on engines...
r6881
MinRK
test Composite/RemoteError tracebacks
r8228 def test_remoteerror_render_exception(self):
"""RemoteErrors get nice tracebacks"""
view = self.client[-1]
ar = view.execute("1/0")
ip = get_ipython()
ip.user_ns['ar'] = ar
with capture_output() as io:
ip.run_cell("ar.get(2)")
self.assertTrue('ZeroDivisionError' in io.stdout, io.stdout)
def test_compositeerror_render_exception(self):
"""CompositeErrors get nice tracebacks"""
view = self.client[:]
ar = view.execute("1/0")
ip = get_ipython()
ip.user_ns['ar'] = ar
MinRK
fix count when testing composite error output
r9730
MinRK
test Composite/RemoteError tracebacks
r8228 with capture_output() as io:
ip.run_cell("ar.get(2)")
MinRK
fix count when testing composite error output
r9730 count = min(error.CompositeError.tb_limit, len(view))
self.assertEqual(io.stdout.count('ZeroDivisionError'), count * 2, io.stdout)
self.assertEqual(io.stdout.count('by zero'), count, io.stdout)
self.assertEqual(io.stdout.count(':execute'), count, io.stdout)
MinRK
test Composite/RemoteError tracebacks
r8228
MinRK
truncate potentially long CompositeErrors...
r9411 def test_compositeerror_truncate(self):
"""Truncate CompositeErrors with many exceptions"""
view = self.client[:]
msg_ids = []
for i in range(10):
ar = view.execute("1/0")
msg_ids.extend(ar.msg_ids)
ar = self.client.get_result(msg_ids)
try:
ar.get()
MinRK
py3: assign e so it survives outside the scope
r9784 except error.CompositeError as _e:
e = _e
MinRK
truncate potentially long CompositeErrors...
r9411 else:
self.fail("Should have raised CompositeError")
lines = e.render_traceback()
with capture_output() as io:
e.print_traceback()
self.assertTrue("more exceptions" in lines[-1])
count = e.tb_limit
self.assertEqual(io.stdout.count('ZeroDivisionError'), 2 * count, io.stdout)
self.assertEqual(io.stdout.count('by zero'), count, io.stdout)
self.assertEqual(io.stdout.count(':execute'), count, io.stdout)
MinRK
test %pylab on engines...
r6881 @dec.skipif_not_matplotlib
MinRK
store whole content, instead of just display data in metadata.outputs
r6891 def test_magic_pylab(self):
MinRK
test %pylab on engines...
r6881 """%pylab works on engines"""
view = self.client[-1]
ar = view.execute("%pylab inline")
# at least check if this raised:
reply = ar.get(5)
# include imports, in case user config
ar = view.execute("plot(rand(100))", silent=False)
reply = ar.get(5)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(len(reply.outputs), 1)
MinRK
test %pylab on engines...
r6881 output = reply.outputs[0]
MinRK
store whole content, instead of just display data in metadata.outputs
r6891 self.assertTrue("data" in output)
data = output['data']
self.assertTrue("image/png" in data)
MinRK
test canning of func_defaults
r8042
def test_func_default_func(self):
"""interactively defined function as apply func default"""
def foo():
return 'foo'
def bar(f=foo):
return f()
MinRK
test %pylab on engines...
r6881
MinRK
test canning of func_defaults
r8042 view = self.client[-1]
ar = view.apply_async(bar)
r = ar.get(10)
MinRK
s/assertEquals/assertEqual (again)...
r8191 self.assertEqual(r, 'foo')
MinRK
basic tests for data_pub
r8106 def test_data_pub_single(self):
view = self.client[-1]
ar = view.execute('\n'.join([
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 'from IPython.kernel.zmq.datapub import publish_data',
MinRK
basic tests for data_pub
r8106 'for i in range(5):',
' publish_data(dict(i=i))'
]), block=False)
self.assertTrue(isinstance(ar.data, dict))
ar.get(5)
self.assertEqual(ar.data, dict(i=4))
def test_data_pub(self):
view = self.client[:]
ar = view.execute('\n'.join([
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 'from IPython.kernel.zmq.datapub import publish_data',
MinRK
basic tests for data_pub
r8106 'for i in range(5):',
' publish_data(dict(i=i))'
]), block=False)
self.assertTrue(all(isinstance(d, dict) for d in ar.data))
ar.get(5)
self.assertEqual(ar.data, [dict(i=4)] * len(ar))
MinRK
test canning for first-level containers as args/kwargs to apply and map
r8132
def test_can_list_arg(self):
"""args in lists are canned"""
view = self.client[-1]
view['a'] = 128
rA = pmod.Reference('a')
ar = view.apply_async(lambda x: x, [rA])
r = ar.get(5)
self.assertEqual(r, [128])
def test_can_dict_arg(self):
"""args in dicts are canned"""
view = self.client[-1]
view['a'] = 128
rA = pmod.Reference('a')
ar = view.apply_async(lambda x: x, dict(foo=rA))
r = ar.get(5)
self.assertEqual(r, dict(foo=128))
def test_can_list_kwarg(self):
"""kwargs in lists are canned"""
view = self.client[-1]
view['a'] = 128
rA = pmod.Reference('a')
ar = view.apply_async(lambda x=5: x, x=[rA])
r = ar.get(5)
self.assertEqual(r, [128])
def test_can_dict_kwarg(self):
"""kwargs in dicts are canned"""
view = self.client[-1]
view['a'] = 128
rA = pmod.Reference('a')
ar = view.apply_async(lambda x=5: x, dict(foo=rA))
r = ar.get(5)
self.assertEqual(r, dict(foo=128))
def test_map_ref(self):
"""view.map works with references"""
view = self.client[:]
ranks = sorted(self.client.ids)
view.scatter('rank', ranks, flatten=True)
rrank = pmod.Reference('rank')
amr = view.map_async(lambda x: x*2, [rrank] * len(view))
drank = amr.get(5)
self.assertEqual(drank, [ r*2 for r in ranks ])
Bradley M. Froehle
Parallel: Support get/set of nested objects in view (e.g. dv['a.b'])
r8271 def test_nested_getitem_setitem(self):
"""get and set with view['a.b']"""
view = self.client[-1]
view.execute('\n'.join([
'class A(object): pass',
'a = A()',
'a.b = 128',
]), block=True)
ra = pmod.Reference('a')
r = view.apply_sync(lambda x: x.b, ra)
self.assertEqual(r, 128)
self.assertEqual(view['a.b'], 128)
view['a.b'] = 0
MinRK
test new execute/output behaviors
r6835
Bradley M. Froehle
Parallel: Support get/set of nested objects in view (e.g. dv['a.b'])
r8271 r = view.apply_sync(lambda x: x.b, ra)
self.assertEqual(r, 0)
self.assertEqual(view['a.b'], 0)
MinRK
test namedtuple as return and apply arg
r9714
def test_return_namedtuple(self):
def namedtuplify(x, y):
from IPython.parallel.tests.test_view import point
return point(x, y)
view = self.client[-1]
p = view.apply_sync(namedtuplify, 1, 2)
self.assertEqual(p.x, 1)
self.assertEqual(p.y, 2)
def test_apply_namedtuple(self):
def echoxy(p):
return p.y, p.x
view = self.client[-1]
tup = view.apply_sync(echoxy, point(1, 2))
self.assertEqual(tup, (2,1))
MinRK
test view.sync_imports
r12880
def test_sync_imports(self):
view = self.client[-1]
with capture_output() as io:
with view.sync_imports():
import IPython
self.assertIn("IPython", io.stdout)
@interactive
def find_ipython():
return 'IPython' in globals()
assert view.apply_sync(find_ipython)
def test_sync_imports_quiet(self):
view = self.client[-1]
with capture_output() as io:
with view.sync_imports(quiet=True):
import IPython
self.assertEqual(io.stdout, '')
@interactive
def find_ipython():
return 'IPython' in globals()
assert view.apply_sync(find_ipython)
MinRK
test namedtuple as return and apply arg
r9714