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