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