Show More
@@ -1,205 +1,264 b'' | |||||
1 | """Tests for asyncresult.py |
|
1 | """Tests for asyncresult.py | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Min RK |
|
5 | * Min RK | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #------------------------------------------------------------------------------- |
|
8 | #------------------------------------------------------------------------------- | |
9 | # Copyright (C) 2011 The IPython Development Team |
|
9 | # Copyright (C) 2011 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #------------------------------------------------------------------------------- |
|
13 | #------------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #------------------------------------------------------------------------------- |
|
15 | #------------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #------------------------------------------------------------------------------- |
|
17 | #------------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import time |
|
19 | import time | |
20 |
|
20 | |||
21 | from IPython.parallel.error import TimeoutError |
|
21 | from IPython.parallel.error import TimeoutError | |
22 |
|
22 | |||
23 | from IPython.parallel import error, Client |
|
23 | from IPython.parallel import error, Client | |
24 | from IPython.parallel.tests import add_engines |
|
24 | from IPython.parallel.tests import add_engines | |
25 | from .clienttest import ClusterTestCase |
|
25 | from .clienttest import ClusterTestCase, capture_output | |
26 |
|
26 | |||
27 | def setup(): |
|
27 | def setup(): | |
28 | add_engines(2, total=True) |
|
28 | add_engines(2, total=True) | |
29 |
|
29 | |||
30 | def wait(n): |
|
30 | def wait(n): | |
31 | import time |
|
31 | import time | |
32 | time.sleep(n) |
|
32 | time.sleep(n) | |
33 | return n |
|
33 | return n | |
34 |
|
34 | |||
35 | class AsyncResultTest(ClusterTestCase): |
|
35 | class AsyncResultTest(ClusterTestCase): | |
36 |
|
36 | |||
37 | def test_single_result_view(self): |
|
37 | def test_single_result_view(self): | |
38 | """various one-target views get the right value for single_result""" |
|
38 | """various one-target views get the right value for single_result""" | |
39 | eid = self.client.ids[-1] |
|
39 | eid = self.client.ids[-1] | |
40 | ar = self.client[eid].apply_async(lambda : 42) |
|
40 | ar = self.client[eid].apply_async(lambda : 42) | |
41 | self.assertEquals(ar.get(), 42) |
|
41 | self.assertEquals(ar.get(), 42) | |
42 | ar = self.client[[eid]].apply_async(lambda : 42) |
|
42 | ar = self.client[[eid]].apply_async(lambda : 42) | |
43 | self.assertEquals(ar.get(), [42]) |
|
43 | self.assertEquals(ar.get(), [42]) | |
44 | ar = self.client[-1:].apply_async(lambda : 42) |
|
44 | ar = self.client[-1:].apply_async(lambda : 42) | |
45 | self.assertEquals(ar.get(), [42]) |
|
45 | self.assertEquals(ar.get(), [42]) | |
46 |
|
46 | |||
47 | def test_get_after_done(self): |
|
47 | def test_get_after_done(self): | |
48 | ar = self.client[-1].apply_async(lambda : 42) |
|
48 | ar = self.client[-1].apply_async(lambda : 42) | |
49 | ar.wait() |
|
49 | ar.wait() | |
50 | self.assertTrue(ar.ready()) |
|
50 | self.assertTrue(ar.ready()) | |
51 | self.assertEquals(ar.get(), 42) |
|
51 | self.assertEquals(ar.get(), 42) | |
52 | self.assertEquals(ar.get(), 42) |
|
52 | self.assertEquals(ar.get(), 42) | |
53 |
|
53 | |||
54 | def test_get_before_done(self): |
|
54 | def test_get_before_done(self): | |
55 | ar = self.client[-1].apply_async(wait, 0.1) |
|
55 | ar = self.client[-1].apply_async(wait, 0.1) | |
56 | self.assertRaises(TimeoutError, ar.get, 0) |
|
56 | self.assertRaises(TimeoutError, ar.get, 0) | |
57 | ar.wait(0) |
|
57 | ar.wait(0) | |
58 | self.assertFalse(ar.ready()) |
|
58 | self.assertFalse(ar.ready()) | |
59 | self.assertEquals(ar.get(), 0.1) |
|
59 | self.assertEquals(ar.get(), 0.1) | |
60 |
|
60 | |||
61 | def test_get_after_error(self): |
|
61 | def test_get_after_error(self): | |
62 | ar = self.client[-1].apply_async(lambda : 1/0) |
|
62 | ar = self.client[-1].apply_async(lambda : 1/0) | |
63 | ar.wait(10) |
|
63 | ar.wait(10) | |
64 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
64 | self.assertRaisesRemote(ZeroDivisionError, ar.get) | |
65 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
65 | self.assertRaisesRemote(ZeroDivisionError, ar.get) | |
66 | self.assertRaisesRemote(ZeroDivisionError, ar.get_dict) |
|
66 | self.assertRaisesRemote(ZeroDivisionError, ar.get_dict) | |
67 |
|
67 | |||
68 | def test_get_dict(self): |
|
68 | def test_get_dict(self): | |
69 | n = len(self.client) |
|
69 | n = len(self.client) | |
70 | ar = self.client[:].apply_async(lambda : 5) |
|
70 | ar = self.client[:].apply_async(lambda : 5) | |
71 | self.assertEquals(ar.get(), [5]*n) |
|
71 | self.assertEquals(ar.get(), [5]*n) | |
72 | d = ar.get_dict() |
|
72 | d = ar.get_dict() | |
73 | self.assertEquals(sorted(d.keys()), sorted(self.client.ids)) |
|
73 | self.assertEquals(sorted(d.keys()), sorted(self.client.ids)) | |
74 | for eid,r in d.iteritems(): |
|
74 | for eid,r in d.iteritems(): | |
75 | self.assertEquals(r, 5) |
|
75 | self.assertEquals(r, 5) | |
76 |
|
76 | |||
77 | def test_list_amr(self): |
|
77 | def test_list_amr(self): | |
78 | ar = self.client.load_balanced_view().map_async(wait, [0.1]*5) |
|
78 | ar = self.client.load_balanced_view().map_async(wait, [0.1]*5) | |
79 | rlist = list(ar) |
|
79 | rlist = list(ar) | |
80 |
|
80 | |||
81 | def test_getattr(self): |
|
81 | def test_getattr(self): | |
82 | ar = self.client[:].apply_async(wait, 0.5) |
|
82 | ar = self.client[:].apply_async(wait, 0.5) | |
83 | self.assertRaises(AttributeError, lambda : ar._foo) |
|
83 | self.assertRaises(AttributeError, lambda : ar._foo) | |
84 | self.assertRaises(AttributeError, lambda : ar.__length_hint__()) |
|
84 | self.assertRaises(AttributeError, lambda : ar.__length_hint__()) | |
85 | self.assertRaises(AttributeError, lambda : ar.foo) |
|
85 | self.assertRaises(AttributeError, lambda : ar.foo) | |
86 | self.assertRaises(AttributeError, lambda : ar.engine_id) |
|
86 | self.assertRaises(AttributeError, lambda : ar.engine_id) | |
87 | self.assertFalse(hasattr(ar, '__length_hint__')) |
|
87 | self.assertFalse(hasattr(ar, '__length_hint__')) | |
88 | self.assertFalse(hasattr(ar, 'foo')) |
|
88 | self.assertFalse(hasattr(ar, 'foo')) | |
89 | self.assertFalse(hasattr(ar, 'engine_id')) |
|
89 | self.assertFalse(hasattr(ar, 'engine_id')) | |
90 | ar.get(5) |
|
90 | ar.get(5) | |
91 | self.assertRaises(AttributeError, lambda : ar._foo) |
|
91 | self.assertRaises(AttributeError, lambda : ar._foo) | |
92 | self.assertRaises(AttributeError, lambda : ar.__length_hint__()) |
|
92 | self.assertRaises(AttributeError, lambda : ar.__length_hint__()) | |
93 | self.assertRaises(AttributeError, lambda : ar.foo) |
|
93 | self.assertRaises(AttributeError, lambda : ar.foo) | |
94 | self.assertTrue(isinstance(ar.engine_id, list)) |
|
94 | self.assertTrue(isinstance(ar.engine_id, list)) | |
95 | self.assertEquals(ar.engine_id, ar['engine_id']) |
|
95 | self.assertEquals(ar.engine_id, ar['engine_id']) | |
96 | self.assertFalse(hasattr(ar, '__length_hint__')) |
|
96 | self.assertFalse(hasattr(ar, '__length_hint__')) | |
97 | self.assertFalse(hasattr(ar, 'foo')) |
|
97 | self.assertFalse(hasattr(ar, 'foo')) | |
98 | self.assertTrue(hasattr(ar, 'engine_id')) |
|
98 | self.assertTrue(hasattr(ar, 'engine_id')) | |
99 |
|
99 | |||
100 | def test_getitem(self): |
|
100 | def test_getitem(self): | |
101 | ar = self.client[:].apply_async(wait, 0.5) |
|
101 | ar = self.client[:].apply_async(wait, 0.5) | |
102 | self.assertRaises(TimeoutError, lambda : ar['foo']) |
|
102 | self.assertRaises(TimeoutError, lambda : ar['foo']) | |
103 | self.assertRaises(TimeoutError, lambda : ar['engine_id']) |
|
103 | self.assertRaises(TimeoutError, lambda : ar['engine_id']) | |
104 | ar.get(5) |
|
104 | ar.get(5) | |
105 | self.assertRaises(KeyError, lambda : ar['foo']) |
|
105 | self.assertRaises(KeyError, lambda : ar['foo']) | |
106 | self.assertTrue(isinstance(ar['engine_id'], list)) |
|
106 | self.assertTrue(isinstance(ar['engine_id'], list)) | |
107 | self.assertEquals(ar.engine_id, ar['engine_id']) |
|
107 | self.assertEquals(ar.engine_id, ar['engine_id']) | |
108 |
|
108 | |||
109 | def test_single_result(self): |
|
109 | def test_single_result(self): | |
110 | ar = self.client[-1].apply_async(wait, 0.5) |
|
110 | ar = self.client[-1].apply_async(wait, 0.5) | |
111 | self.assertRaises(TimeoutError, lambda : ar['foo']) |
|
111 | self.assertRaises(TimeoutError, lambda : ar['foo']) | |
112 | self.assertRaises(TimeoutError, lambda : ar['engine_id']) |
|
112 | self.assertRaises(TimeoutError, lambda : ar['engine_id']) | |
113 | self.assertTrue(ar.get(5) == 0.5) |
|
113 | self.assertTrue(ar.get(5) == 0.5) | |
114 | self.assertTrue(isinstance(ar['engine_id'], int)) |
|
114 | self.assertTrue(isinstance(ar['engine_id'], int)) | |
115 | self.assertTrue(isinstance(ar.engine_id, int)) |
|
115 | self.assertTrue(isinstance(ar.engine_id, int)) | |
116 | self.assertEquals(ar.engine_id, ar['engine_id']) |
|
116 | self.assertEquals(ar.engine_id, ar['engine_id']) | |
117 |
|
117 | |||
118 | def test_abort(self): |
|
118 | def test_abort(self): | |
119 | e = self.client[-1] |
|
119 | e = self.client[-1] | |
120 | ar = e.execute('import time; time.sleep(1)', block=False) |
|
120 | ar = e.execute('import time; time.sleep(1)', block=False) | |
121 | ar2 = e.apply_async(lambda : 2) |
|
121 | ar2 = e.apply_async(lambda : 2) | |
122 | ar2.abort() |
|
122 | ar2.abort() | |
123 | self.assertRaises(error.TaskAborted, ar2.get) |
|
123 | self.assertRaises(error.TaskAborted, ar2.get) | |
124 | ar.get() |
|
124 | ar.get() | |
125 |
|
125 | |||
126 | def test_len(self): |
|
126 | def test_len(self): | |
127 | v = self.client.load_balanced_view() |
|
127 | v = self.client.load_balanced_view() | |
128 | ar = v.map_async(lambda x: x, range(10)) |
|
128 | ar = v.map_async(lambda x: x, range(10)) | |
129 | self.assertEquals(len(ar), 10) |
|
129 | self.assertEquals(len(ar), 10) | |
130 | ar = v.apply_async(lambda x: x, range(10)) |
|
130 | ar = v.apply_async(lambda x: x, range(10)) | |
131 | self.assertEquals(len(ar), 1) |
|
131 | self.assertEquals(len(ar), 1) | |
132 | ar = self.client[:].apply_async(lambda x: x, range(10)) |
|
132 | ar = self.client[:].apply_async(lambda x: x, range(10)) | |
133 | self.assertEquals(len(ar), len(self.client.ids)) |
|
133 | self.assertEquals(len(ar), len(self.client.ids)) | |
134 |
|
134 | |||
135 | def test_wall_time_single(self): |
|
135 | def test_wall_time_single(self): | |
136 | v = self.client.load_balanced_view() |
|
136 | v = self.client.load_balanced_view() | |
137 | ar = v.apply_async(time.sleep, 0.25) |
|
137 | ar = v.apply_async(time.sleep, 0.25) | |
138 | self.assertRaises(TimeoutError, getattr, ar, 'wall_time') |
|
138 | self.assertRaises(TimeoutError, getattr, ar, 'wall_time') | |
139 | ar.get(2) |
|
139 | ar.get(2) | |
140 | self.assertTrue(ar.wall_time < 1.) |
|
140 | self.assertTrue(ar.wall_time < 1.) | |
141 | self.assertTrue(ar.wall_time > 0.2) |
|
141 | self.assertTrue(ar.wall_time > 0.2) | |
142 |
|
142 | |||
143 | def test_wall_time_multi(self): |
|
143 | def test_wall_time_multi(self): | |
144 | self.minimum_engines(4) |
|
144 | self.minimum_engines(4) | |
145 | v = self.client[:] |
|
145 | v = self.client[:] | |
146 | ar = v.apply_async(time.sleep, 0.25) |
|
146 | ar = v.apply_async(time.sleep, 0.25) | |
147 | self.assertRaises(TimeoutError, getattr, ar, 'wall_time') |
|
147 | self.assertRaises(TimeoutError, getattr, ar, 'wall_time') | |
148 | ar.get(2) |
|
148 | ar.get(2) | |
149 | self.assertTrue(ar.wall_time < 1.) |
|
149 | self.assertTrue(ar.wall_time < 1.) | |
150 | self.assertTrue(ar.wall_time > 0.2) |
|
150 | self.assertTrue(ar.wall_time > 0.2) | |
151 |
|
151 | |||
152 | def test_serial_time_single(self): |
|
152 | def test_serial_time_single(self): | |
153 | v = self.client.load_balanced_view() |
|
153 | v = self.client.load_balanced_view() | |
154 | ar = v.apply_async(time.sleep, 0.25) |
|
154 | ar = v.apply_async(time.sleep, 0.25) | |
155 | self.assertRaises(TimeoutError, getattr, ar, 'serial_time') |
|
155 | self.assertRaises(TimeoutError, getattr, ar, 'serial_time') | |
156 | ar.get(2) |
|
156 | ar.get(2) | |
157 | self.assertTrue(ar.serial_time < 1.) |
|
157 | self.assertTrue(ar.serial_time < 1.) | |
158 | self.assertTrue(ar.serial_time > 0.2) |
|
158 | self.assertTrue(ar.serial_time > 0.2) | |
159 |
|
159 | |||
160 | def test_serial_time_multi(self): |
|
160 | def test_serial_time_multi(self): | |
161 | self.minimum_engines(4) |
|
161 | self.minimum_engines(4) | |
162 | v = self.client[:] |
|
162 | v = self.client[:] | |
163 | ar = v.apply_async(time.sleep, 0.25) |
|
163 | ar = v.apply_async(time.sleep, 0.25) | |
164 | self.assertRaises(TimeoutError, getattr, ar, 'serial_time') |
|
164 | self.assertRaises(TimeoutError, getattr, ar, 'serial_time') | |
165 | ar.get(2) |
|
165 | ar.get(2) | |
166 | self.assertTrue(ar.serial_time < 2.) |
|
166 | self.assertTrue(ar.serial_time < 2.) | |
167 | self.assertTrue(ar.serial_time > 0.8) |
|
167 | self.assertTrue(ar.serial_time > 0.8) | |
168 |
|
168 | |||
169 | def test_elapsed_single(self): |
|
169 | def test_elapsed_single(self): | |
170 | v = self.client.load_balanced_view() |
|
170 | v = self.client.load_balanced_view() | |
171 | ar = v.apply_async(time.sleep, 0.25) |
|
171 | ar = v.apply_async(time.sleep, 0.25) | |
172 | while not ar.ready(): |
|
172 | while not ar.ready(): | |
173 | time.sleep(0.01) |
|
173 | time.sleep(0.01) | |
174 | self.assertTrue(ar.elapsed < 1) |
|
174 | self.assertTrue(ar.elapsed < 1) | |
175 | self.assertTrue(ar.elapsed < 1) |
|
175 | self.assertTrue(ar.elapsed < 1) | |
176 | ar.get(2) |
|
176 | ar.get(2) | |
177 |
|
177 | |||
178 | def test_elapsed_multi(self): |
|
178 | def test_elapsed_multi(self): | |
179 | v = self.client[:] |
|
179 | v = self.client[:] | |
180 | ar = v.apply_async(time.sleep, 0.25) |
|
180 | ar = v.apply_async(time.sleep, 0.25) | |
181 | while not ar.ready(): |
|
181 | while not ar.ready(): | |
182 | time.sleep(0.01) |
|
182 | time.sleep(0.01) | |
183 | self.assertTrue(ar.elapsed < 1) |
|
183 | self.assertTrue(ar.elapsed < 1) | |
184 | self.assertTrue(ar.elapsed < 1) |
|
184 | self.assertTrue(ar.elapsed < 1) | |
185 | ar.get(2) |
|
185 | ar.get(2) | |
186 |
|
186 | |||
187 | def test_hubresult_timestamps(self): |
|
187 | def test_hubresult_timestamps(self): | |
188 | self.minimum_engines(4) |
|
188 | self.minimum_engines(4) | |
189 | v = self.client[:] |
|
189 | v = self.client[:] | |
190 | ar = v.apply_async(time.sleep, 0.25) |
|
190 | ar = v.apply_async(time.sleep, 0.25) | |
191 | ar.get(2) |
|
191 | ar.get(2) | |
192 | rc2 = Client(profile='iptest') |
|
192 | rc2 = Client(profile='iptest') | |
193 | # must have try/finally to close second Client, otherwise |
|
193 | # must have try/finally to close second Client, otherwise | |
194 | # will have dangling sockets causing problems |
|
194 | # will have dangling sockets causing problems | |
195 | try: |
|
195 | try: | |
196 | time.sleep(0.25) |
|
196 | time.sleep(0.25) | |
197 | hr = rc2.get_result(ar.msg_ids) |
|
197 | hr = rc2.get_result(ar.msg_ids) | |
198 | self.assertTrue(hr.elapsed > 0., "got bad elapsed: %s" % hr.elapsed) |
|
198 | self.assertTrue(hr.elapsed > 0., "got bad elapsed: %s" % hr.elapsed) | |
199 | hr.get(1) |
|
199 | hr.get(1) | |
200 | self.assertTrue(hr.wall_time < ar.wall_time + 0.2, "got bad wall_time: %s > %s" % (hr.wall_time, ar.wall_time)) |
|
200 | self.assertTrue(hr.wall_time < ar.wall_time + 0.2, "got bad wall_time: %s > %s" % (hr.wall_time, ar.wall_time)) | |
201 | self.assertEquals(hr.serial_time, ar.serial_time) |
|
201 | self.assertEquals(hr.serial_time, ar.serial_time) | |
202 | finally: |
|
202 | finally: | |
203 | rc2.close() |
|
203 | rc2.close() | |
204 |
|
204 | |||
|
205 | def test_display_empty_streams_single(self): | |||
|
206 | """empty stdout/err are not displayed (single result)""" | |||
|
207 | self.minimum_engines(1) | |||
|
208 | ||||
|
209 | v = self.client[-1] | |||
|
210 | ar = v.execute("print (5555)") | |||
|
211 | ar.get(5) | |||
|
212 | with capture_output() as io: | |||
|
213 | ar.display_outputs() | |||
|
214 | self.assertEquals(io.stderr, '') | |||
|
215 | self.assertTrue('5555' in io.stdout) | |||
|
216 | ||||
|
217 | ar = v.execute("a=5") | |||
|
218 | ar.get(5) | |||
|
219 | with capture_output() as io: | |||
|
220 | ar.display_outputs() | |||
|
221 | self.assertEquals(io.stderr, '') | |||
|
222 | self.assertEquals(io.stdout, '') | |||
|
223 | ||||
|
224 | def test_display_empty_streams_type(self): | |||
|
225 | """empty stdout/err are not displayed (groupby type)""" | |||
|
226 | self.minimum_engines(1) | |||
|
227 | ||||
|
228 | v = self.client[:] | |||
|
229 | ar = v.execute("print (5555)") | |||
|
230 | ar.get(5) | |||
|
231 | with capture_output() as io: | |||
|
232 | ar.display_outputs() | |||
|
233 | self.assertEquals(io.stderr, '') | |||
|
234 | self.assertEquals(io.stdout.count('5555'), len(v), io.stdout) | |||
|
235 | self.assertEquals(io.stdout.count('[stdout:'), len(v), io.stdout) | |||
|
236 | ||||
|
237 | ar = v.execute("a=5") | |||
|
238 | ar.get(5) | |||
|
239 | with capture_output() as io: | |||
|
240 | ar.display_outputs() | |||
|
241 | self.assertEquals(io.stderr, '') | |||
|
242 | self.assertEquals(io.stdout, '') | |||
|
243 | ||||
|
244 | def test_display_empty_streams_engine(self): | |||
|
245 | """empty stdout/err are not displayed (groupby engine)""" | |||
|
246 | self.minimum_engines(1) | |||
|
247 | ||||
|
248 | v = self.client[:] | |||
|
249 | ar = v.execute("print (5555)") | |||
|
250 | ar.get(5) | |||
|
251 | with capture_output() as io: | |||
|
252 | ar.display_outputs('engine') | |||
|
253 | self.assertEquals(io.stderr, '') | |||
|
254 | self.assertEquals(io.stdout.count('5555'), len(v), io.stdout) | |||
|
255 | self.assertEquals(io.stdout.count('[stdout:'), len(v), io.stdout) | |||
|
256 | ||||
|
257 | ar = v.execute("a=5") | |||
|
258 | ar.get(5) | |||
|
259 | with capture_output() as io: | |||
|
260 | ar.display_outputs('engine') | |||
|
261 | self.assertEquals(io.stderr, '') | |||
|
262 | self.assertEquals(io.stdout, '') | |||
|
263 | ||||
205 |
|
264 |
General Comments 0
You need to be logged in to leave comments.
Login now