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