Debian Codegraph @

pypy/module/test_lib_pypy/test_stackless.py

raw symbols SCIP
1 """
2 These tests are supposed to run on the following platforms:
3 1. CStackless
4 2. CPython (with the stackless_new module in the path
5 3. pypy-c
6 """
7 from py.test import skip
8 try:
9 import stackless
10 except ImportError:
11 try:
12 from lib_pypy import stackless
13 except ImportError as e:
14 skip('cannot import stackless: %s' % (e,))
15
16 SHOW_STRANGE = False
17
18 def dprint(txt):
19 if SHOW_STRANGE:
20 print(txt)
21
22 class Test_Stackless:
23
24 def test_simple(self):
25 rlist = []
26
27 def f():
28 rlist.append('f')
29
30 def g():
31 rlist.append('g')
32 stackless.schedule()
33
34 def main():
35 rlist.append('m')
36 cg = stackless.tasklet(g)()
37 cf = stackless.tasklet(f)()
38 stackless.run()
39 rlist.append('m')
40
41 main()
42
43 assert stackless.getcurrent() is stackless.getmain()
44 assert rlist == 'm g f m'.split()
45
46 def test_with_channel(self):
47 pref = {}
48 pref[-1] = ['s0', 'r0', 's1', 'r1', 's2', 'r2',
49 's3', 'r3', 's4', 'r4', 's5', 'r5',
50 's6', 'r6', 's7', 'r7', 's8', 'r8',
51 's9', 'r9']
52 pref[0] = ['s0', 'r0', 's1', 's2', 'r1', 'r2',
53 's3', 's4', 'r3', 'r4', 's5', 's6',
54 'r5', 'r6', 's7', 's8', 'r7', 'r8',
55 's9', 'r9']
56 pref[1] = ['s0', 's1', 'r0', 's2', 'r1', 's3',
57 'r2', 's4', 'r3', 's5', 'r4', 's6',
58 'r5', 's7', 'r6', 's8', 'r7', 's9',
59 'r8', 'r9']
60 rlist = []
61
62 def f(outchan):
63 for i in range(10):
64 rlist.append('s%s' % i)
65 outchan.send(i)
66 outchan.send(-1)
67
68 def g(inchan):
69 while 1:
70 val = inchan.receive()
71 if val == -1:
72 break
73 rlist.append('r%s' % val)
74
75 for preference in [-1, 0, 1]:
76 rlist = []
77 ch = stackless.channel()
78 ch.preference = preference
79 t1 = stackless.tasklet(f)(ch)
80 t2 = stackless.tasklet(g)(ch)
81
82 stackless.run()
83
84 assert len(rlist) == 20
85 assert rlist == pref[preference]
86
87 def test_send_counter(self):
88 import random
89
90 numbers = list(range(20))
91 random.shuffle(numbers)
92
93 def counter(n, ch):
94 for i in range(n):
95 stackless.schedule()
96 ch.send(n)
97
98 ch = stackless.channel()
99 for each in numbers:
100 stackless.tasklet(counter)(each, ch)
101
102 stackless.run()
103
104 rlist = []
105 while ch.balance:
106 rlist.append(ch.receive())
107
108 numbers.sort()
109 assert rlist == numbers
110
111 def test_receive_counter(self):
112 import random
113
114 numbers = list(range(20))
115 random.shuffle(numbers)
116
117 rlist = []
118 def counter(n, ch):
119 for i in range(n):
120 stackless.schedule()
121 ch.receive()
122 rlist.append(n)
123
124 ch = stackless.channel()
125 for each in numbers:
126 stackless.tasklet(counter)(each, ch)
127
128 stackless.run()
129
130 while ch.balance:
131 ch.send(None)
132
133 numbers.sort()
134 assert rlist == numbers
135
136 def test_scheduling_cleanup(self):
137 rlist = []
138 def f():
139 rlist.append('fb')
140 stackless.schedule()
141 rlist.append('fa')
142
143 def g():
144 rlist.append('gb')
145 stackless.schedule()
146 rlist.append('ga')
147
148 def h():
149 rlist.append('hb')
150 stackless.schedule()
151 rlist.append('ha')
152
153 tf = stackless.tasklet(f)()
154 tg = stackless.tasklet(g)()
155 th = stackless.tasklet(h)()
156
157 rlist.append('mb')
158 stackless.run()
159 rlist.append('ma')
160
161 assert rlist == 'mb fb gb hb fa ga ha ma'.split()
162
163 def test_except(self):
164 rlist = []
165 def f():
166 rlist.append('f')
167 return 1/0
168
169 def g():
170 rlist.append('bg')
171 stackless.schedule()
172 rlist.append('ag')
173
174 def h():
175 rlist.append('bh')
176 stackless.schedule()
177 rlist.append('ah')
178
179 tg = stackless.tasklet(g)()
180 tf = stackless.tasklet(f)()
181 th = stackless.tasklet(h)()
182
183 try:
184 stackless.run()
185 # cheating, can't test for ZeroDivisionError
186 except Exception as e:
187 rlist.append('E')
188 stackless.schedule()
189 stackless.schedule()
190
191 assert rlist == "bg f E bh ag ah".split()
192
193 def test_except_full(self):
194 rlist = []
195 def f():
196 rlist.append('f')
197 return 1/0
198
199 def g():
200 rlist.append('bg')
201 stackless.schedule()
202 rlist.append('ag')
203
204 def h():
205 rlist.append('bh')
206 stackless.schedule()
207 rlist.append('ah')
208
209 tg = stackless.tasklet(g)()
210 tf = stackless.tasklet(f)()
211 th = stackless.tasklet(h)()
212
213 try:
214 stackless.run()
215 except ZeroDivisionError:
216 rlist.append('E')
217 stackless.schedule()
218 stackless.schedule()
219
220 assert rlist == "bg f E bh ag ah".split()
221
222 def test_kill(self):
223 def f():pass
224 t = stackless.tasklet(f)()
225 t.kill()
226 assert not t.alive
227
228 def test_catch_taskletexit(self):
229 # Tests if TaskletExit can be caught in the tasklet being killed.
230 global taskletexit
231 taskletexit = False
232
233 def f():
234 try:
235 stackless.schedule()
236 except TaskletExit:
237 global TaskletExit
238 taskletexit = True
239 raise
240
241 t = stackless.tasklet(f)()
242 t.run()
243 assert t.alive
244 t.kill()
245 assert not t.alive
246 assert taskletexit
247
248 def test_autocatch_taskletexit(self):
249 # Tests if TaskletExit is caught correctly in stackless.tasklet.setup().
250 def f():
251 stackless.schedule()
252
253 t = stackless.tasklet(f)()
254 t.run()
255 t.kill()
256
257
258 # tests inspired from simple stackless.com examples
259
260 def test_construction(self):
261 output = []
262 def print_(*args):
263 output.append(args)
264
265 def aCallable(value):
266 print_("aCallable:", value)
267
268 task = stackless.tasklet(aCallable)
269 task.setup('Inline using setup')
270
271 stackless.run()
272 assert output == [("aCallable:", 'Inline using setup')]
273
274
275 del output[:]
276 task = stackless.tasklet(aCallable)
277 task('Inline using ()')
278
279 stackless.run()
280 assert output == [("aCallable:", 'Inline using ()')]
281
282 del output[:]
283 task = stackless.tasklet()
284 task.bind(aCallable)
285 task('Bind using ()')
286
287 stackless.run()
288 assert output == [("aCallable:", 'Bind using ()')]
289
290 def test_simple_channel(self):
291 output = []
292 def print_(*args):
293 output.append(args)
294
295 def Sending(channel):
296 print_("sending")
297 channel.send("foo")
298
299 def Receiving(channel):
300 print_("receiving")
301 print_(channel.receive())
302
303 ch=stackless.channel()
304
305 task=stackless.tasklet(Sending)(ch)
306
307 # Note: the argument, schedule is taking is the value,
308 # schedule returns, not the task that runs next
309
310 #stackless.schedule(task)
311 stackless.schedule()
312 task2=stackless.tasklet(Receiving)(ch)
313 #stackless.schedule(task2)
314 stackless.schedule()
315
316 stackless.run()
317
318 assert output == [('sending',), ('receiving',), ('foo',)]
319
320 def test_balance_zero(self):
321 ch=stackless.channel()
322 assert ch.balance == 0
323
324 def test_balance_send(self):
325 def Sending(channel):
326 channel.send("foo")
327
328 ch=stackless.channel()
329
330 task=stackless.tasklet(Sending)(ch)
331 stackless.run()
332
333 assert ch.balance == 1
334
335 def test_balance_recv(self):
336 def Receiving(channel):
337 channel.receive()
338
339 ch=stackless.channel()
340
341 task=stackless.tasklet(Receiving)(ch)
342 stackless.run()
343
344 assert ch.balance == -1
345
346 def test_run(self):
347 output = []
348 def print_(*args):
349 output.append(args)
350
351 def f(i):
352 print_(i)
353
354 stackless.tasklet(f)(1)
355 stackless.tasklet(f)(2)
356 stackless.run()
357
358 assert output == [(1,), (2,)]
359
360 def test_schedule(self):
361 output = []
362 def print_(*args):
363 output.append(args)
364
365 def f(i):
366 print_(i)
367
368 stackless.tasklet(f)(1)
369 stackless.tasklet(f)(2)
370 stackless.schedule()
371
372 assert output == [(1,), (2,)]
373
374
375 def test_cooperative(self):
376 output = []
377 def print_(*args):
378 output.append(args)
379
380 def Loop(i):
381 for x in range(3):
382 stackless.schedule()
383 print_("schedule", i)
384
385 stackless.tasklet(Loop)(1)
386 stackless.tasklet(Loop)(2)
387 stackless.run()
388
389 assert output == [('schedule', 1), ('schedule', 2),
390 ('schedule', 1), ('schedule', 2),
391 ('schedule', 1), ('schedule', 2),]
392
393 def test_channel_callback(self):
394 res = []
395 cb = []
396 def callback_function(chan, task, sending, willblock):
397 cb.append((chan, task, sending, willblock))
398 stackless.set_channel_callback(callback_function)
399 def f(chan):
400 chan.send('hello')
401 val = chan.receive()
402 res.append(val)
403
404 chan = stackless.channel()
405 task = stackless.tasklet(f)(chan)
406 val = chan.receive()
407 res.append(val)
408 chan.send('world')
409 assert res == ['hello','world']
410 maintask = stackless.getmain()
411 assert cb == [
412 (chan, maintask, 0, 1),
413 (chan, task, 1, 0),
414 (chan, maintask, 1, 1),
415 (chan, task, 0, 0)
416 ]
417
418 def test_schedule_callback(self):
419 res = []
420 cb = []
421 def schedule_cb(prev, next):
422 cb.append((prev, next))
423
424 stackless.set_schedule_callback(schedule_cb)
425 def f(i):
426 res.append('A_%s' % i)
427 stackless.schedule()
428 res.append('B_%s' % i)
429
430 t1 = stackless.tasklet(f)(1)
431 t2 = stackless.tasklet(f)(2)
432 maintask = stackless.getmain()
433 stackless.run()
434 assert res == ['A_1', 'A_2', 'B_1', 'B_2']
435 assert len(cb) == 5
436 assert cb[0] == (maintask, t1)
437 assert cb[1] == (t1, t2)
438 assert cb[2] == (t2, t1)
439 assert cb[3] == (t1, t2)
440 assert cb[4] == (t2, maintask)
441
442 def test_bomb(self):
443 try:
444 1/0
445 except:
446 import sys
447 b = stackless.bomb(*sys.exc_info())
448 assert b.type is ZeroDivisionError
449 assert str(b.value).startswith('integer division')
450 assert b.traceback is not None
451
452 def test_send_exception(self):
453 def exp_sender(chan):
454 chan.send_exception(Exception, 'test')
455
456 def exp_recv(chan):
457 try:
458 val = chan.receive()
459 except Exception as exp:
460 assert exp.__class__ is Exception
461 assert str(exp) == 'test'
462
463 chan = stackless.channel()
464 t1 = stackless.tasklet(exp_recv)(chan)
465 t2 = stackless.tasklet(exp_sender)(chan)
466 stackless.run()
467
468 def test_send_sequence(self):
469 res = []
470 lst = [1,2,3,4,5,6,None]
471 iterable = iter(lst)
472 chan = stackless.channel()
473 def f(chan):
474 r = chan.receive()
475 while r:
476 res.append(r)
477 r = chan.receive()
478
479 t = stackless.tasklet(f)(chan)
480 chan.send_sequence(iterable)
481 assert res == [1,2,3,4,5,6]
482
483 def test_getruncount(self):
484 assert stackless.getruncount() == 1
485 def with_schedule():
486 assert stackless.getruncount() == 2
487
488 t1 = stackless.tasklet(with_schedule)()
489 assert stackless.getruncount() == 2
490 stackless.schedule()
491 def with_run():
492 assert stackless.getruncount() == 1
493
494 t2 = stackless.tasklet(with_run)()
495 stackless.run()
496
497 def test_schedule_return(self):
498 def f():pass
499 t1= stackless.tasklet(f)()
500 r = stackless.schedule()
501 assert r is stackless.getmain()
502 t2 = stackless.tasklet(f)()
503 r = stackless.schedule('test')
504 assert r == 'test'
505
506 def test_simple_pipe(self):
507 def pipe(X_in, X_out):
508 foo = X_in.receive()
509 X_out.send(foo)
510
511 X, Y = stackless.channel(), stackless.channel()
512 t = stackless.tasklet(pipe)(X, Y)
513 stackless.run()
514 X.send(42)
515 assert Y.receive() == 42
516
517 def test_nested_pipe(self):
518 dprint('tnp ==== 1')
519 def pipe(X, Y):
520 dprint('tnp_P ==== 1')
521 foo = X.receive()
522 dprint('tnp_P ==== 2')
523 Y.send(foo)
524 dprint('tnp_P ==== 3')
525
526 def nest(X, Y):
527 X2, Y2 = stackless.channel(), stackless.channel()
528 t = stackless.tasklet(pipe)(X2, Y2)
529 dprint('tnp_N ==== 1')
530 X_Val = X.receive()
531 dprint('tnp_N ==== 2')
532 X2.send(X_Val)
533 dprint('tnp_N ==== 3')
534 Y2_Val = Y2.receive()
535 dprint('tnp_N ==== 4')
536 Y.send(Y2_Val)
537 dprint('tnp_N ==== 5')
538
539 X, Y = stackless.channel(), stackless.channel()
540 t1 = stackless.tasklet(nest)(X, Y)
541 X.send(13)
542 dprint('tnp ==== 2')
543 res = Y.receive()
544 dprint('tnp ==== 3')
545 assert res == 13
546 if SHOW_STRANGE:
547 raise Exception('force prints')
548
549 def test_wait_two(self):
550 """
551 A tasklets/channels adaptation of the test_wait_two from the
552 logic object space
553 """
554 def sleep(X, Y):
555 dprint('twt_S ==== 1')
556 value = X.receive()
557 dprint('twt_S ==== 2')
558 Y.send((X, value))
559 dprint('twt_S ==== 3')
560
561 def wait_two(X, Y, Ret_chan):
562 Barrier = stackless.channel()
563 stackless.tasklet(sleep)(X, Barrier)
564 stackless.tasklet(sleep)(Y, Barrier)
565 dprint('twt_W ==== 1')
566 ret = Barrier.receive()
567 dprint('twt_W ==== 2')
568 if ret[0] == X:
569 Ret_chan.send((1, ret[1]))
570 else:
571 Ret_chan.send((2, ret[1]))
572 dprint('twt_W ==== 3')
573
574 X = stackless.channel()
575 Y = stackless.channel()
576 Ret_chan = stackless.channel()
577
578 stackless.tasklet(wait_two)(X, Y, Ret_chan)
579
580 dprint('twt ==== 1')
581 Y.send(42)
582
583 dprint('twt ==== 2')
584 X.send(42)
585 dprint('twt ==== 3')
586 value = Ret_chan.receive()
587 dprint('twt ==== 4')
588 assert value == (2, 42)
589
590
591 def test_schedule_return_value(self):
592
593 def task(val):
594 value = stackless.schedule(val)
595 assert value == val
596
597 stackless.tasklet(task)(10)
598 stackless.tasklet(task)(5)
599
600 stackless.run()
601
602 def test_kill_tasklet_waiting_for_channel(self):
603 # issue #2595
604 c = stackless.channel()
605 def sender():
606 c.send(1)
607 def receiver():
608 v = c.receive()
609 def killer(tl):
610 tl.kill()
611 def main():
612 trk = stackless.tasklet(receiver)()
613 stackless.schedule()
614 killer(trk)
615 stackless.schedule()
616 stackless.tasklet(sender)()
617 stackless.schedule()
618 stackless.tasklet(receiver)()
619 stackless.schedule()
620 stackless.tasklet(main)()
621 stackless.run()