| 1 |
import py |
| 2 |
import pytest |
| 3 |
try: |
| 4 |
import _continuation |
| 5 |
except ImportError: |
| 6 |
py.test.skip("to run on top of a translated pypy-c") |
| 7 |
|
| 8 |
py.test.skip("convert to an apptest") |
| 9 |
|
| 10 |
import sys, random |
| 11 |
from rpython.tool.udir import udir |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
STATUS_MAX = 50000 |
| 16 |
CONTINULETS = 50 |
| 17 |
|
| 18 |
def set_fast_mode(): |
| 19 |
global STATUS_MAX, CONTINULETS |
| 20 |
STATUS_MAX = 100 |
| 21 |
CONTINULETS = 5 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class Done(Exception): |
| 26 |
pass |
| 27 |
|
| 28 |
|
| 29 |
class Runner(object): |
| 30 |
|
| 31 |
def __init__(self): |
| 32 |
self.foobar = 12345 |
| 33 |
self.conts = {} |
| 34 |
self.contlist = [] |
| 35 |
|
| 36 |
def run_test(self): |
| 37 |
self.start_continulets() |
| 38 |
self.n = 0 |
| 39 |
try: |
| 40 |
while True: |
| 41 |
self.do_switch(src=None) |
| 42 |
assert self.target is None |
| 43 |
except Done: |
| 44 |
self.check_traceback(sys.exc_info()[2]) |
| 45 |
|
| 46 |
def do_switch(self, src): |
| 47 |
assert src not in self.conts.values() |
| 48 |
c = random.choice(self.contlist) |
| 49 |
self.target = self.conts[c] |
| 50 |
self.conts[c] = src |
| 51 |
c.switch() |
| 52 |
assert self.target is src |
| 53 |
|
| 54 |
def run_continulet(self, c, i): |
| 55 |
while True: |
| 56 |
assert self.target is c |
| 57 |
assert self.contlist[i] is c |
| 58 |
self.do_switch(c) |
| 59 |
assert self.foobar == 12345 |
| 60 |
self.n += 1 |
| 61 |
if self.n >= STATUS_MAX: |
| 62 |
raise Done |
| 63 |
|
| 64 |
def start_continulets(self, i=0): |
| 65 |
c = _continuation.continulet(self.run_continulet, i) |
| 66 |
self.contlist.append(c) |
| 67 |
if i < CONTINULETS: |
| 68 |
self.start_continulets(i + 1) |
| 69 |
|
| 70 |
self.conts[c] = c |
| 71 |
|
| 72 |
def check_traceback(self, tb): |
| 73 |
found = [] |
| 74 |
tb = tb.tb_next |
| 75 |
while tb: |
| 76 |
if tb.tb_frame.f_code.co_name != 'do_switch': |
| 77 |
assert tb.tb_frame.f_code.co_name == 'run_continulet', ( |
| 78 |
"got %r" % (tb.tb_frame.f_code.co_name,)) |
| 79 |
found.append(tb.tb_frame.f_locals['c']) |
| 80 |
tb = tb.tb_next |
| 81 |
found.reverse() |
| 82 |
|
| 83 |
expected = [] |
| 84 |
c = self.target |
| 85 |
while c is not None: |
| 86 |
expected.append(c) |
| 87 |
c = self.conts[c] |
| 88 |
|
| 89 |
assert found == expected, "%r == %r" % (found, expected) |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
class AppTestWrapper: |
| 94 |
def setup_class(cls): |
| 95 |
"Run test_various_depths() when we are run with 'pypy py.test -A'." |
| 96 |
from pypy.conftest import option |
| 97 |
if not option.runappdirect: |
| 98 |
py.test.skip("meant only for -A run") |
| 99 |
cls.w_vmprof_file = cls.space.wrap(str(udir.join('profile.vmprof'))) |
| 100 |
|
| 101 |
def test_vmprof(self): |
| 102 |
""" |
| 103 |
The point of this test is to check that we do NOT segfault. In |
| 104 |
particular, we need to ensure that vmprof does not sample the stack in |
| 105 |
the middle of a switch, else we read nonsense. |
| 106 |
""" |
| 107 |
_vmprof = pytest.importorskip('_vmprof') |
| 108 |
def switch_forever(c): |
| 109 |
while True: |
| 110 |
c.switch() |
| 111 |
|
| 112 |
f = open(self.vmprof_file, 'w+b') |
| 113 |
_vmprof.enable(f.fileno(), 1/250.0, False, False, False, False) |
| 114 |
c = _continuation.continulet(switch_forever) |
| 115 |
for i in range(10**7): |
| 116 |
if i % 100000 == 0: |
| 117 |
print i |
| 118 |
c.switch() |
| 119 |
_vmprof.disable() |
| 120 |
f.close() |
| 121 |
|
| 122 |
def test_thread_switch_to_sub(self): |
| 123 |
try: |
| 124 |
import thread, time |
| 125 |
except ImportError: |
| 126 |
py.test.skip("no threads") |
| 127 |
c_list = [] |
| 128 |
lock = thread.allocate_lock() |
| 129 |
lock.acquire() |
| 130 |
lock2 = thread.allocate_lock() |
| 131 |
lock2.acquire() |
| 132 |
|
| 133 |
def fn(): |
| 134 |
c = _continuation.continulet(lambda c_main: c_main.switch()) |
| 135 |
c.switch() |
| 136 |
c_list.append(c) |
| 137 |
lock.release() |
| 138 |
lock2.acquire() |
| 139 |
|
| 140 |
thread.start_new_thread(fn, ()) |
| 141 |
lock.acquire() |
| 142 |
[c] = c_list |
| 143 |
py.test.raises(_continuation.error, c.switch) |
| 144 |
|
| 145 |
lock2.release() |
| 146 |
time.sleep(0.5) |
| 147 |
py.test.raises(_continuation.error, c.switch) |
| 148 |
|
| 149 |
def test_thread_switch_to_sub_nonstarted(self): |
| 150 |
try: |
| 151 |
import thread, time |
| 152 |
except ImportError: |
| 153 |
py.test.skip("no threads") |
| 154 |
c_list = [] |
| 155 |
lock = thread.allocate_lock() |
| 156 |
lock.acquire() |
| 157 |
lock2 = thread.allocate_lock() |
| 158 |
lock2.acquire() |
| 159 |
|
| 160 |
def fn(): |
| 161 |
c = _continuation.continulet(lambda c_main: None) |
| 162 |
c_list.append(c) |
| 163 |
lock.release() |
| 164 |
lock2.acquire() |
| 165 |
|
| 166 |
thread.start_new_thread(fn, ()) |
| 167 |
lock.acquire() |
| 168 |
[c] = c_list |
| 169 |
py.test.raises(_continuation.error, c.switch) |
| 170 |
|
| 171 |
lock2.release() |
| 172 |
time.sleep(0.5) |
| 173 |
py.test.raises(_continuation.error, c.switch) |
| 174 |
|
| 175 |
def test_thread_switch_to_main(self): |
| 176 |
try: |
| 177 |
import thread, time |
| 178 |
except ImportError: |
| 179 |
py.test.skip("no threads") |
| 180 |
c_list = [] |
| 181 |
lock = thread.allocate_lock() |
| 182 |
lock.acquire() |
| 183 |
lock2 = thread.allocate_lock() |
| 184 |
lock2.acquire() |
| 185 |
|
| 186 |
def fn(): |
| 187 |
def in_continulet(c_main): |
| 188 |
c_list.append(c_main) |
| 189 |
lock.release() |
| 190 |
lock2.acquire() |
| 191 |
c = _continuation.continulet(in_continulet) |
| 192 |
c.switch() |
| 193 |
|
| 194 |
thread.start_new_thread(fn, ()) |
| 195 |
lock.acquire() |
| 196 |
[c] = c_list |
| 197 |
py.test.raises(_continuation.error, c.switch) |
| 198 |
|
| 199 |
lock2.release() |
| 200 |
time.sleep(0.5) |
| 201 |
py.test.raises(_continuation.error, c.switch) |
| 202 |
|
| 203 |
def _setup(): |
| 204 |
for _i in range(20): |
| 205 |
def test_single_threaded(self): |
| 206 |
Runner().run_test() |
| 207 |
test_single_threaded.func_name = 'test_single_threaded_%d' % _i |
| 208 |
setattr(AppTestWrapper, test_single_threaded.func_name, |
| 209 |
test_single_threaded) |
| 210 |
for _i in range(5): |
| 211 |
def test_multi_threaded(self): |
| 212 |
multithreaded_test() |
| 213 |
test_multi_threaded.func_name = 'test_multi_threaded_%d' % _i |
| 214 |
setattr(AppTestWrapper, test_multi_threaded.func_name, |
| 215 |
test_multi_threaded) |
| 216 |
_setup() |
| 217 |
|
| 218 |
class ThreadTest(object): |
| 219 |
def __init__(self, lock): |
| 220 |
self.lock = lock |
| 221 |
self.ok = False |
| 222 |
lock.acquire() |
| 223 |
def run(self): |
| 224 |
try: |
| 225 |
Runner().run_test() |
| 226 |
self.ok = True |
| 227 |
finally: |
| 228 |
self.lock.release() |
| 229 |
|
| 230 |
def multithreaded_test(): |
| 231 |
try: |
| 232 |
import thread |
| 233 |
except ImportError: |
| 234 |
py.test.skip("no threads") |
| 235 |
ts = [ThreadTest(thread.allocate_lock()) for i in range(5)] |
| 236 |
for t in ts: |
| 237 |
thread.start_new_thread(t.run, ()) |
| 238 |
for t in ts: |
| 239 |
t.lock.acquire() |
| 240 |
for t in ts: |
| 241 |
assert t.ok |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
if __name__ == '__main__': |
| 246 |
Runner().run_test() |