Show More
@@ -1,374 +1,377 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """Test template for complete engine object""" |
|
4 | 4 | |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #------------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #------------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #------------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | import cPickle as pickle |
|
19 | 19 | |
|
20 | 20 | from twisted.internet import defer, reactor |
|
21 | 21 | from twisted.python import failure |
|
22 | 22 | from twisted.application import service |
|
23 | 23 | import zope.interface as zi |
|
24 | 24 | |
|
25 | 25 | from IPython.kernel import newserialized |
|
26 | 26 | from IPython.kernel import error |
|
27 | 27 | from IPython.kernel.pickleutil import can, uncan |
|
28 | 28 | import IPython.kernel.engineservice as es |
|
29 | 29 | from IPython.kernel.core.interpreter import Interpreter |
|
30 | 30 | from IPython.testing.parametric import Parametric, parametric |
|
31 | 31 | |
|
32 | 32 | #------------------------------------------------------------------------------- |
|
33 | 33 | # Tests |
|
34 | 34 | #------------------------------------------------------------------------------- |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | # A sequence of valid commands run through execute |
|
38 | 38 | validCommands = ['a=5', |
|
39 | 39 | 'b=10', |
|
40 | 40 | 'a=5; b=10; c=a+b', |
|
41 | 41 | 'import math; 2.0*math.pi', |
|
42 | 42 | """def f(): |
|
43 | 43 | result = 0.0 |
|
44 | 44 | for i in range(10): |
|
45 | 45 | result += i |
|
46 | 46 | """, |
|
47 | 47 | 'if 1<2: a=5', |
|
48 | 48 | """import time |
|
49 | 49 | time.sleep(0.1)""", |
|
50 | 50 | """from math import cos; |
|
51 | 51 | x = 1.0*cos(0.5)""", # Semicolons lead to Discard ast nodes that should be discarded |
|
52 | 52 | """s = 1 |
|
53 | 53 | s = set() |
|
54 | 54 | """, # Trailing whitespace should be allowed. |
|
55 | 55 | """import math |
|
56 | 56 | math.cos(1.0)""", # Test a method call with a discarded return value |
|
57 | 57 | """x=1.0234 |
|
58 | 58 | a=5; b=10""", # Test an embedded semicolon |
|
59 | 59 | """x=1.0234 |
|
60 | 60 | a=5; b=10;""" # Test both an embedded and trailing semicolon |
|
61 | 61 | ] |
|
62 | 62 | |
|
63 | 63 | # A sequence of commands that raise various exceptions |
|
64 | 64 | invalidCommands = [('a=1/0',ZeroDivisionError), |
|
65 | 65 | ('print v',NameError), |
|
66 | 66 | ('l=[];l[0]',IndexError), |
|
67 | 67 | ("d={};d['a']",KeyError), |
|
68 | 68 | ("assert 1==0",AssertionError), |
|
69 | 69 | ("import abababsdbfsbaljasdlja",ImportError), |
|
70 | 70 | ("raise Exception()",Exception)] |
|
71 | 71 | |
|
72 | 72 | def testf(x): |
|
73 | 73 | return 2.0*x |
|
74 | 74 | |
|
75 | 75 | globala = 99 |
|
76 | 76 | |
|
77 | 77 | def testg(x): |
|
78 | 78 | return globala*x |
|
79 | 79 | |
|
80 | 80 | class IEngineCoreTestCase(object): |
|
81 | 81 | """Test an IEngineCore implementer.""" |
|
82 | 82 | |
|
83 | 83 | def createShell(self): |
|
84 | 84 | return Interpreter() |
|
85 | 85 | |
|
86 | 86 | def catchQueueCleared(self, f): |
|
87 | 87 | try: |
|
88 | 88 | f.raiseException() |
|
89 | 89 | except error.QueueCleared: |
|
90 | 90 | pass |
|
91 | 91 | |
|
92 | 92 | def testIEngineCoreInterface(self): |
|
93 | 93 | """Does self.engine claim to implement IEngineCore?""" |
|
94 | 94 | self.assert_(es.IEngineCore.providedBy(self.engine)) |
|
95 | 95 | |
|
96 | 96 | def testIEngineCoreInterfaceMethods(self): |
|
97 | 97 | """Does self.engine have the methods and attributes in IEngineCore.""" |
|
98 | 98 | for m in list(es.IEngineCore): |
|
99 | 99 | self.assert_(hasattr(self.engine, m)) |
|
100 | 100 | |
|
101 | 101 | def testIEngineCoreDeferreds(self): |
|
102 | 102 | d = self.engine.execute('a=5') |
|
103 | 103 | d.addCallback(lambda _: self.engine.pull('a')) |
|
104 | 104 | d.addCallback(lambda _: self.engine.get_result()) |
|
105 | 105 | d.addCallback(lambda _: self.engine.keys()) |
|
106 | 106 | d.addCallback(lambda _: self.engine.push(dict(a=10))) |
|
107 | 107 | return d |
|
108 | 108 | |
|
109 | 109 | def runTestExecute(self, cmd): |
|
110 | 110 | self.shell = Interpreter() |
|
111 | 111 | actual = self.shell.execute(cmd) |
|
112 | 112 | def compare(computed): |
|
113 | 113 | actual['id'] = computed['id'] |
|
114 | 114 | self.assertEquals(actual, computed) |
|
115 | 115 | d = self.engine.execute(cmd) |
|
116 | 116 | d.addCallback(compare) |
|
117 | 117 | return d |
|
118 | 118 | |
|
119 | 119 | @parametric |
|
120 | 120 | def testExecute(cls): |
|
121 | 121 | return [(cls.runTestExecute, cmd) for cmd in validCommands] |
|
122 | 122 | |
|
123 | 123 | def runTestExecuteFailures(self, cmd, exc): |
|
124 | 124 | def compare(f): |
|
125 | 125 | self.assertRaises(exc, f.raiseException) |
|
126 | 126 | d = self.engine.execute(cmd) |
|
127 | 127 | d.addErrback(compare) |
|
128 | 128 | return d |
|
129 | 129 | |
|
130 | 130 | @parametric |
|
131 | 131 | def testExecuteFailuresEngineService(cls): |
|
132 | 132 | return [(cls.runTestExecuteFailures, cmd, exc) |
|
133 | 133 | for cmd, exc in invalidCommands] |
|
134 | 134 | |
|
135 | 135 | def runTestPushPull(self, o): |
|
136 | 136 | d = self.engine.push(dict(a=o)) |
|
137 | 137 | d.addCallback(lambda r: self.engine.pull('a')) |
|
138 | 138 | d.addCallback(lambda r: self.assertEquals(o,r)) |
|
139 | 139 | return d |
|
140 | 140 | |
|
141 | 141 | @parametric |
|
142 | 142 | def testPushPull(cls): |
|
143 | 143 | objs = [10,"hi there",1.2342354,{"p":(1,2)},None] |
|
144 | 144 | return [(cls.runTestPushPull, o) for o in objs] |
|
145 | 145 | |
|
146 | 146 | def testPullNameError(self): |
|
147 | 147 | d = self.engine.push(dict(a=5)) |
|
148 | 148 | d.addCallback(lambda _:self.engine.reset()) |
|
149 | 149 | d.addCallback(lambda _: self.engine.pull("a")) |
|
150 | 150 | d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException)) |
|
151 | 151 | return d |
|
152 | 152 | |
|
153 | 153 | def testPushPullFailures(self): |
|
154 | 154 | d = self.engine.pull('a') |
|
155 | 155 | d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException)) |
|
156 | 156 | d.addCallback(lambda _: self.engine.execute('l = lambda x: x')) |
|
157 | 157 | d.addCallback(lambda _: self.engine.pull('l')) |
|
158 | 158 | d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException)) |
|
159 | 159 | d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x))) |
|
160 | 160 | d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException)) |
|
161 | 161 | return d |
|
162 | 162 | |
|
163 | 163 | def testPushPullArray(self): |
|
164 | 164 | try: |
|
165 | 165 | import numpy |
|
166 | 166 | except: |
|
167 | 167 | return |
|
168 | 168 | a = numpy.random.random(1000) |
|
169 | 169 | d = self.engine.push(dict(a=a)) |
|
170 | 170 | d.addCallback(lambda _: self.engine.pull('a')) |
|
171 | 171 | d.addCallback(lambda b: b==a) |
|
172 | 172 | d.addCallback(lambda c: c.all()) |
|
173 | 173 | return self.assertDeferredEquals(d, True) |
|
174 | 174 | |
|
175 | 175 | def testPushFunction(self): |
|
176 | 176 | |
|
177 | 177 | d = self.engine.push_function(dict(f=testf)) |
|
178 | 178 | d.addCallback(lambda _: self.engine.execute('result = f(10)')) |
|
179 | 179 | d.addCallback(lambda _: self.engine.pull('result')) |
|
180 | 180 | d.addCallback(lambda r: self.assertEquals(r, testf(10))) |
|
181 | 181 | return d |
|
182 | 182 | |
|
183 | 183 | def testPullFunction(self): |
|
184 | 184 | d = self.engine.push_function(dict(f=testf, g=testg)) |
|
185 | 185 | d.addCallback(lambda _: self.engine.pull_function(('f','g'))) |
|
186 | 186 | d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10))) |
|
187 | 187 | return d |
|
188 | 188 | |
|
189 | 189 | def testPushFunctionGlobal(self): |
|
190 | 190 | """Make sure that pushed functions pick up the user's namespace for globals.""" |
|
191 | 191 | d = self.engine.push(dict(globala=globala)) |
|
192 | 192 | d.addCallback(lambda _: self.engine.push_function(dict(g=testg))) |
|
193 | 193 | d.addCallback(lambda _: self.engine.execute('result = g(10)')) |
|
194 | 194 | d.addCallback(lambda _: self.engine.pull('result')) |
|
195 | 195 | d.addCallback(lambda r: self.assertEquals(r, testg(10))) |
|
196 | 196 | return d |
|
197 | 197 | |
|
198 | 198 | def testGetResultFailure(self): |
|
199 | 199 | d = self.engine.get_result(None) |
|
200 | 200 | d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException)) |
|
201 | 201 | d.addCallback(lambda _: self.engine.get_result(10)) |
|
202 | 202 | d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException)) |
|
203 | 203 | return d |
|
204 | 204 | |
|
205 | 205 | def runTestGetResult(self, cmd): |
|
206 | 206 | self.shell = Interpreter() |
|
207 | 207 | actual = self.shell.execute(cmd) |
|
208 | 208 | def compare(computed): |
|
209 | 209 | actual['id'] = computed['id'] |
|
210 | 210 | self.assertEquals(actual, computed) |
|
211 | 211 | d = self.engine.execute(cmd) |
|
212 | 212 | d.addCallback(lambda r: self.engine.get_result(r['number'])) |
|
213 | 213 | d.addCallback(compare) |
|
214 | 214 | return d |
|
215 | 215 | |
|
216 | 216 | @parametric |
|
217 | 217 | def testGetResult(cls): |
|
218 | 218 | return [(cls.runTestGetResult, cmd) for cmd in validCommands] |
|
219 | 219 | |
|
220 | 220 | def testGetResultDefault(self): |
|
221 | 221 | cmd = 'a=5' |
|
222 | 222 | shell = self.createShell() |
|
223 | 223 | shellResult = shell.execute(cmd) |
|
224 | 224 | def popit(dikt, key): |
|
225 | 225 | dikt.pop(key) |
|
226 | 226 | return dikt |
|
227 | 227 | d = self.engine.execute(cmd) |
|
228 | 228 | d.addCallback(lambda _: self.engine.get_result()) |
|
229 | 229 | d.addCallback(lambda r: self.assertEquals(shellResult, popit(r,'id'))) |
|
230 | 230 | return d |
|
231 | 231 | |
|
232 | 232 | def testKeys(self): |
|
233 | 233 | d = self.engine.keys() |
|
234 | 234 | d.addCallback(lambda s: isinstance(s, list)) |
|
235 | 235 | d.addCallback(lambda r: self.assertEquals(r, True)) |
|
236 | 236 | return d |
|
237 | 237 | |
|
238 | 238 | Parametric(IEngineCoreTestCase) |
|
239 | 239 | |
|
240 | 240 | class IEngineSerializedTestCase(object): |
|
241 | 241 | """Test an IEngineCore implementer.""" |
|
242 | 242 | |
|
243 | 243 | def testIEngineSerializedInterface(self): |
|
244 | 244 | """Does self.engine claim to implement IEngineCore?""" |
|
245 | 245 | self.assert_(es.IEngineSerialized.providedBy(self.engine)) |
|
246 | 246 | |
|
247 | 247 | def testIEngineSerializedInterfaceMethods(self): |
|
248 | 248 | """Does self.engine have the methods and attributes in IEngineCore.""" |
|
249 | 249 | for m in list(es.IEngineSerialized): |
|
250 | 250 | self.assert_(hasattr(self.engine, m)) |
|
251 | 251 | |
|
252 | 252 | def testIEngineSerializedDeferreds(self): |
|
253 | 253 | dList = [] |
|
254 | 254 | d = self.engine.push_serialized(dict(key=newserialized.serialize(12345))) |
|
255 | 255 | self.assert_(isinstance(d, defer.Deferred)) |
|
256 | 256 | dList.append(d) |
|
257 | 257 | d = self.engine.pull_serialized('key') |
|
258 | 258 | self.assert_(isinstance(d, defer.Deferred)) |
|
259 | 259 | dList.append(d) |
|
260 | 260 | D = defer.DeferredList(dList) |
|
261 | 261 | return D |
|
262 | 262 | |
|
263 | 263 | def testPushPullSerialized(self): |
|
264 | 264 | objs = [10,"hi there",1.2342354,{"p":(1,2)}] |
|
265 | 265 | d = defer.succeed(None) |
|
266 | 266 | for o in objs: |
|
267 | 267 | self.engine.push_serialized(dict(key=newserialized.serialize(o))) |
|
268 | 268 | value = self.engine.pull_serialized('key') |
|
269 | 269 | value.addCallback(lambda serial: newserialized.IUnSerialized(serial).getObject()) |
|
270 | 270 | d = self.assertDeferredEquals(value,o,d) |
|
271 | 271 | return d |
|
272 | 272 | |
|
273 | 273 | def testPullSerializedFailures(self): |
|
274 | 274 | d = self.engine.pull_serialized('a') |
|
275 | 275 | d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException)) |
|
276 | 276 | d.addCallback(lambda _: self.engine.execute('l = lambda x: x')) |
|
277 | 277 | d.addCallback(lambda _: self.engine.pull_serialized('l')) |
|
278 | 278 | d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException)) |
|
279 | 279 | return d |
|
280 | 280 | |
|
281 | 281 | Parametric(IEngineSerializedTestCase) |
|
282 | 282 | |
|
283 | 283 | class IEngineQueuedTestCase(object): |
|
284 | 284 | """Test an IEngineQueued implementer.""" |
|
285 | 285 | |
|
286 | 286 | def testIEngineQueuedInterface(self): |
|
287 | 287 | """Does self.engine claim to implement IEngineQueued?""" |
|
288 | 288 | self.assert_(es.IEngineQueued.providedBy(self.engine)) |
|
289 | 289 | |
|
290 | 290 | def testIEngineQueuedInterfaceMethods(self): |
|
291 | 291 | """Does self.engine have the methods and attributes in IEngineQueued.""" |
|
292 | 292 | for m in list(es.IEngineQueued): |
|
293 | 293 | self.assert_(hasattr(self.engine, m)) |
|
294 | 294 | |
|
295 | 295 | def testIEngineQueuedDeferreds(self): |
|
296 | 296 | dList = [] |
|
297 | 297 | d = self.engine.clear_queue() |
|
298 | 298 | self.assert_(isinstance(d, defer.Deferred)) |
|
299 | 299 | dList.append(d) |
|
300 | 300 | d = self.engine.queue_status() |
|
301 | 301 | self.assert_(isinstance(d, defer.Deferred)) |
|
302 | 302 | dList.append(d) |
|
303 | 303 | D = defer.DeferredList(dList) |
|
304 | 304 | return D |
|
305 | 305 | |
|
306 | 306 | def testClearQueue(self): |
|
307 | 307 | result = self.engine.clear_queue() |
|
308 | 308 | d1 = self.assertDeferredEquals(result, None) |
|
309 | 309 | d1.addCallback(lambda _: self.engine.queue_status()) |
|
310 | 310 | d2 = self.assertDeferredEquals(d1, {'queue':[], 'pending':'None'}) |
|
311 | 311 | return d2 |
|
312 | 312 | |
|
313 | 313 | def testQueueStatus(self): |
|
314 | 314 | result = self.engine.queue_status() |
|
315 | 315 | result.addCallback(lambda r: 'queue' in r and 'pending' in r) |
|
316 | 316 | d = self.assertDeferredEquals(result, True) |
|
317 | 317 | return d |
|
318 | 318 | |
|
319 | 319 | Parametric(IEngineQueuedTestCase) |
|
320 | 320 | |
|
321 | 321 | class IEnginePropertiesTestCase(object): |
|
322 | 322 | """Test an IEngineProperties implementor.""" |
|
323 | 323 | |
|
324 | 324 | def testIEnginePropertiesInterface(self): |
|
325 | 325 | """Does self.engine claim to implement IEngineProperties?""" |
|
326 | 326 | self.assert_(es.IEngineProperties.providedBy(self.engine)) |
|
327 | 327 | |
|
328 | 328 | def testIEnginePropertiesInterfaceMethods(self): |
|
329 | 329 | """Does self.engine have the methods and attributes in IEngineProperties.""" |
|
330 | 330 | for m in list(es.IEngineProperties): |
|
331 | 331 | self.assert_(hasattr(self.engine, m)) |
|
332 | 332 | |
|
333 | 333 | def testGetSetProperties(self): |
|
334 | 334 | dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5)) |
|
335 | 335 | d = self.engine.set_properties(dikt) |
|
336 | 336 | d.addCallback(lambda r: self.engine.get_properties()) |
|
337 | 337 | d = self.assertDeferredEquals(d, dikt) |
|
338 | 338 | d.addCallback(lambda r: self.engine.get_properties(('c',))) |
|
339 | 339 | d = self.assertDeferredEquals(d, {'c': dikt['c']}) |
|
340 | 340 | d.addCallback(lambda r: self.engine.set_properties(dict(c=False))) |
|
341 | 341 | d.addCallback(lambda r: self.engine.get_properties(('c', 'd'))) |
|
342 | 342 | d = self.assertDeferredEquals(d, dict(c=False, d=None)) |
|
343 | 343 | return d |
|
344 | 344 | |
|
345 | 345 | def testClearProperties(self): |
|
346 | 346 | dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5)) |
|
347 | 347 | d = self.engine.set_properties(dikt) |
|
348 | 348 | d.addCallback(lambda r: self.engine.clear_properties()) |
|
349 | 349 | d.addCallback(lambda r: self.engine.get_properties()) |
|
350 | 350 | d = self.assertDeferredEquals(d, {}) |
|
351 | 351 | return d |
|
352 | 352 | |
|
353 | 353 | def testDelHasProperties(self): |
|
354 | 354 | dikt = dict(a=5, b='asdf', c=True, d=None, e=range(5)) |
|
355 | 355 | d = self.engine.set_properties(dikt) |
|
356 | 356 | d.addCallback(lambda r: self.engine.del_properties(('b','e'))) |
|
357 | 357 | d.addCallback(lambda r: self.engine.has_properties(('a','b','c','d','e'))) |
|
358 | 358 | d = self.assertDeferredEquals(d, [True, False, True, True, False]) |
|
359 | 359 | return d |
|
360 | 360 | |
|
361 | 361 | def testStrictDict(self): |
|
362 | s = """from IPython.kernel.engineservice import get_engine | |
|
363 | p = get_engine(%s).properties"""%self.engine.id | |
|
362 | s = """from IPython.kernel.engineservice import get_engine; p = get_engine(%s).properties"""%self.engine.id | |
|
364 | 363 | d = self.engine.execute(s) |
|
364 | # These 3 lines cause a weird testing error on some platforms (OS X). | |
|
365 | # I am leaving them here in case they are masking some really | |
|
366 | # weird reactor issue. For now I will just keep my eye on this. | |
|
365 | 367 | d.addCallback(lambda r: self.engine.execute("p['a'] = lambda _:None")) |
|
366 | 368 | d.addErrback(lambda f: self.assertRaises(error.InvalidProperty, |
|
367 | 369 | f.raiseException)) |
|
370 | # Below here seems to be fine | |
|
368 | 371 | d.addCallback(lambda r: self.engine.execute("p['a'] = range(5)")) |
|
369 | 372 | d.addCallback(lambda r: self.engine.execute("p['a'].append(5)")) |
|
370 | 373 | d.addCallback(lambda r: self.engine.get_properties('a')) |
|
371 | 374 | d = self.assertDeferredEquals(d, dict(a=range(5))) |
|
372 | 375 | return d |
|
373 | 376 | |
|
374 | 377 | Parametric(IEnginePropertiesTestCase) |
General Comments 0
You need to be logged in to leave comments.
Login now