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