| 1 |
import thread, time |
| 2 |
from pypy.module.thread.test.support import GenericTestThread |
| 3 |
import pytest |
| 4 |
|
| 5 |
class AppTestThread(GenericTestThread): |
| 6 |
|
| 7 |
|
| 8 |
def test_thread_error(self): |
| 9 |
import _thread |
| 10 |
assert _thread.error is RuntimeError |
| 11 |
|
| 12 |
def test_start_new_thread(self): |
| 13 |
import _thread |
| 14 |
feedback = [] |
| 15 |
please_start = [] |
| 16 |
def f(x, y, z): |
| 17 |
self.waitfor(lambda: please_start) |
| 18 |
feedback.append(42) |
| 19 |
_thread.start_new_thread(f, (1, 2), {'z': 3}) |
| 20 |
assert feedback == [] |
| 21 |
please_start.append(1) |
| 22 |
self.waitfor(lambda: feedback) |
| 23 |
assert feedback == [42] |
| 24 |
|
| 25 |
def test_thread_count(self): |
| 26 |
import _thread, time |
| 27 |
feedback = [] |
| 28 |
please_start = [] |
| 29 |
def f(): |
| 30 |
feedback.append(42) |
| 31 |
self.waitfor(lambda: please_start) |
| 32 |
assert _thread._count() == 0 |
| 33 |
_thread.start_new_thread(f, ()) |
| 34 |
self.waitfor(lambda: feedback) |
| 35 |
assert _thread._count() == 1 |
| 36 |
please_start.append(1) |
| 37 |
|
| 38 |
|
| 39 |
def test_start_new_thread_args(self): |
| 40 |
import _thread |
| 41 |
def f(): |
| 42 |
pass |
| 43 |
test_args = [ |
| 44 |
(f, [], {}), |
| 45 |
(f, (), []), |
| 46 |
("", (), {}), |
| 47 |
] |
| 48 |
for args in test_args: |
| 49 |
try: |
| 50 |
_thread.start_new_thread(*args) |
| 51 |
assert False |
| 52 |
except TypeError: |
| 53 |
pass |
| 54 |
|
| 55 |
def test_get_ident(self): |
| 56 |
import _thread |
| 57 |
ident = _thread.get_ident() |
| 58 |
feedback = [] |
| 59 |
def f(): |
| 60 |
feedback.append(_thread.get_ident()) |
| 61 |
ident2 = _thread.start_new_thread(f, ()) |
| 62 |
assert ident2 != ident |
| 63 |
assert ident == _thread.get_ident() |
| 64 |
self.waitfor(lambda: feedback) |
| 65 |
assert feedback[0] != ident |
| 66 |
|
| 67 |
def test_get_native_id(self): |
| 68 |
import _thread |
| 69 |
ident = _thread.get_native_id() |
| 70 |
feedback = [] |
| 71 |
def f(): |
| 72 |
feedback.append(_thread.get_native_id()) |
| 73 |
ident2 = _thread.start_new_thread(f, ()) |
| 74 |
assert ident2 != ident |
| 75 |
assert ident == _thread.get_native_id() |
| 76 |
self.waitfor(lambda: feedback) |
| 77 |
assert feedback[0] != ident |
| 78 |
|
| 79 |
def test_sys_getframe(self): |
| 80 |
|
| 81 |
def main(): |
| 82 |
import _thread, sys |
| 83 |
def dump_frames(feedback): |
| 84 |
f = sys._getframe() |
| 85 |
for i in range(3): |
| 86 |
if f is None: |
| 87 |
feedback.append(None) |
| 88 |
else: |
| 89 |
feedback.append(f.f_code.co_name) |
| 90 |
self.busywait(0.04) |
| 91 |
assert f is sys._getframe(i) |
| 92 |
f = f.f_back |
| 93 |
def dummyfn(feedback): |
| 94 |
dump_frames(feedback) |
| 95 |
feedback = [] |
| 96 |
dummyfn(feedback) |
| 97 |
assert feedback == ['dump_frames', 'dummyfn', 'main'] |
| 98 |
feedbacks = [] |
| 99 |
for i in range(3): |
| 100 |
feedback = [] |
| 101 |
_thread.start_new_thread(dummyfn, (feedback,)) |
| 102 |
feedbacks.append(feedback) |
| 103 |
expected = 3*[['dump_frames', 'dummyfn', None]] |
| 104 |
self.waitfor(lambda: feedbacks == expected) |
| 105 |
assert feedbacks == expected |
| 106 |
main() |
| 107 |
|
| 108 |
def test_thread_exit(self): |
| 109 |
import _thread, sys, io |
| 110 |
|
| 111 |
|
| 112 |
import traceback |
| 113 |
def fn1(): |
| 114 |
_thread.exit() |
| 115 |
def fn2(): |
| 116 |
raise SystemExit |
| 117 |
def fn3(): |
| 118 |
raise ValueError("hello world") |
| 119 |
prev = sys.stderr |
| 120 |
try: |
| 121 |
sys.stderr = io.StringIO() |
| 122 |
_thread.start_new_thread(fn1, ()) |
| 123 |
_thread.start_new_thread(fn2, ()) |
| 124 |
self.busywait(0.2) |
| 125 |
assert sys.stderr.getvalue() == '' |
| 126 |
|
| 127 |
sys.stderr = io.StringIO() |
| 128 |
_thread.start_new_thread(fn3, ()) |
| 129 |
self.waitfor(lambda: "ValueError" in sys.stderr.getvalue()) |
| 130 |
result = sys.stderr.getvalue().splitlines() |
| 131 |
|
| 132 |
assert result[1].startswith("Traceback ") |
| 133 |
assert result[-1] == "ValueError: hello world" |
| 134 |
finally: |
| 135 |
sys.stderr = prev |
| 136 |
|
| 137 |
def test_perthread_excinfo(self): |
| 138 |
import _thread, sys |
| 139 |
done = [] |
| 140 |
def fn1(n): |
| 141 |
success = False |
| 142 |
try: |
| 143 |
caught = False |
| 144 |
try: |
| 145 |
try: |
| 146 |
{}[n] |
| 147 |
except KeyError: |
| 148 |
self.busywait(0.05) |
| 149 |
caught = True |
| 150 |
raise |
| 151 |
except KeyError: |
| 152 |
self.busywait(0.05) |
| 153 |
assert caught |
| 154 |
etype, evalue, etb = sys.exc_info() |
| 155 |
assert etype is KeyError |
| 156 |
assert evalue.args[0] == n |
| 157 |
success = True |
| 158 |
finally: |
| 159 |
done.append(success) |
| 160 |
for i in range(20): |
| 161 |
_thread.start_new_thread(fn1, (i,)) |
| 162 |
self.waitfor(lambda: len(done) == 20) |
| 163 |
assert done == 20*[True] |
| 164 |
|
| 165 |
def test_no_corruption(self): |
| 166 |
import _thread |
| 167 |
lst = [] |
| 168 |
done_marker = [] |
| 169 |
def f(x, done): |
| 170 |
for j in range(40): |
| 171 |
lst.insert(0, x+j) |
| 172 |
done.append(True) |
| 173 |
for i in range(0, 120, 40): |
| 174 |
done = [] |
| 175 |
_thread.start_new_thread(f, (i, done)) |
| 176 |
done_marker.append(done) |
| 177 |
for done in done_marker: |
| 178 |
self.waitfor(lambda: done, delay=3) |
| 179 |
assert done |
| 180 |
assert sorted(lst) == list(range(120)) |
| 181 |
|
| 182 |
def test_stack_size(self): |
| 183 |
import _thread |
| 184 |
_thread.stack_size(0) |
| 185 |
res = _thread.stack_size(0) |
| 186 |
assert res == 0 |
| 187 |
res = _thread.stack_size(1024*1024) |
| 188 |
assert res == 0 |
| 189 |
res = _thread.stack_size(2*1024*1024) |
| 190 |
assert res == 1024*1024 |
| 191 |
res = _thread.stack_size(0) |
| 192 |
assert res == 2*1024*1024 |
| 193 |
|
| 194 |
def test_interrupt_main(self): |
| 195 |
import _thread, time |
| 196 |
import signal |
| 197 |
|
| 198 |
def f(): |
| 199 |
for x in range(40): |
| 200 |
if waiting: |
| 201 |
_thread.interrupt_main() |
| 202 |
return |
| 203 |
time.sleep(0.1) |
| 204 |
|
| 205 |
def busy_wait(): |
| 206 |
waiting.append(None) |
| 207 |
for x in range(50): |
| 208 |
time.sleep(0.1) |
| 209 |
waiting.pop() |
| 210 |
|
| 211 |
|
| 212 |
signal.signal(signal.SIGINT, signal.default_int_handler) |
| 213 |
|
| 214 |
for i in range(100): |
| 215 |
print() |
| 216 |
print("loop", i) |
| 217 |
waiting = [] |
| 218 |
_thread.start_new_thread(f, ()) |
| 219 |
raises(KeyboardInterrupt, busy_wait) |
| 220 |
|
| 221 |
def test_interrupt_main_wrong_arg(self): |
| 222 |
import _thread |
| 223 |
with raises(ValueError): |
| 224 |
_thread.interrupt_main(-1) |
| 225 |
|
| 226 |
def test_interrupt_non_main(self): |
| 227 |
import _thread as thread, time, sys |
| 228 |
if sys.platform == "win32": |
| 229 |
skip("hangs on windows") |
| 230 |
import __pypy__ |
| 231 |
import signal |
| 232 |
|
| 233 |
def f(): |
| 234 |
try: |
| 235 |
childstarted.append(thread.get_ident()) |
| 236 |
self.waitfor(lambda: False) |
| 237 |
except ValueError: |
| 238 |
childstarted.pop() |
| 239 |
else: |
| 240 |
thread.interrupt_main() |
| 241 |
|
| 242 |
for i in range(20): |
| 243 |
print(i) |
| 244 |
|
| 245 |
childstarted = [] |
| 246 |
thread.start_new_thread(f, ()) |
| 247 |
self.waitfor(lambda: bool(childstarted)) |
| 248 |
|
| 249 |
__pypy__.thread._raise_in_thread(childstarted[0], ValueError) |
| 250 |
|
| 251 |
|
| 252 |
def wait(): |
| 253 |
return not childstarted |
| 254 |
self.waitfor(wait) |
| 255 |
|
| 256 |
def test_interrupt_self(self): |
| 257 |
import _thread as thread, __pypy__ |
| 258 |
ident = thread.get_ident() |
| 259 |
for i in range(20): |
| 260 |
print(i) |
| 261 |
with raises(ValueError): |
| 262 |
__pypy__.thread._raise_in_thread(ident, ValueError) |
| 263 |
self.waitfor(lambda self: False) |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.skip("too slow") |
| 267 |
class _AppTestThread(GenericTestThread): |
| 268 |
''' |
| 269 |
This test is very slow, do not run it by default. |
| 270 |
''' |
| 271 |
def setup_class(cls): |
| 272 |
GenericTestThread.setup_class.im_func(cls) |
| 273 |
|
| 274 |
|
| 275 |
space = cls.space |
| 276 |
lock = thread.allocate_lock() |
| 277 |
lock.acquire() |
| 278 |
def f(): |
| 279 |
lock.acquire() |
| 280 |
lock.release() |
| 281 |
start = thread._count() |
| 282 |
try: |
| 283 |
try: |
| 284 |
for i in range(1000): |
| 285 |
thread.start_new_thread(f, ()) |
| 286 |
finally: |
| 287 |
lock.release() |
| 288 |
except (thread.error, MemoryError): |
| 289 |
cls.w_can_start_many_threads = space.wrap(False) |
| 290 |
else: |
| 291 |
cls.w_can_start_many_threads = space.wrap(True) |
| 292 |
|
| 293 |
remaining = thread._count() |
| 294 |
retries = 0 |
| 295 |
while remaining > start: |
| 296 |
retries += 1 |
| 297 |
if retries == 200: |
| 298 |
raise Exception("the test's threads don't stop!") |
| 299 |
time.sleep(0.2) |
| 300 |
remaining = thread._count() |
| 301 |
|
| 302 |
def test_many_threads(self): |
| 303 |
import time, sys |
| 304 |
if sys.version_info[0] < 3: |
| 305 |
import thread as _thread |
| 306 |
else: |
| 307 |
import _thread |
| 308 |
if self.can_start_many_threads or sys.platform == 'win32': |
| 309 |
skip("this OS supports too many threads to check (> 1000)") |
| 310 |
lock = _thread.allocate_lock() |
| 311 |
lock.acquire() |
| 312 |
count = [0] |
| 313 |
def f(): |
| 314 |
count[0] += 1 |
| 315 |
lock.acquire() |
| 316 |
lock.release() |
| 317 |
count[0] -= 1 |
| 318 |
try: |
| 319 |
try: |
| 320 |
for i in range(1000): |
| 321 |
_thread.start_new_thread(f, ()) |
| 322 |
finally: |
| 323 |
lock.release() |
| 324 |
|
| 325 |
while count[0] > 10: |
| 326 |
print(count[0]) |
| 327 |
print("ok.") |
| 328 |
except (_thread.error, MemoryError): |
| 329 |
pass |
| 330 |
else: |
| 331 |
raise Exception("could unexpectedly start 1000 threads") |
| 332 |
|
| 333 |
_thread.start_new_thread(lambda: None, ()) |
| 334 |
|
| 335 |
|