##// END OF EJS Templates
Rename two tests that had identical names to distinguish them....
Fernando Perez -
Show More
@@ -1,372 +1,373 b''
1 1 # encoding: utf-8
2 2
3 3 """Test template for complete engine object"""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 #-------------------------------------------------------------------------------
8 8 # Copyright (C) 2008 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-------------------------------------------------------------------------------
13 13
14 14 #-------------------------------------------------------------------------------
15 15 # Imports
16 16 #-------------------------------------------------------------------------------
17 17
18 18 import cPickle as pickle
19 19
20 20 from twisted.internet import defer, reactor
21 21 from twisted.python import failure
22 22 from twisted.application import service
23 23 import zope.interface as zi
24 24
25 25 from IPython.kernel import newserialized
26 26 from IPython.kernel import error
27 27 from IPython.kernel.pickleutil import can, uncan
28 28 import IPython.kernel.engineservice as es
29 29 from IPython.kernel.core.interpreter import Interpreter
30 30 from IPython.testing.parametric import Parametric, parametric
31 31
32 32 #-------------------------------------------------------------------------------
33 33 # Tests
34 34 #-------------------------------------------------------------------------------
35 35
36 36
37 37 # A sequence of valid commands run through execute
38 38 validCommands = ['a=5',
39 39 'b=10',
40 40 'a=5; b=10; c=a+b',
41 41 'import math; 2.0*math.pi',
42 42 """def f():
43 43 result = 0.0
44 44 for i in range(10):
45 45 result += i
46 46 """,
47 47 'if 1<2: a=5',
48 48 """import time
49 49 time.sleep(0.1)""",
50 50 """from math import cos;
51 51 x = 1.0*cos(0.5)""", # Semicolons lead to Discard ast nodes that should be discarded
52 52 """from sets import Set
53 53 s = Set()
54 54 """, # Trailing whitespace should be allowed.
55 55 """import math
56 56 math.cos(1.0)""", # Test a method call with a discarded return value
57 57 """x=1.0234
58 58 a=5; b=10""", # Test an embedded semicolon
59 59 """x=1.0234
60 60 a=5; b=10;""" # Test both an embedded and trailing semicolon
61 61 ]
62 62
63 63 # A sequence of commands that raise various exceptions
64 64 invalidCommands = [('a=1/0',ZeroDivisionError),
65 65 ('print v',NameError),
66 66 ('l=[];l[0]',IndexError),
67 67 ("d={};d['a']",KeyError),
68 68 ("assert 1==0",AssertionError),
69 69 ("import abababsdbfsbaljasdlja",ImportError),
70 70 ("raise Exception()",Exception)]
71 71
72 72 def testf(x):
73 73 return 2.0*x
74 74
75 75 globala = 99
76 76
77 77 def testg(x):
78 78 return globala*x
79 79
80 80 class IEngineCoreTestCase(object):
81 81 """Test an IEngineCore implementer."""
82 82
83 83 def createShell(self):
84 84 return Interpreter()
85 85
86 86 def catchQueueCleared(self, f):
87 87 try:
88 88 f.raiseException()
89 89 except error.QueueCleared:
90 90 pass
91 91
92 92 def testIEngineCoreInterface(self):
93 93 """Does self.engine claim to implement IEngineCore?"""
94 94 self.assert_(es.IEngineCore.providedBy(self.engine))
95 95
96 96 def testIEngineCoreInterfaceMethods(self):
97 97 """Does self.engine have the methods and attributes in IEngineCore."""
98 98 for m in list(es.IEngineCore):
99 99 self.assert_(hasattr(self.engine, m))
100 100
101 101 def testIEngineCoreDeferreds(self):
102 102 d = self.engine.execute('a=5')
103 103 d.addCallback(lambda _: self.engine.pull('a'))
104 104 d.addCallback(lambda _: self.engine.get_result())
105 105 d.addCallback(lambda _: self.engine.keys())
106 106 d.addCallback(lambda _: self.engine.push(dict(a=10)))
107 107 return d
108 108
109 109 def runTestExecute(self, cmd):
110 110 self.shell = Interpreter()
111 111 actual = self.shell.execute(cmd)
112 112 def compare(computed):
113 113 actual['id'] = computed['id']
114 114 self.assertEquals(actual, computed)
115 115 d = self.engine.execute(cmd)
116 116 d.addCallback(compare)
117 117 return d
118 118
119 119 @parametric
120 120 def testExecute(cls):
121 121 return [(cls.runTestExecute, cmd) for cmd in validCommands]
122 122
123 123 def runTestExecuteFailures(self, cmd, exc):
124 124 def compare(f):
125 125 self.assertRaises(exc, f.raiseException)
126 126 d = self.engine.execute(cmd)
127 127 d.addErrback(compare)
128 128 return d
129 129
130 130 @parametric
131 def testExecuteFailures(cls):
132 return [(cls.runTestExecuteFailures, cmd, exc) for cmd, exc in invalidCommands]
131 def testExecuteFailuresEngineService(cls):
132 return [(cls.runTestExecuteFailures, cmd, exc)
133 for cmd, exc in invalidCommands]
133 134
134 135 def runTestPushPull(self, o):
135 136 d = self.engine.push(dict(a=o))
136 137 d.addCallback(lambda r: self.engine.pull('a'))
137 138 d.addCallback(lambda r: self.assertEquals(o,r))
138 139 return d
139 140
140 141 @parametric
141 142 def testPushPull(cls):
142 143 objs = [10,"hi there",1.2342354,{"p":(1,2)},None]
143 144 return [(cls.runTestPushPull, o) for o in objs]
144 145
145 146 def testPullNameError(self):
146 147 d = self.engine.push(dict(a=5))
147 148 d.addCallback(lambda _:self.engine.reset())
148 149 d.addCallback(lambda _: self.engine.pull("a"))
149 150 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
150 151 return d
151 152
152 153 def testPushPullFailures(self):
153 154 d = self.engine.pull('a')
154 155 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
155 156 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
156 157 d.addCallback(lambda _: self.engine.pull('l'))
157 158 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
158 159 d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x)))
159 160 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
160 161 return d
161 162
162 163 def testPushPullArray(self):
163 164 try:
164 165 import numpy
165 166 except:
166 167 return
167 168 a = numpy.random.random(1000)
168 169 d = self.engine.push(dict(a=a))
169 170 d.addCallback(lambda _: self.engine.pull('a'))
170 171 d.addCallback(lambda b: b==a)
171 172 d.addCallback(lambda c: c.all())
172 173 return self.assertDeferredEquals(d, True)
173 174
174 175 def testPushFunction(self):
175 176
176 177 d = self.engine.push_function(dict(f=testf))
177 178 d.addCallback(lambda _: self.engine.execute('result = f(10)'))
178 179 d.addCallback(lambda _: self.engine.pull('result'))
179 180 d.addCallback(lambda r: self.assertEquals(r, testf(10)))
180 181 return d
181 182
182 183 def testPullFunction(self):
183 184 d = self.engine.push_function(dict(f=testf, g=testg))
184 185 d.addCallback(lambda _: self.engine.pull_function(('f','g')))
185 186 d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
186 187 return d
187 188
188 189 def testPushFunctionGlobal(self):
189 190 """Make sure that pushed functions pick up the user's namespace for globals."""
190 191 d = self.engine.push(dict(globala=globala))
191 192 d.addCallback(lambda _: self.engine.push_function(dict(g=testg)))
192 193 d.addCallback(lambda _: self.engine.execute('result = g(10)'))
193 194 d.addCallback(lambda _: self.engine.pull('result'))
194 195 d.addCallback(lambda r: self.assertEquals(r, testg(10)))
195 196 return d
196 197
197 198 def testGetResultFailure(self):
198 199 d = self.engine.get_result(None)
199 200 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
200 201 d.addCallback(lambda _: self.engine.get_result(10))
201 202 d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
202 203 return d
203 204
204 205 def runTestGetResult(self, cmd):
205 206 self.shell = Interpreter()
206 207 actual = self.shell.execute(cmd)
207 208 def compare(computed):
208 209 actual['id'] = computed['id']
209 210 self.assertEquals(actual, computed)
210 211 d = self.engine.execute(cmd)
211 212 d.addCallback(lambda r: self.engine.get_result(r['number']))
212 213 d.addCallback(compare)
213 214 return d
214 215
215 216 @parametric
216 217 def testGetResult(cls):
217 218 return [(cls.runTestGetResult, cmd) for cmd in validCommands]
218 219
219 220 def testGetResultDefault(self):
220 221 cmd = 'a=5'
221 222 shell = self.createShell()
222 223 shellResult = shell.execute(cmd)
223 224 def popit(dikt, key):
224 225 dikt.pop(key)
225 226 return dikt
226 227 d = self.engine.execute(cmd)
227 228 d.addCallback(lambda _: self.engine.get_result())
228 229 d.addCallback(lambda r: self.assertEquals(shellResult, popit(r,'id')))
229 230 return d
230 231
231 232 def testKeys(self):
232 233 d = self.engine.keys()
233 234 d.addCallback(lambda s: isinstance(s, list))
234 235 d.addCallback(lambda r: self.assertEquals(r, True))
235 236 return d
236 237
237 238 Parametric(IEngineCoreTestCase)
238 239
239 240 class IEngineSerializedTestCase(object):
240 241 """Test an IEngineCore implementer."""
241 242
242 243 def testIEngineSerializedInterface(self):
243 244 """Does self.engine claim to implement IEngineCore?"""
244 245 self.assert_(es.IEngineSerialized.providedBy(self.engine))
245 246
246 247 def testIEngineSerializedInterfaceMethods(self):
247 248 """Does self.engine have the methods and attributes in IEngireCore."""
248 249 for m in list(es.IEngineSerialized):
249 250 self.assert_(hasattr(self.engine, m))
250 251
251 252 def testIEngineSerializedDeferreds(self):
252 253 dList = []
253 254 d = self.engine.push_serialized(dict(key=newserialized.serialize(12345)))
254 255 self.assert_(isinstance(d, defer.Deferred))
255 256 dList.append(d)
256 257 d = self.engine.pull_serialized('key')
257 258 self.assert_(isinstance(d, defer.Deferred))
258 259 dList.append(d)
259 260 D = defer.DeferredList(dList)
260 261 return D
261 262
262 263 def testPushPullSerialized(self):
263 264 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
264 265 d = defer.succeed(None)
265 266 for o in objs:
266 267 self.engine.push_serialized(dict(key=newserialized.serialize(o)))
267 268 value = self.engine.pull_serialized('key')
268 269 value.addCallback(lambda serial: newserialized.IUnSerialized(serial).getObject())
269 270 d = self.assertDeferredEquals(value,o,d)
270 271 return d
271 272
272 273 def testPullSerializedFailures(self):
273 274 d = self.engine.pull_serialized('a')
274 275 d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
275 276 d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
276 277 d.addCallback(lambda _: self.engine.pull_serialized('l'))
277 278 d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
278 279 return d
279 280
280 281 Parametric(IEngineSerializedTestCase)
281 282
282 283 class IEngineQueuedTestCase(object):
283 284 """Test an IEngineQueued implementer."""
284 285
285 286 def testIEngineQueuedInterface(self):
286 287 """Does self.engine claim to implement IEngineQueued?"""
287 288 self.assert_(es.IEngineQueued.providedBy(self.engine))
288 289
289 290 def testIEngineQueuedInterfaceMethods(self):
290 291 """Does self.engine have the methods and attributes in IEngireQueued."""
291 292 for m in list(es.IEngineQueued):
292 293 self.assert_(hasattr(self.engine, m))
293 294
294 295 def testIEngineQueuedDeferreds(self):
295 296 dList = []
296 297 d = self.engine.clear_queue()
297 298 self.assert_(isinstance(d, defer.Deferred))
298 299 dList.append(d)
299 300 d = self.engine.queue_status()
300 301 self.assert_(isinstance(d, defer.Deferred))
301 302 dList.append(d)
302 303 D = defer.DeferredList(dList)
303 304 return D
304 305
305 306 def testClearQueue(self):
306 307 result = self.engine.clear_queue()
307 308 d1 = self.assertDeferredEquals(result, None)
308 309 d1.addCallback(lambda _: self.engine.queue_status())
309 310 d2 = self.assertDeferredEquals(d1, {'queue':[], 'pending':'None'})
310 311 return d2
311 312
312 313 def testQueueStatus(self):
313 314 result = self.engine.queue_status()
314 315 result.addCallback(lambda r: 'queue' in r and 'pending' in r)
315 316 d = self.assertDeferredEquals(result, True)
316 317 return d
317 318
318 319 Parametric(IEngineQueuedTestCase)
319 320
320 321 class IEnginePropertiesTestCase(object):
321 322 """Test an IEngineProperties implementor."""
322 323
323 324 def testIEnginePropertiesInterface(self):
324 325 """Does self.engine claim to implement IEngineProperties?"""
325 326 self.assert_(es.IEngineProperties.providedBy(self.engine))
326 327
327 328 def testIEnginePropertiesInterfaceMethods(self):
328 329 """Does self.engine have the methods and attributes in IEngireProperties."""
329 330 for m in list(es.IEngineProperties):
330 331 self.assert_(hasattr(self.engine, m))
331 332
332 333 def testGetSetProperties(self):
333 334 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
334 335 d = self.engine.set_properties(dikt)
335 336 d.addCallback(lambda r: self.engine.get_properties())
336 337 d = self.assertDeferredEquals(d, dikt)
337 338 d.addCallback(lambda r: self.engine.get_properties(('c',)))
338 339 d = self.assertDeferredEquals(d, {'c': dikt['c']})
339 340 d.addCallback(lambda r: self.engine.set_properties(dict(c=False)))
340 341 d.addCallback(lambda r: self.engine.get_properties(('c', 'd')))
341 342 d = self.assertDeferredEquals(d, dict(c=False, d=None))
342 343 return d
343 344
344 345 def testClearProperties(self):
345 346 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
346 347 d = self.engine.set_properties(dikt)
347 348 d.addCallback(lambda r: self.engine.clear_properties())
348 349 d.addCallback(lambda r: self.engine.get_properties())
349 350 d = self.assertDeferredEquals(d, {})
350 351 return d
351 352
352 353 def testDelHasProperties(self):
353 354 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
354 355 d = self.engine.set_properties(dikt)
355 356 d.addCallback(lambda r: self.engine.del_properties(('b','e')))
356 357 d.addCallback(lambda r: self.engine.has_properties(('a','b','c','d','e')))
357 358 d = self.assertDeferredEquals(d, [True, False, True, True, False])
358 359 return d
359 360
360 361 def testStrictDict(self):
361 362 s = """from IPython.kernel.engineservice import get_engine
362 363 p = get_engine(%s).properties"""%self.engine.id
363 364 d = self.engine.execute(s)
364 365 d.addCallback(lambda r: self.engine.execute("p['a'] = lambda _:None"))
365 366 d = self.assertDeferredRaises(d, error.InvalidProperty)
366 367 d.addCallback(lambda r: self.engine.execute("p['a'] = range(5)"))
367 368 d.addCallback(lambda r: self.engine.execute("p['a'].append(5)"))
368 369 d.addCallback(lambda r: self.engine.get_properties('a'))
369 370 d = self.assertDeferredEquals(d, dict(a=range(5)))
370 371 return d
371 372
372 373 Parametric(IEnginePropertiesTestCase)
@@ -1,828 +1,828 b''
1 1 # encoding: utf-8
2 2
3 3 """"""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 #-------------------------------------------------------------------------------
8 8 # Copyright (C) 2008 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-------------------------------------------------------------------------------
13 13
14 14 #-------------------------------------------------------------------------------
15 15 # Imports
16 16 #-------------------------------------------------------------------------------
17 17
18 18 from twisted.internet import defer
19 19
20 20 from IPython.kernel import engineservice as es
21 21 from IPython.kernel import multiengine as me
22 22 from IPython.kernel import newserialized
23 23 from IPython.kernel.error import NotDefined
24 24 from IPython.testing import util
25 25 from IPython.testing.parametric import parametric, Parametric
26 26 from IPython.kernel import newserialized
27 27 from IPython.kernel.util import printer
28 28 from IPython.kernel.error import (InvalidEngineID,
29 29 NoEnginesRegistered,
30 30 CompositeError,
31 31 InvalidDeferredID)
32 32 from IPython.kernel.tests.engineservicetest import validCommands, invalidCommands
33 33 from IPython.kernel.core.interpreter import Interpreter
34 34
35 35
36 36 #-------------------------------------------------------------------------------
37 37 # Base classes and utilities
38 38 #-------------------------------------------------------------------------------
39 39
40 40 class IMultiEngineBaseTestCase(object):
41 41 """Basic utilities for working with multiengine tests.
42 42
43 43 Some subclass should define:
44 44
45 45 * self.multiengine
46 46 * self.engines to keep track of engines for clean up"""
47 47
48 48 def createShell(self):
49 49 return Interpreter()
50 50
51 51 def addEngine(self, n=1):
52 52 for i in range(n):
53 53 e = es.EngineService()
54 54 e.startService()
55 55 regDict = self.controller.register_engine(es.QueuedEngine(e), None)
56 56 e.id = regDict['id']
57 57 self.engines.append(e)
58 58
59 59
60 60 def testf(x):
61 61 return 2.0*x
62 62
63 63
64 64 globala = 99
65 65
66 66
67 67 def testg(x):
68 68 return globala*x
69 69
70 70
71 71 def isdid(did):
72 72 if not isinstance(did, str):
73 73 return False
74 74 if not len(did)==40:
75 75 return False
76 76 return True
77 77
78 78
79 79 def _raise_it(f):
80 80 try:
81 81 f.raiseException()
82 82 except CompositeError, e:
83 83 e.raise_exception()
84 84
85 85 #-------------------------------------------------------------------------------
86 86 # IMultiEngineTestCase
87 87 #-------------------------------------------------------------------------------
88 88
89 89 class IMultiEngineTestCase(IMultiEngineBaseTestCase):
90 90 """A test for any object that implements IEngineMultiplexer.
91 91
92 92 self.multiengine must be defined and implement IEngineMultiplexer.
93 93 """
94 94
95 95 def testIMultiEngineInterface(self):
96 96 """Does self.engine claim to implement IEngineCore?"""
97 97 self.assert_(me.IEngineMultiplexer.providedBy(self.multiengine))
98 98 self.assert_(me.IMultiEngine.providedBy(self.multiengine))
99 99
100 100 def testIEngineMultiplexerInterfaceMethods(self):
101 101 """Does self.engine have the methods and attributes in IEngineCore."""
102 102 for m in list(me.IEngineMultiplexer):
103 103 self.assert_(hasattr(self.multiengine, m))
104 104
105 105 def testIEngineMultiplexerDeferreds(self):
106 106 self.addEngine(1)
107 107 d= self.multiengine.execute('a=5', targets=0)
108 108 d.addCallback(lambda _: self.multiengine.push(dict(a=5),targets=0))
109 109 d.addCallback(lambda _: self.multiengine.push(dict(a=5, b='asdf', c=[1,2,3]),targets=0))
110 110 d.addCallback(lambda _: self.multiengine.pull(('a','b','c'),targets=0))
111 111 d.addCallback(lambda _: self.multiengine.get_result(targets=0))
112 112 d.addCallback(lambda _: self.multiengine.reset(targets=0))
113 113 d.addCallback(lambda _: self.multiengine.keys(targets=0))
114 114 d.addCallback(lambda _: self.multiengine.push_serialized(dict(a=newserialized.serialize(10)),targets=0))
115 115 d.addCallback(lambda _: self.multiengine.pull_serialized('a',targets=0))
116 116 d.addCallback(lambda _: self.multiengine.clear_queue(targets=0))
117 117 d.addCallback(lambda _: self.multiengine.queue_status(targets=0))
118 118 return d
119 119
120 120 def testInvalidEngineID(self):
121 121 self.addEngine(1)
122 122 badID = 100
123 123 d = self.multiengine.execute('a=5', targets=badID)
124 124 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
125 125 d.addCallback(lambda _: self.multiengine.push(dict(a=5), targets=badID))
126 126 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
127 127 d.addCallback(lambda _: self.multiengine.pull('a', targets=badID))
128 128 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
129 129 d.addCallback(lambda _: self.multiengine.reset(targets=badID))
130 130 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
131 131 d.addCallback(lambda _: self.multiengine.keys(targets=badID))
132 132 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
133 133 d.addCallback(lambda _: self.multiengine.push_serialized(dict(a=newserialized.serialize(10)), targets=badID))
134 134 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
135 135 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=badID))
136 136 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
137 137 d.addCallback(lambda _: self.multiengine.queue_status(targets=badID))
138 138 d.addErrback(lambda f: self.assertRaises(InvalidEngineID, f.raiseException))
139 139 return d
140 140
141 141 def testNoEnginesRegistered(self):
142 142 badID = 'all'
143 143 d= self.multiengine.execute('a=5', targets=badID)
144 144 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
145 145 d.addCallback(lambda _: self.multiengine.push(dict(a=5), targets=badID))
146 146 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
147 147 d.addCallback(lambda _: self.multiengine.pull('a', targets=badID))
148 148 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
149 149 d.addCallback(lambda _: self.multiengine.get_result(targets=badID))
150 150 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
151 151 d.addCallback(lambda _: self.multiengine.reset(targets=badID))
152 152 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
153 153 d.addCallback(lambda _: self.multiengine.keys(targets=badID))
154 154 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
155 155 d.addCallback(lambda _: self.multiengine.push_serialized(dict(a=newserialized.serialize(10)), targets=badID))
156 156 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
157 157 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=badID))
158 158 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
159 159 d.addCallback(lambda _: self.multiengine.queue_status(targets=badID))
160 160 d.addErrback(lambda f: self.assertRaises(NoEnginesRegistered, f.raiseException))
161 161 return d
162 162
163 163 def runExecuteAll(self, d, cmd, shell):
164 164 actual = shell.execute(cmd)
165 165 d.addCallback(lambda _: self.multiengine.execute(cmd))
166 166 def compare(result):
167 167 for r in result:
168 168 actual['id'] = r['id']
169 169 self.assertEquals(r, actual)
170 170 d.addCallback(compare)
171 171
172 172 def testExecuteAll(self):
173 173 self.addEngine(4)
174 174 d= defer.Deferred()
175 175 shell = Interpreter()
176 176 for cmd in validCommands:
177 177 self.runExecuteAll(d, cmd, shell)
178 178 d.callback(None)
179 179 return d
180 180
181 181 # The following two methods show how to do parametrized
182 182 # tests. This is really slick! Same is used above.
183 183 def runExecuteFailures(self, cmd, exc):
184 184 self.addEngine(4)
185 185 d= self.multiengine.execute(cmd)
186 186 d.addErrback(lambda f: self.assertRaises(exc, _raise_it, f))
187 187 return d
188 188
189 189 @parametric
190 def testExecuteFailures(cls):
190 def testExecuteFailuresMultiEng(cls):
191 191 return [(cls.runExecuteFailures,cmd,exc) for
192 192 cmd,exc in invalidCommands]
193 193
194 194 def testPushPull(self):
195 195 self.addEngine(1)
196 196 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
197 197 d= self.multiengine.push(dict(key=objs[0]), targets=0)
198 198 d.addCallback(lambda _: self.multiengine.pull('key', targets=0))
199 199 d.addCallback(lambda r: self.assertEquals(r, [objs[0]]))
200 200 d.addCallback(lambda _: self.multiengine.push(dict(key=objs[1]), targets=0))
201 201 d.addCallback(lambda _: self.multiengine.pull('key', targets=0))
202 202 d.addCallback(lambda r: self.assertEquals(r, [objs[1]]))
203 203 d.addCallback(lambda _: self.multiengine.push(dict(key=objs[2]), targets=0))
204 204 d.addCallback(lambda _: self.multiengine.pull('key', targets=0))
205 205 d.addCallback(lambda r: self.assertEquals(r, [objs[2]]))
206 206 d.addCallback(lambda _: self.multiengine.push(dict(key=objs[3]), targets=0))
207 207 d.addCallback(lambda _: self.multiengine.pull('key', targets=0))
208 208 d.addCallback(lambda r: self.assertEquals(r, [objs[3]]))
209 209 d.addCallback(lambda _: self.multiengine.reset(targets=0))
210 210 d.addCallback(lambda _: self.multiengine.pull('a', targets=0))
211 211 d.addErrback(lambda f: self.assertRaises(NameError, _raise_it, f))
212 212 d.addCallback(lambda _: self.multiengine.push(dict(a=10,b=20)))
213 213 d.addCallback(lambda _: self.multiengine.pull(('a','b')))
214 214 d.addCallback(lambda r: self.assertEquals(r, [[10,20]]))
215 215 return d
216 216
217 217 def testPushPullAll(self):
218 218 self.addEngine(4)
219 219 d= self.multiengine.push(dict(a=10))
220 220 d.addCallback(lambda _: self.multiengine.pull('a'))
221 221 d.addCallback(lambda r: self.assert_(r==[10,10,10,10]))
222 222 d.addCallback(lambda _: self.multiengine.push(dict(a=10, b=20)))
223 223 d.addCallback(lambda _: self.multiengine.pull(('a','b')))
224 224 d.addCallback(lambda r: self.assert_(r==4*[[10,20]]))
225 225 d.addCallback(lambda _: self.multiengine.push(dict(a=10, b=20), targets=0))
226 226 d.addCallback(lambda _: self.multiengine.pull(('a','b'), targets=0))
227 227 d.addCallback(lambda r: self.assert_(r==[[10,20]]))
228 228 d.addCallback(lambda _: self.multiengine.push(dict(a=None, b=None), targets=0))
229 229 d.addCallback(lambda _: self.multiengine.pull(('a','b'), targets=0))
230 230 d.addCallback(lambda r: self.assert_(r==[[None,None]]))
231 231 return d
232 232
233 233 def testPushPullSerialized(self):
234 234 self.addEngine(1)
235 235 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
236 236 d= self.multiengine.push_serialized(dict(key=newserialized.serialize(objs[0])), targets=0)
237 237 d.addCallback(lambda _: self.multiengine.pull_serialized('key', targets=0))
238 238 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
239 239 d.addCallback(lambda r: self.assertEquals(r, objs[0]))
240 240 d.addCallback(lambda _: self.multiengine.push_serialized(dict(key=newserialized.serialize(objs[1])), targets=0))
241 241 d.addCallback(lambda _: self.multiengine.pull_serialized('key', targets=0))
242 242 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
243 243 d.addCallback(lambda r: self.assertEquals(r, objs[1]))
244 244 d.addCallback(lambda _: self.multiengine.push_serialized(dict(key=newserialized.serialize(objs[2])), targets=0))
245 245 d.addCallback(lambda _: self.multiengine.pull_serialized('key', targets=0))
246 246 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
247 247 d.addCallback(lambda r: self.assertEquals(r, objs[2]))
248 248 d.addCallback(lambda _: self.multiengine.push_serialized(dict(key=newserialized.serialize(objs[3])), targets=0))
249 249 d.addCallback(lambda _: self.multiengine.pull_serialized('key', targets=0))
250 250 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
251 251 d.addCallback(lambda r: self.assertEquals(r, objs[3]))
252 252 d.addCallback(lambda _: self.multiengine.push(dict(a=10,b=range(5)), targets=0))
253 253 d.addCallback(lambda _: self.multiengine.pull_serialized(('a','b'), targets=0))
254 254 d.addCallback(lambda serial: [newserialized.IUnSerialized(s).getObject() for s in serial[0]])
255 255 d.addCallback(lambda r: self.assertEquals(r, [10, range(5)]))
256 256 d.addCallback(lambda _: self.multiengine.reset(targets=0))
257 257 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=0))
258 258 d.addErrback(lambda f: self.assertRaises(NameError, _raise_it, f))
259 259 return d
260 260
261 261 objs = [10,"hi there",1.2342354,{"p":(1,2)}]
262 262 d= defer.succeed(None)
263 263 for o in objs:
264 264 self.multiengine.push_serialized(0, key=newserialized.serialize(o))
265 265 value = self.multiengine.pull_serialized(0, 'key')
266 266 value.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
267 267 d = self.assertDeferredEquals(value,o,d)
268 268 return d
269 269
270 270 def runGetResultAll(self, d, cmd, shell):
271 271 actual = shell.execute(cmd)
272 272 d.addCallback(lambda _: self.multiengine.execute(cmd))
273 273 d.addCallback(lambda _: self.multiengine.get_result())
274 274 def compare(result):
275 275 for r in result:
276 276 actual['id'] = r['id']
277 277 self.assertEquals(r, actual)
278 278 d.addCallback(compare)
279 279
280 280 def testGetResultAll(self):
281 281 self.addEngine(4)
282 282 d= defer.Deferred()
283 283 shell = Interpreter()
284 284 for cmd in validCommands:
285 285 self.runGetResultAll(d, cmd, shell)
286 286 d.callback(None)
287 287 return d
288 288
289 289 def testGetResultDefault(self):
290 290 self.addEngine(1)
291 291 target = 0
292 292 cmd = 'a=5'
293 293 shell = self.createShell()
294 294 shellResult = shell.execute(cmd)
295 295 def popit(dikt, key):
296 296 dikt.pop(key)
297 297 return dikt
298 298 d= self.multiengine.execute(cmd, targets=target)
299 299 d.addCallback(lambda _: self.multiengine.get_result(targets=target))
300 300 d.addCallback(lambda r: self.assertEquals(shellResult, popit(r[0],'id')))
301 301 return d
302 302
303 303 def testGetResultFailure(self):
304 304 self.addEngine(1)
305 305 d= self.multiengine.get_result(None, targets=0)
306 306 d.addErrback(lambda f: self.assertRaises(IndexError, _raise_it, f))
307 307 d.addCallback(lambda _: self.multiengine.get_result(10, targets=0))
308 308 d.addErrback(lambda f: self.assertRaises(IndexError, _raise_it, f))
309 309 return d
310 310
311 311 def testPushFunction(self):
312 312 self.addEngine(1)
313 313 d= self.multiengine.push_function(dict(f=testf), targets=0)
314 314 d.addCallback(lambda _: self.multiengine.execute('result = f(10)', targets=0))
315 315 d.addCallback(lambda _: self.multiengine.pull('result', targets=0))
316 316 d.addCallback(lambda r: self.assertEquals(r[0], testf(10)))
317 317 d.addCallback(lambda _: self.multiengine.push(dict(globala=globala), targets=0))
318 318 d.addCallback(lambda _: self.multiengine.push_function(dict(g=testg), targets=0))
319 319 d.addCallback(lambda _: self.multiengine.execute('result = g(10)', targets=0))
320 320 d.addCallback(lambda _: self.multiengine.pull('result', targets=0))
321 321 d.addCallback(lambda r: self.assertEquals(r[0], testg(10)))
322 322 return d
323 323
324 324 def testPullFunction(self):
325 325 self.addEngine(1)
326 326 d= self.multiengine.push(dict(a=globala), targets=0)
327 327 d.addCallback(lambda _: self.multiengine.push_function(dict(f=testf), targets=0))
328 328 d.addCallback(lambda _: self.multiengine.pull_function('f', targets=0))
329 329 d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
330 330 d.addCallback(lambda _: self.multiengine.execute("def g(x): return x*x", targets=0))
331 331 d.addCallback(lambda _: self.multiengine.pull_function(('f','g'),targets=0))
332 332 d.addCallback(lambda r: self.assertEquals((r[0][0](10),r[0][1](10)), (testf(10), 100)))
333 333 return d
334 334
335 335 def testPushFunctionAll(self):
336 336 self.addEngine(4)
337 337 d= self.multiengine.push_function(dict(f=testf))
338 338 d.addCallback(lambda _: self.multiengine.execute('result = f(10)'))
339 339 d.addCallback(lambda _: self.multiengine.pull('result'))
340 340 d.addCallback(lambda r: self.assertEquals(r, 4*[testf(10)]))
341 341 d.addCallback(lambda _: self.multiengine.push(dict(globala=globala)))
342 342 d.addCallback(lambda _: self.multiengine.push_function(dict(testg=testg)))
343 343 d.addCallback(lambda _: self.multiengine.execute('result = testg(10)'))
344 344 d.addCallback(lambda _: self.multiengine.pull('result'))
345 345 d.addCallback(lambda r: self.assertEquals(r, 4*[testg(10)]))
346 346 return d
347 347
348 348 def testPullFunctionAll(self):
349 349 self.addEngine(4)
350 350 d= self.multiengine.push_function(dict(f=testf))
351 351 d.addCallback(lambda _: self.multiengine.pull_function('f'))
352 352 d.addCallback(lambda r: self.assertEquals([func(10) for func in r], 4*[testf(10)]))
353 353 return d
354 354
355 355 def testGetIDs(self):
356 356 self.addEngine(1)
357 357 d= self.multiengine.get_ids()
358 358 d.addCallback(lambda r: self.assertEquals(r, [0]))
359 359 d.addCallback(lambda _: self.addEngine(3))
360 360 d.addCallback(lambda _: self.multiengine.get_ids())
361 361 d.addCallback(lambda r: self.assertEquals(r, [0,1,2,3]))
362 362 return d
363 363
364 364 def testClearQueue(self):
365 365 self.addEngine(4)
366 366 d= self.multiengine.clear_queue()
367 367 d.addCallback(lambda r: self.assertEquals(r,4*[None]))
368 368 return d
369 369
370 370 def testQueueStatus(self):
371 371 self.addEngine(4)
372 372 d= self.multiengine.queue_status(targets=0)
373 373 d.addCallback(lambda r: self.assert_(isinstance(r[0],tuple)))
374 374 return d
375 375
376 376 def testGetSetProperties(self):
377 377 self.addEngine(4)
378 378 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
379 379 d= self.multiengine.set_properties(dikt)
380 380 d.addCallback(lambda r: self.multiengine.get_properties())
381 381 d.addCallback(lambda r: self.assertEquals(r, 4*[dikt]))
382 382 d.addCallback(lambda r: self.multiengine.get_properties(('c',)))
383 383 d.addCallback(lambda r: self.assertEquals(r, 4*[{'c': dikt['c']}]))
384 384 d.addCallback(lambda r: self.multiengine.set_properties(dict(c=False)))
385 385 d.addCallback(lambda r: self.multiengine.get_properties(('c', 'd')))
386 386 d.addCallback(lambda r: self.assertEquals(r, 4*[dict(c=False, d=None)]))
387 387 return d
388 388
389 389 def testClearProperties(self):
390 390 self.addEngine(4)
391 391 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
392 392 d= self.multiengine.set_properties(dikt)
393 393 d.addCallback(lambda r: self.multiengine.clear_properties())
394 394 d.addCallback(lambda r: self.multiengine.get_properties())
395 395 d.addCallback(lambda r: self.assertEquals(r, 4*[{}]))
396 396 return d
397 397
398 398 def testDelHasProperties(self):
399 399 self.addEngine(4)
400 400 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
401 401 d= self.multiengine.set_properties(dikt)
402 402 d.addCallback(lambda r: self.multiengine.del_properties(('b','e')))
403 403 d.addCallback(lambda r: self.multiengine.has_properties(('a','b','c','d','e')))
404 404 d.addCallback(lambda r: self.assertEquals(r, 4*[[True, False, True, True, False]]))
405 405 return d
406 406
407 407 Parametric(IMultiEngineTestCase)
408 408
409 409 #-------------------------------------------------------------------------------
410 410 # ISynchronousMultiEngineTestCase
411 411 #-------------------------------------------------------------------------------
412 412
413 413 class ISynchronousMultiEngineTestCase(IMultiEngineBaseTestCase):
414 414
415 415 def testISynchronousMultiEngineInterface(self):
416 416 """Does self.engine claim to implement IEngineCore?"""
417 417 self.assert_(me.ISynchronousEngineMultiplexer.providedBy(self.multiengine))
418 418 self.assert_(me.ISynchronousMultiEngine.providedBy(self.multiengine))
419 419
420 420 def testExecute(self):
421 421 self.addEngine(4)
422 422 execute = self.multiengine.execute
423 423 d= execute('a=5', targets=0, block=True)
424 424 d.addCallback(lambda r: self.assert_(len(r)==1))
425 425 d.addCallback(lambda _: execute('b=10'))
426 426 d.addCallback(lambda r: self.assert_(len(r)==4))
427 427 d.addCallback(lambda _: execute('c=30', block=False))
428 428 d.addCallback(lambda did: self.assert_(isdid(did)))
429 429 d.addCallback(lambda _: execute('d=[0,1,2]', block=False))
430 430 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
431 431 d.addCallback(lambda r: self.assert_(len(r)==4))
432 432 return d
433 433
434 434 def testPushPull(self):
435 435 data = dict(a=10, b=1.05, c=range(10), d={'e':(1,2),'f':'hi'})
436 436 self.addEngine(4)
437 437 push = self.multiengine.push
438 438 pull = self.multiengine.pull
439 439 d= push({'data':data}, targets=0)
440 440 d.addCallback(lambda r: pull('data', targets=0))
441 441 d.addCallback(lambda r: self.assertEqual(r,[data]))
442 442 d.addCallback(lambda _: push({'data':data}))
443 443 d.addCallback(lambda r: pull('data'))
444 444 d.addCallback(lambda r: self.assertEqual(r,4*[data]))
445 445 d.addCallback(lambda _: push({'data':data}, block=False))
446 446 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
447 447 d.addCallback(lambda _: pull('data', block=False))
448 448 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
449 449 d.addCallback(lambda r: self.assertEqual(r,4*[data]))
450 450 d.addCallback(lambda _: push(dict(a=10,b=20)))
451 451 d.addCallback(lambda _: pull(('a','b')))
452 452 d.addCallback(lambda r: self.assertEquals(r, 4*[[10,20]]))
453 453 return d
454 454
455 455 def testPushPullFunction(self):
456 456 self.addEngine(4)
457 457 pushf = self.multiengine.push_function
458 458 pullf = self.multiengine.pull_function
459 459 push = self.multiengine.push
460 460 pull = self.multiengine.pull
461 461 execute = self.multiengine.execute
462 462 d= pushf({'testf':testf}, targets=0)
463 463 d.addCallback(lambda r: pullf('testf', targets=0))
464 464 d.addCallback(lambda r: self.assertEqual(r[0](1.0), testf(1.0)))
465 465 d.addCallback(lambda _: execute('r = testf(10)', targets=0))
466 466 d.addCallback(lambda _: pull('r', targets=0))
467 467 d.addCallback(lambda r: self.assertEquals(r[0], testf(10)))
468 468 d.addCallback(lambda _: pushf({'testf':testf}, block=False))
469 469 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
470 470 d.addCallback(lambda _: pullf('testf', block=False))
471 471 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
472 472 d.addCallback(lambda r: self.assertEqual(r[0](1.0), testf(1.0)))
473 473 d.addCallback(lambda _: execute("def g(x): return x*x", targets=0))
474 474 d.addCallback(lambda _: pullf(('testf','g'),targets=0))
475 475 d.addCallback(lambda r: self.assertEquals((r[0][0](10),r[0][1](10)), (testf(10), 100)))
476 476 return d
477 477
478 478 def testGetResult(self):
479 479 shell = Interpreter()
480 480 result1 = shell.execute('a=10')
481 481 result1['id'] = 0
482 482 result2 = shell.execute('b=20')
483 483 result2['id'] = 0
484 484 execute= self.multiengine.execute
485 485 get_result = self.multiengine.get_result
486 486 self.addEngine(1)
487 487 d= execute('a=10')
488 488 d.addCallback(lambda _: get_result())
489 489 d.addCallback(lambda r: self.assertEquals(r[0], result1))
490 490 d.addCallback(lambda _: execute('b=20'))
491 491 d.addCallback(lambda _: get_result(1))
492 492 d.addCallback(lambda r: self.assertEquals(r[0], result1))
493 493 d.addCallback(lambda _: get_result(2, block=False))
494 494 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
495 495 d.addCallback(lambda r: self.assertEquals(r[0], result2))
496 496 return d
497 497
498 498 def testResetAndKeys(self):
499 499 self.addEngine(1)
500 500
501 501 #Blocking mode
502 502 d= self.multiengine.push(dict(a=10, b=20, c=range(10)), targets=0)
503 503 d.addCallback(lambda _: self.multiengine.keys(targets=0))
504 504 def keys_found(keys):
505 505 self.assert_('a' in keys[0])
506 506 self.assert_('b' in keys[0])
507 507 self.assert_('b' in keys[0])
508 508 d.addCallback(keys_found)
509 509 d.addCallback(lambda _: self.multiengine.reset(targets=0))
510 510 d.addCallback(lambda _: self.multiengine.keys(targets=0))
511 511 def keys_not_found(keys):
512 512 self.assert_('a' not in keys[0])
513 513 self.assert_('b' not in keys[0])
514 514 self.assert_('b' not in keys[0])
515 515 d.addCallback(keys_not_found)
516 516
517 517 #Non-blocking mode
518 518 d.addCallback(lambda _: self.multiengine.push(dict(a=10, b=20, c=range(10)), targets=0))
519 519 d.addCallback(lambda _: self.multiengine.keys(targets=0, block=False))
520 520 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
521 521 def keys_found(keys):
522 522 self.assert_('a' in keys[0])
523 523 self.assert_('b' in keys[0])
524 524 self.assert_('b' in keys[0])
525 525 d.addCallback(keys_found)
526 526 d.addCallback(lambda _: self.multiengine.reset(targets=0, block=False))
527 527 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
528 528 d.addCallback(lambda _: self.multiengine.keys(targets=0, block=False))
529 529 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
530 530 def keys_not_found(keys):
531 531 self.assert_('a' not in keys[0])
532 532 self.assert_('b' not in keys[0])
533 533 self.assert_('b' not in keys[0])
534 534 d.addCallback(keys_not_found)
535 535
536 536 return d
537 537
538 538 def testPushPullSerialized(self):
539 539 self.addEngine(1)
540 540 dikt = dict(a=10,b='hi there',c=1.2345,d={'p':(1,2)})
541 541 sdikt = {}
542 542 for k,v in dikt.iteritems():
543 543 sdikt[k] = newserialized.serialize(v)
544 544 d= self.multiengine.push_serialized(dict(a=sdikt['a']), targets=0)
545 545 d.addCallback(lambda _: self.multiengine.pull('a',targets=0))
546 546 d.addCallback(lambda r: self.assertEquals(r[0], dikt['a']))
547 547 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=0))
548 548 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
549 549 d.addCallback(lambda r: self.assertEquals(r, dikt['a']))
550 550 d.addCallback(lambda _: self.multiengine.push_serialized(sdikt, targets=0))
551 551 d.addCallback(lambda _: self.multiengine.pull_serialized(sdikt.keys(), targets=0))
552 552 d.addCallback(lambda serial: [newserialized.IUnSerialized(s).getObject() for s in serial[0]])
553 553 d.addCallback(lambda r: self.assertEquals(r, dikt.values()))
554 554 d.addCallback(lambda _: self.multiengine.reset(targets=0))
555 555 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=0))
556 556 d.addErrback(lambda f: self.assertRaises(NameError, _raise_it, f))
557 557
558 558 #Non-blocking mode
559 559 d.addCallback(lambda r: self.multiengine.push_serialized(dict(a=sdikt['a']), targets=0, block=False))
560 560 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
561 561 d.addCallback(lambda _: self.multiengine.pull('a',targets=0))
562 562 d.addCallback(lambda r: self.assertEquals(r[0], dikt['a']))
563 563 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=0, block=False))
564 564 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
565 565 d.addCallback(lambda serial: newserialized.IUnSerialized(serial[0]).getObject())
566 566 d.addCallback(lambda r: self.assertEquals(r, dikt['a']))
567 567 d.addCallback(lambda _: self.multiengine.push_serialized(sdikt, targets=0, block=False))
568 568 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
569 569 d.addCallback(lambda _: self.multiengine.pull_serialized(sdikt.keys(), targets=0, block=False))
570 570 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
571 571 d.addCallback(lambda serial: [newserialized.IUnSerialized(s).getObject() for s in serial[0]])
572 572 d.addCallback(lambda r: self.assertEquals(r, dikt.values()))
573 573 d.addCallback(lambda _: self.multiengine.reset(targets=0))
574 574 d.addCallback(lambda _: self.multiengine.pull_serialized('a', targets=0, block=False))
575 575 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
576 576 d.addErrback(lambda f: self.assertRaises(NameError, _raise_it, f))
577 577 return d
578 578
579 579 def testClearQueue(self):
580 580 self.addEngine(4)
581 581 d= self.multiengine.clear_queue()
582 582 d.addCallback(lambda r: self.multiengine.clear_queue(block=False))
583 583 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
584 584 d.addCallback(lambda r: self.assertEquals(r,4*[None]))
585 585 return d
586 586
587 587 def testQueueStatus(self):
588 588 self.addEngine(4)
589 589 d= self.multiengine.queue_status(targets=0)
590 590 d.addCallback(lambda r: self.assert_(isinstance(r[0],tuple)))
591 591 d.addCallback(lambda r: self.multiengine.queue_status(targets=0, block=False))
592 592 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
593 593 d.addCallback(lambda r: self.assert_(isinstance(r[0],tuple)))
594 594 return d
595 595
596 596 def testGetIDs(self):
597 597 self.addEngine(1)
598 598 d= self.multiengine.get_ids()
599 599 d.addCallback(lambda r: self.assertEquals(r, [0]))
600 600 d.addCallback(lambda _: self.addEngine(3))
601 601 d.addCallback(lambda _: self.multiengine.get_ids())
602 602 d.addCallback(lambda r: self.assertEquals(r, [0,1,2,3]))
603 603 return d
604 604
605 605 def testGetSetProperties(self):
606 606 self.addEngine(4)
607 607 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
608 608 d= self.multiengine.set_properties(dikt)
609 609 d.addCallback(lambda r: self.multiengine.get_properties())
610 610 d.addCallback(lambda r: self.assertEquals(r, 4*[dikt]))
611 611 d.addCallback(lambda r: self.multiengine.get_properties(('c',)))
612 612 d.addCallback(lambda r: self.assertEquals(r, 4*[{'c': dikt['c']}]))
613 613 d.addCallback(lambda r: self.multiengine.set_properties(dict(c=False)))
614 614 d.addCallback(lambda r: self.multiengine.get_properties(('c', 'd')))
615 615 d.addCallback(lambda r: self.assertEquals(r, 4*[dict(c=False, d=None)]))
616 616
617 617 #Non-blocking
618 618 d.addCallback(lambda r: self.multiengine.set_properties(dikt, block=False))
619 619 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
620 620 d.addCallback(lambda r: self.multiengine.get_properties(block=False))
621 621 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
622 622 d.addCallback(lambda r: self.assertEquals(r, 4*[dikt]))
623 623 d.addCallback(lambda r: self.multiengine.get_properties(('c',), block=False))
624 624 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
625 625 d.addCallback(lambda r: self.assertEquals(r, 4*[{'c': dikt['c']}]))
626 626 d.addCallback(lambda r: self.multiengine.set_properties(dict(c=False), block=False))
627 627 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
628 628 d.addCallback(lambda r: self.multiengine.get_properties(('c', 'd'), block=False))
629 629 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
630 630 d.addCallback(lambda r: self.assertEquals(r, 4*[dict(c=False, d=None)]))
631 631 return d
632 632
633 633 def testClearProperties(self):
634 634 self.addEngine(4)
635 635 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
636 636 d= self.multiengine.set_properties(dikt)
637 637 d.addCallback(lambda r: self.multiengine.clear_properties())
638 638 d.addCallback(lambda r: self.multiengine.get_properties())
639 639 d.addCallback(lambda r: self.assertEquals(r, 4*[{}]))
640 640
641 641 #Non-blocking
642 642 d.addCallback(lambda r: self.multiengine.set_properties(dikt, block=False))
643 643 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
644 644 d.addCallback(lambda r: self.multiengine.clear_properties(block=False))
645 645 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
646 646 d.addCallback(lambda r: self.multiengine.get_properties(block=False))
647 647 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
648 648 d.addCallback(lambda r: self.assertEquals(r, 4*[{}]))
649 649 return d
650 650
651 651 def testDelHasProperties(self):
652 652 self.addEngine(4)
653 653 dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5))
654 654 d= self.multiengine.set_properties(dikt)
655 655 d.addCallback(lambda r: self.multiengine.del_properties(('b','e')))
656 656 d.addCallback(lambda r: self.multiengine.has_properties(('a','b','c','d','e')))
657 657 d.addCallback(lambda r: self.assertEquals(r, 4*[[True, False, True, True, False]]))
658 658
659 659 #Non-blocking
660 660 d.addCallback(lambda r: self.multiengine.set_properties(dikt, block=False))
661 661 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
662 662 d.addCallback(lambda r: self.multiengine.del_properties(('b','e'), block=False))
663 663 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
664 664 d.addCallback(lambda r: self.multiengine.has_properties(('a','b','c','d','e'), block=False))
665 665 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
666 666 d.addCallback(lambda r: self.assertEquals(r, 4*[[True, False, True, True, False]]))
667 667 return d
668 668
669 669 def test_clear_pending_deferreds(self):
670 670 self.addEngine(4)
671 671 did_list = []
672 672 d= self.multiengine.execute('a=10',block=False)
673 673 d.addCallback(lambda did: did_list.append(did))
674 674 d.addCallback(lambda _: self.multiengine.push(dict(b=10),block=False))
675 675 d.addCallback(lambda did: did_list.append(did))
676 676 d.addCallback(lambda _: self.multiengine.pull(('a','b'),block=False))
677 677 d.addCallback(lambda did: did_list.append(did))
678 678 d.addCallback(lambda _: self.multiengine.clear_pending_deferreds())
679 679 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[0],True))
680 680 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
681 681 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[1],True))
682 682 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
683 683 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[2],True))
684 684 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
685 685 return d
686 686
687 687 #-------------------------------------------------------------------------------
688 688 # Coordinator test cases
689 689 #-------------------------------------------------------------------------------
690 690
691 691 class IMultiEngineCoordinatorTestCase(object):
692 692
693 693 def testScatterGather(self):
694 694 self.addEngine(4)
695 695 d= self.multiengine.scatter('a', range(16))
696 696 d.addCallback(lambda r: self.multiengine.gather('a'))
697 697 d.addCallback(lambda r: self.assertEquals(r, range(16)))
698 698 d.addCallback(lambda _: self.multiengine.gather('asdf'))
699 699 d.addErrback(lambda f: self.assertRaises(NameError, _raise_it, f))
700 700 return d
701 701
702 702 def testScatterGatherNumpy(self):
703 703 try:
704 704 import numpy
705 705 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
706 706 except:
707 707 return
708 708 else:
709 709 self.addEngine(4)
710 710 a = numpy.arange(16)
711 711 d = self.multiengine.scatter('a', a)
712 712 d.addCallback(lambda r: self.multiengine.gather('a'))
713 713 d.addCallback(lambda r: assert_array_equal(r, a))
714 714 return d
715 715
716 716 def testMap(self):
717 717 self.addEngine(4)
718 718 def f(x):
719 719 return x**2
720 720 data = range(16)
721 721 d= self.multiengine.map(f, data)
722 722 d.addCallback(lambda r: self.assertEquals(r,[f(x) for x in data]))
723 723 return d
724 724
725 725
726 726 class ISynchronousMultiEngineCoordinatorTestCase(IMultiEngineCoordinatorTestCase):
727 727
728 728 def testScatterGatherNonblocking(self):
729 729 self.addEngine(4)
730 730 d= self.multiengine.scatter('a', range(16), block=False)
731 731 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
732 732 d.addCallback(lambda r: self.multiengine.gather('a', block=False))
733 733 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
734 734 d.addCallback(lambda r: self.assertEquals(r, range(16)))
735 735 return d
736 736
737 737 def testScatterGatherNumpyNonblocking(self):
738 738 try:
739 739 import numpy
740 740 from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
741 741 except:
742 742 return
743 743 else:
744 744 self.addEngine(4)
745 745 a = numpy.arange(16)
746 746 d = self.multiengine.scatter('a', a, block=False)
747 747 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
748 748 d.addCallback(lambda r: self.multiengine.gather('a', block=False))
749 749 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
750 750 d.addCallback(lambda r: assert_array_equal(r, a))
751 751 return d
752 752
753 753 def test_clear_pending_deferreds(self):
754 754 self.addEngine(4)
755 755 did_list = []
756 756 d= self.multiengine.scatter('a',range(16),block=False)
757 757 d.addCallback(lambda did: did_list.append(did))
758 758 d.addCallback(lambda _: self.multiengine.gather('a',block=False))
759 759 d.addCallback(lambda did: did_list.append(did))
760 760 d.addCallback(lambda _: self.multiengine.map(lambda x: x, range(16),block=False))
761 761 d.addCallback(lambda did: did_list.append(did))
762 762 d.addCallback(lambda _: self.multiengine.clear_pending_deferreds())
763 763 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[0],True))
764 764 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
765 765 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[1],True))
766 766 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
767 767 d.addCallback(lambda _: self.multiengine.get_pending_deferred(did_list[2],True))
768 768 d.addErrback(lambda f: self.assertRaises(InvalidDeferredID, f.raiseException))
769 769 return d
770 770
771 771 #-------------------------------------------------------------------------------
772 772 # Extras test cases
773 773 #-------------------------------------------------------------------------------
774 774
775 775 class IMultiEngineExtrasTestCase(object):
776 776
777 777 def testZipPull(self):
778 778 self.addEngine(4)
779 779 d= self.multiengine.push(dict(a=10,b=20))
780 780 d.addCallback(lambda r: self.multiengine.zip_pull(('a','b')))
781 781 d.addCallback(lambda r: self.assert_(r, [4*[10],4*[20]]))
782 782 return d
783 783
784 784 def testRun(self):
785 785 self.addEngine(4)
786 786 import tempfile
787 787 fname = tempfile.mktemp('foo.py')
788 788 f= open(fname, 'w')
789 789 f.write('a = 10\nb=30')
790 790 f.close()
791 791 d= self.multiengine.run(fname)
792 792 d.addCallback(lambda r: self.multiengine.pull(('a','b')))
793 793 d.addCallback(lambda r: self.assertEquals(r, 4*[[10,30]]))
794 794 return d
795 795
796 796
797 797 class ISynchronousMultiEngineExtrasTestCase(IMultiEngineExtrasTestCase):
798 798
799 799 def testZipPullNonblocking(self):
800 800 self.addEngine(4)
801 801 d= self.multiengine.push(dict(a=10,b=20))
802 802 d.addCallback(lambda r: self.multiengine.zip_pull(('a','b'), block=False))
803 803 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
804 804 d.addCallback(lambda r: self.assert_(r, [4*[10],4*[20]]))
805 805 return d
806 806
807 807 def testRunNonblocking(self):
808 808 self.addEngine(4)
809 809 import tempfile
810 810 fname = tempfile.mktemp('foo.py')
811 811 f= open(fname, 'w')
812 812 f.write('a = 10\nb=30')
813 813 f.close()
814 814 d= self.multiengine.run(fname, block=False)
815 815 d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
816 816 d.addCallback(lambda r: self.multiengine.pull(('a','b')))
817 817 d.addCallback(lambda r: self.assertEquals(r, 4*[[10,30]]))
818 818 return d
819 819
820 820
821 821 #-------------------------------------------------------------------------------
822 822 # IFullSynchronousMultiEngineTestCase
823 823 #-------------------------------------------------------------------------------
824 824
825 825 class IFullSynchronousMultiEngineTestCase(ISynchronousMultiEngineTestCase,
826 826 ISynchronousMultiEngineCoordinatorTestCase,
827 827 ISynchronousMultiEngineExtrasTestCase):
828 828 pass
General Comments 0
You need to be logged in to leave comments. Login now