##// END OF EJS Templates
move coding declaration above docstring in test_view...
MinRK -
Show More
@@ -1,125 +1,125 b''
1 # -*- coding: utf-8 -*-
1 """test LoadBalancedView objects
2 """test LoadBalancedView objects
2
3
3 Authors:
4 Authors:
4
5
5 * Min RK
6 * Min RK
6 """
7 """
7 # -*- coding: utf-8 -*-
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
21
22 import zmq
22 import zmq
23
23
24 from IPython import parallel as pmod
24 from IPython import parallel as pmod
25 from IPython.parallel import error
25 from IPython.parallel import error
26
26
27 from IPython.parallel.tests import add_engines
27 from IPython.parallel.tests import add_engines
28
28
29 from .clienttest import ClusterTestCase, crash, wait, skip_without
29 from .clienttest import ClusterTestCase, crash, wait, skip_without
30
30
31 def setup():
31 def setup():
32 add_engines(3)
32 add_engines(3)
33
33
34 class TestLoadBalancedView(ClusterTestCase):
34 class TestLoadBalancedView(ClusterTestCase):
35
35
36 def setUp(self):
36 def setUp(self):
37 ClusterTestCase.setUp(self)
37 ClusterTestCase.setUp(self)
38 self.view = self.client.load_balanced_view()
38 self.view = self.client.load_balanced_view()
39
39
40 def test_z_crash_task(self):
40 def test_z_crash_task(self):
41 """test graceful handling of engine death (balanced)"""
41 """test graceful handling of engine death (balanced)"""
42 # self.add_engines(1)
42 # self.add_engines(1)
43 ar = self.view.apply_async(crash)
43 ar = self.view.apply_async(crash)
44 self.assertRaisesRemote(error.EngineError, ar.get, 10)
44 self.assertRaisesRemote(error.EngineError, ar.get, 10)
45 eid = ar.engine_id
45 eid = ar.engine_id
46 tic = time.time()
46 tic = time.time()
47 while eid in self.client.ids and time.time()-tic < 5:
47 while eid in self.client.ids and time.time()-tic < 5:
48 time.sleep(.01)
48 time.sleep(.01)
49 self.client.spin()
49 self.client.spin()
50 self.assertFalse(eid in self.client.ids, "Engine should have died")
50 self.assertFalse(eid in self.client.ids, "Engine should have died")
51
51
52 def test_map(self):
52 def test_map(self):
53 def f(x):
53 def f(x):
54 return x**2
54 return x**2
55 data = range(16)
55 data = range(16)
56 r = self.view.map_sync(f, data)
56 r = self.view.map_sync(f, data)
57 self.assertEquals(r, map(f, data))
57 self.assertEquals(r, map(f, data))
58
58
59 def test_abort(self):
59 def test_abort(self):
60 view = self.view
60 view = self.view
61 ar = self.client[:].apply_async(time.sleep, .5)
61 ar = self.client[:].apply_async(time.sleep, .5)
62 ar2 = view.apply_async(lambda : 2)
62 ar2 = view.apply_async(lambda : 2)
63 ar3 = view.apply_async(lambda : 3)
63 ar3 = view.apply_async(lambda : 3)
64 view.abort(ar2)
64 view.abort(ar2)
65 view.abort(ar3.msg_ids)
65 view.abort(ar3.msg_ids)
66 self.assertRaises(error.TaskAborted, ar2.get)
66 self.assertRaises(error.TaskAborted, ar2.get)
67 self.assertRaises(error.TaskAborted, ar3.get)
67 self.assertRaises(error.TaskAborted, ar3.get)
68
68
69 def test_retries(self):
69 def test_retries(self):
70 add_engines(3)
70 add_engines(3)
71 view = self.view
71 view = self.view
72 view.timeout = 1 # prevent hang if this doesn't behave
72 view.timeout = 1 # prevent hang if this doesn't behave
73 def fail():
73 def fail():
74 assert False
74 assert False
75 for r in range(len(self.client)-1):
75 for r in range(len(self.client)-1):
76 with view.temp_flags(retries=r):
76 with view.temp_flags(retries=r):
77 self.assertRaisesRemote(AssertionError, view.apply_sync, fail)
77 self.assertRaisesRemote(AssertionError, view.apply_sync, fail)
78
78
79 with view.temp_flags(retries=len(self.client), timeout=0.25):
79 with view.temp_flags(retries=len(self.client), timeout=0.25):
80 self.assertRaisesRemote(error.TaskTimeout, view.apply_sync, fail)
80 self.assertRaisesRemote(error.TaskTimeout, view.apply_sync, fail)
81
81
82 def test_invalid_dependency(self):
82 def test_invalid_dependency(self):
83 view = self.view
83 view = self.view
84 with view.temp_flags(after='12345'):
84 with view.temp_flags(after='12345'):
85 self.assertRaisesRemote(error.InvalidDependency, view.apply_sync, lambda : 1)
85 self.assertRaisesRemote(error.InvalidDependency, view.apply_sync, lambda : 1)
86
86
87 def test_impossible_dependency(self):
87 def test_impossible_dependency(self):
88 if len(self.client) < 2:
88 if len(self.client) < 2:
89 add_engines(2)
89 add_engines(2)
90 view = self.client.load_balanced_view()
90 view = self.client.load_balanced_view()
91 ar1 = view.apply_async(lambda : 1)
91 ar1 = view.apply_async(lambda : 1)
92 ar1.get()
92 ar1.get()
93 e1 = ar1.engine_id
93 e1 = ar1.engine_id
94 e2 = e1
94 e2 = e1
95 while e2 == e1:
95 while e2 == e1:
96 ar2 = view.apply_async(lambda : 1)
96 ar2 = view.apply_async(lambda : 1)
97 ar2.get()
97 ar2.get()
98 e2 = ar2.engine_id
98 e2 = ar2.engine_id
99
99
100 with view.temp_flags(follow=[ar1, ar2]):
100 with view.temp_flags(follow=[ar1, ar2]):
101 self.assertRaisesRemote(error.ImpossibleDependency, view.apply_sync, lambda : 1)
101 self.assertRaisesRemote(error.ImpossibleDependency, view.apply_sync, lambda : 1)
102
102
103
103
104 def test_follow(self):
104 def test_follow(self):
105 ar = self.view.apply_async(lambda : 1)
105 ar = self.view.apply_async(lambda : 1)
106 ar.get()
106 ar.get()
107 ars = []
107 ars = []
108 first_id = ar.engine_id
108 first_id = ar.engine_id
109
109
110 self.view.follow = ar
110 self.view.follow = ar
111 for i in range(5):
111 for i in range(5):
112 ars.append(self.view.apply_async(lambda : 1))
112 ars.append(self.view.apply_async(lambda : 1))
113 self.view.wait(ars)
113 self.view.wait(ars)
114 for ar in ars:
114 for ar in ars:
115 self.assertEquals(ar.engine_id, first_id)
115 self.assertEquals(ar.engine_id, first_id)
116
116
117 def test_after(self):
117 def test_after(self):
118 view = self.view
118 view = self.view
119 ar = view.apply_async(time.sleep, 0.5)
119 ar = view.apply_async(time.sleep, 0.5)
120 with view.temp_flags(after=ar):
120 with view.temp_flags(after=ar):
121 ar2 = view.apply_async(lambda : 1)
121 ar2 = view.apply_async(lambda : 1)
122
122
123 ar.wait()
123 ar.wait()
124 ar2.wait()
124 ar2.wait()
125 self.assertTrue(ar2.started > ar.completed)
125 self.assertTrue(ar2.started > ar.completed)
@@ -1,446 +1,446 b''
1 # -*- coding: utf-8 -*-
1 """test View objects
2 """test View objects
2
3
3 Authors:
4 Authors:
4
5
5 * Min RK
6 * Min RK
6 """
7 """
7 # -*- coding: utf-8 -*-
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
25
26 from IPython import parallel as pmod
26 from IPython import parallel as pmod
27 from IPython.parallel import error
27 from IPython.parallel import error
28 from IPython.parallel import AsyncResult, AsyncHubResult, AsyncMapResult
28 from IPython.parallel import AsyncResult, AsyncHubResult, AsyncMapResult
29 from IPython.parallel import DirectView
29 from IPython.parallel import DirectView
30 from IPython.parallel.util import interactive
30 from IPython.parallel.util import interactive
31
31
32 from IPython.parallel.tests import add_engines
32 from IPython.parallel.tests import add_engines
33
33
34 from .clienttest import ClusterTestCase, crash, wait, skip_without
34 from .clienttest import ClusterTestCase, crash, wait, skip_without
35
35
36 def setup():
36 def setup():
37 add_engines(3)
37 add_engines(3)
38
38
39 class TestView(ClusterTestCase):
39 class TestView(ClusterTestCase):
40
40
41 def test_z_crash_mux(self):
41 def test_z_crash_mux(self):
42 """test graceful handling of engine death (direct)"""
42 """test graceful handling of engine death (direct)"""
43 # self.add_engines(1)
43 # self.add_engines(1)
44 eid = self.client.ids[-1]
44 eid = self.client.ids[-1]
45 ar = self.client[eid].apply_async(crash)
45 ar = self.client[eid].apply_async(crash)
46 self.assertRaisesRemote(error.EngineError, ar.get)
46 self.assertRaisesRemote(error.EngineError, ar.get)
47 eid = ar.engine_id
47 eid = ar.engine_id
48 tic = time.time()
48 tic = time.time()
49 while eid in self.client.ids and time.time()-tic < 5:
49 while eid in self.client.ids and time.time()-tic < 5:
50 time.sleep(.01)
50 time.sleep(.01)
51 self.client.spin()
51 self.client.spin()
52 self.assertFalse(eid in self.client.ids, "Engine should have died")
52 self.assertFalse(eid in self.client.ids, "Engine should have died")
53
53
54 def test_push_pull(self):
54 def test_push_pull(self):
55 """test pushing and pulling"""
55 """test pushing and pulling"""
56 data = dict(a=10, b=1.05, c=range(10), d={'e':(1,2),'f':'hi'})
56 data = dict(a=10, b=1.05, c=range(10), d={'e':(1,2),'f':'hi'})
57 t = self.client.ids[-1]
57 t = self.client.ids[-1]
58 v = self.client[t]
58 v = self.client[t]
59 push = v.push
59 push = v.push
60 pull = v.pull
60 pull = v.pull
61 v.block=True
61 v.block=True
62 nengines = len(self.client)
62 nengines = len(self.client)
63 push({'data':data})
63 push({'data':data})
64 d = pull('data')
64 d = pull('data')
65 self.assertEquals(d, data)
65 self.assertEquals(d, data)
66 self.client[:].push({'data':data})
66 self.client[:].push({'data':data})
67 d = self.client[:].pull('data', block=True)
67 d = self.client[:].pull('data', block=True)
68 self.assertEquals(d, nengines*[data])
68 self.assertEquals(d, nengines*[data])
69 ar = push({'data':data}, block=False)
69 ar = push({'data':data}, block=False)
70 self.assertTrue(isinstance(ar, AsyncResult))
70 self.assertTrue(isinstance(ar, AsyncResult))
71 r = ar.get()
71 r = ar.get()
72 ar = self.client[:].pull('data', block=False)
72 ar = self.client[:].pull('data', block=False)
73 self.assertTrue(isinstance(ar, AsyncResult))
73 self.assertTrue(isinstance(ar, AsyncResult))
74 r = ar.get()
74 r = ar.get()
75 self.assertEquals(r, nengines*[data])
75 self.assertEquals(r, nengines*[data])
76 self.client[:].push(dict(a=10,b=20))
76 self.client[:].push(dict(a=10,b=20))
77 r = self.client[:].pull(('a','b'), block=True)
77 r = self.client[:].pull(('a','b'), block=True)
78 self.assertEquals(r, nengines*[[10,20]])
78 self.assertEquals(r, nengines*[[10,20]])
79
79
80 def test_push_pull_function(self):
80 def test_push_pull_function(self):
81 "test pushing and pulling functions"
81 "test pushing and pulling functions"
82 def testf(x):
82 def testf(x):
83 return 2.0*x
83 return 2.0*x
84
84
85 t = self.client.ids[-1]
85 t = self.client.ids[-1]
86 v = self.client[t]
86 v = self.client[t]
87 v.block=True
87 v.block=True
88 push = v.push
88 push = v.push
89 pull = v.pull
89 pull = v.pull
90 execute = v.execute
90 execute = v.execute
91 push({'testf':testf})
91 push({'testf':testf})
92 r = pull('testf')
92 r = pull('testf')
93 self.assertEqual(r(1.0), testf(1.0))
93 self.assertEqual(r(1.0), testf(1.0))
94 execute('r = testf(10)')
94 execute('r = testf(10)')
95 r = pull('r')
95 r = pull('r')
96 self.assertEquals(r, testf(10))
96 self.assertEquals(r, testf(10))
97 ar = self.client[:].push({'testf':testf}, block=False)
97 ar = self.client[:].push({'testf':testf}, block=False)
98 ar.get()
98 ar.get()
99 ar = self.client[:].pull('testf', block=False)
99 ar = self.client[:].pull('testf', block=False)
100 rlist = ar.get()
100 rlist = ar.get()
101 for r in rlist:
101 for r in rlist:
102 self.assertEqual(r(1.0), testf(1.0))
102 self.assertEqual(r(1.0), testf(1.0))
103 execute("def g(x): return x*x")
103 execute("def g(x): return x*x")
104 r = pull(('testf','g'))
104 r = pull(('testf','g'))
105 self.assertEquals((r[0](10),r[1](10)), (testf(10), 100))
105 self.assertEquals((r[0](10),r[1](10)), (testf(10), 100))
106
106
107 def test_push_function_globals(self):
107 def test_push_function_globals(self):
108 """test that pushed functions have access to globals"""
108 """test that pushed functions have access to globals"""
109 @interactive
109 @interactive
110 def geta():
110 def geta():
111 return a
111 return a
112 # self.add_engines(1)
112 # self.add_engines(1)
113 v = self.client[-1]
113 v = self.client[-1]
114 v.block=True
114 v.block=True
115 v['f'] = geta
115 v['f'] = geta
116 self.assertRaisesRemote(NameError, v.execute, 'b=f()')
116 self.assertRaisesRemote(NameError, v.execute, 'b=f()')
117 v.execute('a=5')
117 v.execute('a=5')
118 v.execute('b=f()')
118 v.execute('b=f()')
119 self.assertEquals(v['b'], 5)
119 self.assertEquals(v['b'], 5)
120
120
121 def test_push_function_defaults(self):
121 def test_push_function_defaults(self):
122 """test that pushed functions preserve default args"""
122 """test that pushed functions preserve default args"""
123 def echo(a=10):
123 def echo(a=10):
124 return a
124 return a
125 v = self.client[-1]
125 v = self.client[-1]
126 v.block=True
126 v.block=True
127 v['f'] = echo
127 v['f'] = echo
128 v.execute('b=f()')
128 v.execute('b=f()')
129 self.assertEquals(v['b'], 10)
129 self.assertEquals(v['b'], 10)
130
130
131 def test_get_result(self):
131 def test_get_result(self):
132 """test getting results from the Hub."""
132 """test getting results from the Hub."""
133 c = pmod.Client(profile='iptest')
133 c = pmod.Client(profile='iptest')
134 # self.add_engines(1)
134 # self.add_engines(1)
135 t = c.ids[-1]
135 t = c.ids[-1]
136 v = c[t]
136 v = c[t]
137 v2 = self.client[t]
137 v2 = self.client[t]
138 ar = v.apply_async(wait, 1)
138 ar = v.apply_async(wait, 1)
139 # give the monitor time to notice the message
139 # give the monitor time to notice the message
140 time.sleep(.25)
140 time.sleep(.25)
141 ahr = v2.get_result(ar.msg_ids)
141 ahr = v2.get_result(ar.msg_ids)
142 self.assertTrue(isinstance(ahr, AsyncHubResult))
142 self.assertTrue(isinstance(ahr, AsyncHubResult))
143 self.assertEquals(ahr.get(), ar.get())
143 self.assertEquals(ahr.get(), ar.get())
144 ar2 = v2.get_result(ar.msg_ids)
144 ar2 = v2.get_result(ar.msg_ids)
145 self.assertFalse(isinstance(ar2, AsyncHubResult))
145 self.assertFalse(isinstance(ar2, AsyncHubResult))
146 c.spin()
146 c.spin()
147 c.close()
147 c.close()
148
148
149 def test_run_newline(self):
149 def test_run_newline(self):
150 """test that run appends newline to files"""
150 """test that run appends newline to files"""
151 tmpfile = mktemp()
151 tmpfile = mktemp()
152 with open(tmpfile, 'w') as f:
152 with open(tmpfile, 'w') as f:
153 f.write("""def g():
153 f.write("""def g():
154 return 5
154 return 5
155 """)
155 """)
156 v = self.client[-1]
156 v = self.client[-1]
157 v.run(tmpfile, block=True)
157 v.run(tmpfile, block=True)
158 self.assertEquals(v.apply_sync(lambda f: f(), pmod.Reference('g')), 5)
158 self.assertEquals(v.apply_sync(lambda f: f(), pmod.Reference('g')), 5)
159
159
160 def test_apply_tracked(self):
160 def test_apply_tracked(self):
161 """test tracking for apply"""
161 """test tracking for apply"""
162 # self.add_engines(1)
162 # self.add_engines(1)
163 t = self.client.ids[-1]
163 t = self.client.ids[-1]
164 v = self.client[t]
164 v = self.client[t]
165 v.block=False
165 v.block=False
166 def echo(n=1024*1024, **kwargs):
166 def echo(n=1024*1024, **kwargs):
167 with v.temp_flags(**kwargs):
167 with v.temp_flags(**kwargs):
168 return v.apply(lambda x: x, 'x'*n)
168 return v.apply(lambda x: x, 'x'*n)
169 ar = echo(1, track=False)
169 ar = echo(1, track=False)
170 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
170 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
171 self.assertTrue(ar.sent)
171 self.assertTrue(ar.sent)
172 ar = echo(track=True)
172 ar = echo(track=True)
173 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
173 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
174 self.assertEquals(ar.sent, ar._tracker.done)
174 self.assertEquals(ar.sent, ar._tracker.done)
175 ar._tracker.wait()
175 ar._tracker.wait()
176 self.assertTrue(ar.sent)
176 self.assertTrue(ar.sent)
177
177
178 def test_push_tracked(self):
178 def test_push_tracked(self):
179 t = self.client.ids[-1]
179 t = self.client.ids[-1]
180 ns = dict(x='x'*1024*1024)
180 ns = dict(x='x'*1024*1024)
181 v = self.client[t]
181 v = self.client[t]
182 ar = v.push(ns, block=False, track=False)
182 ar = v.push(ns, block=False, track=False)
183 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
183 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
184 self.assertTrue(ar.sent)
184 self.assertTrue(ar.sent)
185
185
186 ar = v.push(ns, block=False, track=True)
186 ar = v.push(ns, block=False, track=True)
187 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
187 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
188 self.assertEquals(ar.sent, ar._tracker.done)
188 self.assertEquals(ar.sent, ar._tracker.done)
189 ar._tracker.wait()
189 ar._tracker.wait()
190 self.assertTrue(ar.sent)
190 self.assertTrue(ar.sent)
191 ar.get()
191 ar.get()
192
192
193 def test_scatter_tracked(self):
193 def test_scatter_tracked(self):
194 t = self.client.ids
194 t = self.client.ids
195 x='x'*1024*1024
195 x='x'*1024*1024
196 ar = self.client[t].scatter('x', x, block=False, track=False)
196 ar = self.client[t].scatter('x', x, block=False, track=False)
197 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
197 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
198 self.assertTrue(ar.sent)
198 self.assertTrue(ar.sent)
199
199
200 ar = self.client[t].scatter('x', x, block=False, track=True)
200 ar = self.client[t].scatter('x', x, block=False, track=True)
201 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
201 self.assertTrue(isinstance(ar._tracker, zmq.MessageTracker))
202 self.assertEquals(ar.sent, ar._tracker.done)
202 self.assertEquals(ar.sent, ar._tracker.done)
203 ar._tracker.wait()
203 ar._tracker.wait()
204 self.assertTrue(ar.sent)
204 self.assertTrue(ar.sent)
205 ar.get()
205 ar.get()
206
206
207 def test_remote_reference(self):
207 def test_remote_reference(self):
208 v = self.client[-1]
208 v = self.client[-1]
209 v['a'] = 123
209 v['a'] = 123
210 ra = pmod.Reference('a')
210 ra = pmod.Reference('a')
211 b = v.apply_sync(lambda x: x, ra)
211 b = v.apply_sync(lambda x: x, ra)
212 self.assertEquals(b, 123)
212 self.assertEquals(b, 123)
213
213
214
214
215 def test_scatter_gather(self):
215 def test_scatter_gather(self):
216 view = self.client[:]
216 view = self.client[:]
217 seq1 = range(16)
217 seq1 = range(16)
218 view.scatter('a', seq1)
218 view.scatter('a', seq1)
219 seq2 = view.gather('a', block=True)
219 seq2 = view.gather('a', block=True)
220 self.assertEquals(seq2, seq1)
220 self.assertEquals(seq2, seq1)
221 self.assertRaisesRemote(NameError, view.gather, 'asdf', block=True)
221 self.assertRaisesRemote(NameError, view.gather, 'asdf', block=True)
222
222
223 @skip_without('numpy')
223 @skip_without('numpy')
224 def test_scatter_gather_numpy(self):
224 def test_scatter_gather_numpy(self):
225 import numpy
225 import numpy
226 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
226 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
227 view = self.client[:]
227 view = self.client[:]
228 a = numpy.arange(64)
228 a = numpy.arange(64)
229 view.scatter('a', a)
229 view.scatter('a', a)
230 b = view.gather('a', block=True)
230 b = view.gather('a', block=True)
231 assert_array_equal(b, a)
231 assert_array_equal(b, a)
232
232
233 def test_map(self):
233 def test_map(self):
234 view = self.client[:]
234 view = self.client[:]
235 def f(x):
235 def f(x):
236 return x**2
236 return x**2
237 data = range(16)
237 data = range(16)
238 r = view.map_sync(f, data)
238 r = view.map_sync(f, data)
239 self.assertEquals(r, map(f, data))
239 self.assertEquals(r, map(f, data))
240
240
241 def test_scatterGatherNonblocking(self):
241 def test_scatterGatherNonblocking(self):
242 data = range(16)
242 data = range(16)
243 view = self.client[:]
243 view = self.client[:]
244 view.scatter('a', data, block=False)
244 view.scatter('a', data, block=False)
245 ar = view.gather('a', block=False)
245 ar = view.gather('a', block=False)
246 self.assertEquals(ar.get(), data)
246 self.assertEquals(ar.get(), data)
247
247
248 @skip_without('numpy')
248 @skip_without('numpy')
249 def test_scatter_gather_numpy_nonblocking(self):
249 def test_scatter_gather_numpy_nonblocking(self):
250 import numpy
250 import numpy
251 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
251 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
252 a = numpy.arange(64)
252 a = numpy.arange(64)
253 view = self.client[:]
253 view = self.client[:]
254 ar = view.scatter('a', a, block=False)
254 ar = view.scatter('a', a, block=False)
255 self.assertTrue(isinstance(ar, AsyncResult))
255 self.assertTrue(isinstance(ar, AsyncResult))
256 amr = view.gather('a', block=False)
256 amr = view.gather('a', block=False)
257 self.assertTrue(isinstance(amr, AsyncMapResult))
257 self.assertTrue(isinstance(amr, AsyncMapResult))
258 assert_array_equal(amr.get(), a)
258 assert_array_equal(amr.get(), a)
259
259
260 def test_execute(self):
260 def test_execute(self):
261 view = self.client[:]
261 view = self.client[:]
262 # self.client.debug=True
262 # self.client.debug=True
263 execute = view.execute
263 execute = view.execute
264 ar = execute('c=30', block=False)
264 ar = execute('c=30', block=False)
265 self.assertTrue(isinstance(ar, AsyncResult))
265 self.assertTrue(isinstance(ar, AsyncResult))
266 ar = execute('d=[0,1,2]', block=False)
266 ar = execute('d=[0,1,2]', block=False)
267 self.client.wait(ar, 1)
267 self.client.wait(ar, 1)
268 self.assertEquals(len(ar.get()), len(self.client))
268 self.assertEquals(len(ar.get()), len(self.client))
269 for c in view['c']:
269 for c in view['c']:
270 self.assertEquals(c, 30)
270 self.assertEquals(c, 30)
271
271
272 def test_abort(self):
272 def test_abort(self):
273 view = self.client[-1]
273 view = self.client[-1]
274 ar = view.execute('import time; time.sleep(0.25)', block=False)
274 ar = view.execute('import time; time.sleep(0.25)', block=False)
275 ar2 = view.apply_async(lambda : 2)
275 ar2 = view.apply_async(lambda : 2)
276 ar3 = view.apply_async(lambda : 3)
276 ar3 = view.apply_async(lambda : 3)
277 view.abort(ar2)
277 view.abort(ar2)
278 view.abort(ar3.msg_ids)
278 view.abort(ar3.msg_ids)
279 self.assertRaises(error.TaskAborted, ar2.get)
279 self.assertRaises(error.TaskAborted, ar2.get)
280 self.assertRaises(error.TaskAborted, ar3.get)
280 self.assertRaises(error.TaskAborted, ar3.get)
281
281
282 def test_temp_flags(self):
282 def test_temp_flags(self):
283 view = self.client[-1]
283 view = self.client[-1]
284 view.block=True
284 view.block=True
285 with view.temp_flags(block=False):
285 with view.temp_flags(block=False):
286 self.assertFalse(view.block)
286 self.assertFalse(view.block)
287 self.assertTrue(view.block)
287 self.assertTrue(view.block)
288
288
289 def test_importer(self):
289 def test_importer(self):
290 view = self.client[-1]
290 view = self.client[-1]
291 view.clear(block=True)
291 view.clear(block=True)
292 with view.importer:
292 with view.importer:
293 import re
293 import re
294
294
295 @interactive
295 @interactive
296 def findall(pat, s):
296 def findall(pat, s):
297 # this globals() step isn't necessary in real code
297 # this globals() step isn't necessary in real code
298 # only to prevent a closure in the test
298 # only to prevent a closure in the test
299 re = globals()['re']
299 re = globals()['re']
300 return re.findall(pat, s)
300 return re.findall(pat, s)
301
301
302 self.assertEquals(view.apply_sync(findall, '\w+', 'hello world'), 'hello world'.split())
302 self.assertEquals(view.apply_sync(findall, '\w+', 'hello world'), 'hello world'.split())
303
303
304 # parallel magic tests
304 # parallel magic tests
305
305
306 def test_magic_px_blocking(self):
306 def test_magic_px_blocking(self):
307 ip = get_ipython()
307 ip = get_ipython()
308 v = self.client[-1]
308 v = self.client[-1]
309 v.activate()
309 v.activate()
310 v.block=True
310 v.block=True
311
311
312 ip.magic_px('a=5')
312 ip.magic_px('a=5')
313 self.assertEquals(v['a'], 5)
313 self.assertEquals(v['a'], 5)
314 ip.magic_px('a=10')
314 ip.magic_px('a=10')
315 self.assertEquals(v['a'], 10)
315 self.assertEquals(v['a'], 10)
316 sio = StringIO()
316 sio = StringIO()
317 savestdout = sys.stdout
317 savestdout = sys.stdout
318 sys.stdout = sio
318 sys.stdout = sio
319 ip.magic_px('print a')
319 ip.magic_px('print a')
320 sys.stdout = savestdout
320 sys.stdout = savestdout
321 sio.read()
321 sio.read()
322 self.assertTrue('[stdout:%i]'%v.targets in sio.buf)
322 self.assertTrue('[stdout:%i]'%v.targets in sio.buf)
323 self.assertTrue(sio.buf.rstrip().endswith('10'))
323 self.assertTrue(sio.buf.rstrip().endswith('10'))
324 self.assertRaisesRemote(ZeroDivisionError, ip.magic_px, '1/0')
324 self.assertRaisesRemote(ZeroDivisionError, ip.magic_px, '1/0')
325
325
326 def test_magic_px_nonblocking(self):
326 def test_magic_px_nonblocking(self):
327 ip = get_ipython()
327 ip = get_ipython()
328 v = self.client[-1]
328 v = self.client[-1]
329 v.activate()
329 v.activate()
330 v.block=False
330 v.block=False
331
331
332 ip.magic_px('a=5')
332 ip.magic_px('a=5')
333 self.assertEquals(v['a'], 5)
333 self.assertEquals(v['a'], 5)
334 ip.magic_px('a=10')
334 ip.magic_px('a=10')
335 self.assertEquals(v['a'], 10)
335 self.assertEquals(v['a'], 10)
336 sio = StringIO()
336 sio = StringIO()
337 savestdout = sys.stdout
337 savestdout = sys.stdout
338 sys.stdout = sio
338 sys.stdout = sio
339 ip.magic_px('print a')
339 ip.magic_px('print a')
340 sys.stdout = savestdout
340 sys.stdout = savestdout
341 sio.read()
341 sio.read()
342 self.assertFalse('[stdout:%i]'%v.targets in sio.buf)
342 self.assertFalse('[stdout:%i]'%v.targets in sio.buf)
343 ip.magic_px('1/0')
343 ip.magic_px('1/0')
344 ar = v.get_result(-1)
344 ar = v.get_result(-1)
345 self.assertRaisesRemote(ZeroDivisionError, ar.get)
345 self.assertRaisesRemote(ZeroDivisionError, ar.get)
346
346
347 def test_magic_autopx_blocking(self):
347 def test_magic_autopx_blocking(self):
348 ip = get_ipython()
348 ip = get_ipython()
349 v = self.client[-1]
349 v = self.client[-1]
350 v.activate()
350 v.activate()
351 v.block=True
351 v.block=True
352
352
353 sio = StringIO()
353 sio = StringIO()
354 savestdout = sys.stdout
354 savestdout = sys.stdout
355 sys.stdout = sio
355 sys.stdout = sio
356 ip.magic_autopx()
356 ip.magic_autopx()
357 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
357 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
358 ip.run_cell('print b')
358 ip.run_cell('print b')
359 ip.run_cell("b/c")
359 ip.run_cell("b/c")
360 ip.run_code(compile('b*=2', '', 'single'))
360 ip.run_code(compile('b*=2', '', 'single'))
361 ip.magic_autopx()
361 ip.magic_autopx()
362 sys.stdout = savestdout
362 sys.stdout = savestdout
363 sio.read()
363 sio.read()
364 output = sio.buf.strip()
364 output = sio.buf.strip()
365 self.assertTrue(output.startswith('%autopx enabled'))
365 self.assertTrue(output.startswith('%autopx enabled'))
366 self.assertTrue(output.endswith('%autopx disabled'))
366 self.assertTrue(output.endswith('%autopx disabled'))
367 self.assertTrue('RemoteError: ZeroDivisionError' in output)
367 self.assertTrue('RemoteError: ZeroDivisionError' in output)
368 ar = v.get_result(-2)
368 ar = v.get_result(-2)
369 self.assertEquals(v['a'], 5)
369 self.assertEquals(v['a'], 5)
370 self.assertEquals(v['b'], 20)
370 self.assertEquals(v['b'], 20)
371 self.assertRaisesRemote(ZeroDivisionError, ar.get)
371 self.assertRaisesRemote(ZeroDivisionError, ar.get)
372
372
373 def test_magic_autopx_nonblocking(self):
373 def test_magic_autopx_nonblocking(self):
374 ip = get_ipython()
374 ip = get_ipython()
375 v = self.client[-1]
375 v = self.client[-1]
376 v.activate()
376 v.activate()
377 v.block=False
377 v.block=False
378
378
379 sio = StringIO()
379 sio = StringIO()
380 savestdout = sys.stdout
380 savestdout = sys.stdout
381 sys.stdout = sio
381 sys.stdout = sio
382 ip.magic_autopx()
382 ip.magic_autopx()
383 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
383 ip.run_cell('\n'.join(('a=5','b=10','c=0')))
384 ip.run_cell('print b')
384 ip.run_cell('print b')
385 ip.run_cell("b/c")
385 ip.run_cell("b/c")
386 ip.run_code(compile('b*=2', '', 'single'))
386 ip.run_code(compile('b*=2', '', 'single'))
387 ip.magic_autopx()
387 ip.magic_autopx()
388 sys.stdout = savestdout
388 sys.stdout = savestdout
389 sio.read()
389 sio.read()
390 output = sio.buf.strip()
390 output = sio.buf.strip()
391 self.assertTrue(output.startswith('%autopx enabled'))
391 self.assertTrue(output.startswith('%autopx enabled'))
392 self.assertTrue(output.endswith('%autopx disabled'))
392 self.assertTrue(output.endswith('%autopx disabled'))
393 self.assertFalse('ZeroDivisionError' in output)
393 self.assertFalse('ZeroDivisionError' in output)
394 ar = v.get_result(-2)
394 ar = v.get_result(-2)
395 self.assertEquals(v['a'], 5)
395 self.assertEquals(v['a'], 5)
396 self.assertEquals(v['b'], 20)
396 self.assertEquals(v['b'], 20)
397 self.assertRaisesRemote(ZeroDivisionError, ar.get)
397 self.assertRaisesRemote(ZeroDivisionError, ar.get)
398
398
399 def test_magic_result(self):
399 def test_magic_result(self):
400 ip = get_ipython()
400 ip = get_ipython()
401 v = self.client[-1]
401 v = self.client[-1]
402 v.activate()
402 v.activate()
403 v['a'] = 111
403 v['a'] = 111
404 ra = v['a']
404 ra = v['a']
405
405
406 ar = ip.magic_result()
406 ar = ip.magic_result()
407 self.assertEquals(ar.msg_ids, [v.history[-1]])
407 self.assertEquals(ar.msg_ids, [v.history[-1]])
408 self.assertEquals(ar.get(), 111)
408 self.assertEquals(ar.get(), 111)
409 ar = ip.magic_result('-2')
409 ar = ip.magic_result('-2')
410 self.assertEquals(ar.msg_ids, [v.history[-2]])
410 self.assertEquals(ar.msg_ids, [v.history[-2]])
411
411
412 def test_unicode_execute(self):
412 def test_unicode_execute(self):
413 """test executing unicode strings"""
413 """test executing unicode strings"""
414 v = self.client[-1]
414 v = self.client[-1]
415 v.block=True
415 v.block=True
416 code=u"a=u'é'"
416 code=u"a=u'é'"
417 v.execute(code)
417 v.execute(code)
418 self.assertEquals(v['a'], u'é')
418 self.assertEquals(v['a'], u'é')
419
419
420 def test_unicode_apply_result(self):
420 def test_unicode_apply_result(self):
421 """test unicode apply results"""
421 """test unicode apply results"""
422 v = self.client[-1]
422 v = self.client[-1]
423 r = v.apply_sync(lambda : u'é')
423 r = v.apply_sync(lambda : u'é')
424 self.assertEquals(r, u'é')
424 self.assertEquals(r, u'é')
425
425
426 def test_unicode_apply_arg(self):
426 def test_unicode_apply_arg(self):
427 """test passing unicode arguments to apply"""
427 """test passing unicode arguments to apply"""
428 v = self.client[-1]
428 v = self.client[-1]
429
429
430 @interactive
430 @interactive
431 def check_unicode(a, check):
431 def check_unicode(a, check):
432 assert isinstance(a, unicode), "%r is not unicode"%a
432 assert isinstance(a, unicode), "%r is not unicode"%a
433 assert isinstance(check, bytes), "%r is not bytes"%check
433 assert isinstance(check, bytes), "%r is not bytes"%check
434 assert a.encode('utf8') == check, "%s != %s"%(a,check)
434 assert a.encode('utf8') == check, "%s != %s"%(a,check)
435
435
436 for s in [ u'é', u'ßø®∫','asdf'.decode() ]:
436 for s in [ u'é', u'ßø®∫','asdf'.decode() ]:
437 try:
437 try:
438 v.apply_sync(check_unicode, s, s.encode('utf8'))
438 v.apply_sync(check_unicode, s, s.encode('utf8'))
439 except error.RemoteError as e:
439 except error.RemoteError as e:
440 if e.ename == 'AssertionError':
440 if e.ename == 'AssertionError':
441 self.fail(e.evalue)
441 self.fail(e.evalue)
442 else:
442 else:
443 raise e
443 raise e
444
444
445
445
446
446
General Comments 0
You need to be logged in to leave comments. Login now