Debian Codegraph @

pypy/module/test_lib_pypy/test_coroutine.py

raw symbols SCIP
1 from py.test import skip, raises
2
3 skip('test needs to be updated')
4
5 try:
6 from stackless import coroutine, CoroutineExit
7 except ImportError as e:
8 skip('cannot import stackless: %s' % (e,))
9
10
11 class Test_Coroutine:
12
13 def test_is_zombie(self):
14 co = coroutine()
15 def f():
16 print('in coro')
17 assert not co.is_zombie
18 co.bind(f)
19 assert not co.is_zombie
20 co.switch()
21 assert not co.is_zombie
22
23 def test_is_zombie_del_without_frame(self):
24 import gc
25 res = []
26 class MyCoroutine(coroutine):
27 def __del__(self):
28 res.append(self.is_zombie)
29 def f():
30 print('in coro')
31 co = MyCoroutine()
32 co.bind(f)
33 co.switch()
34 del co
35 for i in range(10):
36 gc.collect()
37 if res:
38 break
39 co = coroutine()
40 co.bind(f)
41 co.switch()
42 assert res[0], "is_zombie was False in __del__"
43
44 def test_is_zombie_del_with_frame(self):
45 import gc
46 res = []
47 class MyCoroutine(coroutine):
48 def __del__(self):
49 res.append(self.is_zombie)
50 main = coroutine.getcurrent()
51 def f():
52 print('in coro')
53 main.switch()
54 co = MyCoroutine()
55 co.bind(f)
56 co.switch()
57 del co
58 for i in range(10):
59 gc.collect()
60 if res:
61 break
62 co = coroutine()
63 co.bind(f)
64 co.switch()
65 assert res[0], "is_zombie was False in __del__"
66
67 def test_raise_propagate(self):
68 co = coroutine()
69 def f():
70 return 1/0
71 co.bind(f)
72 try:
73 co.switch()
74 except ZeroDivisionError:
75 pass
76 else:
77 raise AssertionError("exception not propagated")
78
79 def test_strange_test(self):
80 def f():
81 return 42
82 def create():
83 b = coroutine()
84 b.bind(f)
85 b.switch()
86 return b
87 a = coroutine()
88 a.bind(create)
89 b = a.switch()
90 def nothing():
91 pass
92 a.bind(nothing)
93 def kill():
94 a.kill()
95 b.bind(kill)
96 b.switch()
97
98 def test_kill(self):
99 co = coroutine()
100 def f():
101 pass
102 assert not co.is_alive
103 co.bind(f)
104 assert co.is_alive
105 co.kill()
106 assert not co.is_alive
107
108 def test_catch_coroutineexit(self):
109 coroutineexit = []
110 co_a = coroutine()
111 co_test = coroutine.getcurrent()
112
113 def a():
114 try:
115 co_test.switch()
116 except CoroutineExit:
117 coroutineexit.append(True)
118 raise
119
120 co_a.bind(a)
121 co_a.switch()
122 assert co_a.is_alive
123
124 co_a.kill()
125 assert coroutineexit == [True]
126 assert not co_a.is_alive
127
128 def test_throw(self):
129 exceptions = []
130 co = coroutine()
131 def f(main):
132 try:
133 main.switch()
134 except RuntimeError:
135 exceptions.append(True)
136
137 co.bind(f, coroutine.getcurrent())
138 co.switch()
139 co.throw(RuntimeError)
140 assert exceptions == [True]
141
142 def test_propagation(self):
143 exceptions = []
144 co = coroutine()
145 co2 = coroutine()
146 def f(main):
147 main.switch()
148
149 co.bind(f, coroutine.getcurrent())
150 co.switch()
151
152 try:
153 co.throw(RuntimeError)
154 except RuntimeError:
155 exceptions.append(1)
156
157 def f2():
158 raise RuntimeError
159
160 co2.bind(f2)
161
162 try:
163 co2.switch()
164 except RuntimeError:
165 exceptions.append(2)
166
167 assert exceptions == [1,2]
168
169 def test_bogus_bind(self):
170 co = coroutine()
171 def f():
172 pass
173 co.bind(f)
174 raises(ValueError, co.bind, f)
175
176 def test_simple_task(self):
177 maintask = coroutine.getcurrent()
178 def f():pass
179 co = coroutine()
180 co.bind(f)
181 co.switch()
182 assert not co.is_alive
183 assert maintask is coroutine.getcurrent()
184
185 def test_backto_main(self):
186 maintask = coroutine.getcurrent()
187 def f(task):
188 task.switch()
189 co = coroutine()
190 co.bind(f,maintask)
191 co.switch()
192
193 def test_wrapped_main(self):
194 class mwrap(object):
195 def __init__(self, coro):
196 self._coro = coro
197
198 def __getattr__(self, attr):
199 return getattr(self._coro, attr)
200
201 maintask = mwrap(coroutine.getcurrent())
202 def f(task):
203 task.switch()
204 co = coroutine()
205 co.bind(f,maintask)
206 co.switch()
207