| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import unittest |
| 9 |
from test.test_support import verbose, TestFailed, import_module |
| 10 |
thread = import_module('thread') |
| 11 |
|
| 12 |
critical_section = thread.allocate_lock() |
| 13 |
done = thread.allocate_lock() |
| 14 |
|
| 15 |
def task(): |
| 16 |
global N, critical_section, done |
| 17 |
import random |
| 18 |
x = random.randrange(1, 3) |
| 19 |
critical_section.acquire() |
| 20 |
N -= 1 |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
finished = N == 0 |
| 25 |
critical_section.release() |
| 26 |
if finished: |
| 27 |
done.release() |
| 28 |
|
| 29 |
def test_import_hangers(): |
| 30 |
import sys |
| 31 |
if verbose: |
| 32 |
print "testing import hangers ...", |
| 33 |
|
| 34 |
import test.threaded_import_hangers |
| 35 |
try: |
| 36 |
if test.threaded_import_hangers.errors: |
| 37 |
raise TestFailed(test.threaded_import_hangers.errors) |
| 38 |
elif verbose: |
| 39 |
print "OK." |
| 40 |
finally: |
| 41 |
|
| 42 |
|
| 43 |
del sys.modules['test.threaded_import_hangers'] |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def test_main(): |
| 55 |
global N, done |
| 56 |
|
| 57 |
import imp |
| 58 |
if imp.lock_held(): |
| 59 |
|
| 60 |
raise unittest.SkipTest("can't run when import lock is held") |
| 61 |
|
| 62 |
done.acquire() |
| 63 |
for N in (20, 50) * 3: |
| 64 |
if verbose: |
| 65 |
print "Trying", N, "threads ...", |
| 66 |
for i in range(N): |
| 67 |
thread.start_new_thread(task, ()) |
| 68 |
done.acquire() |
| 69 |
if verbose: |
| 70 |
print "OK." |
| 71 |
done.release() |
| 72 |
|
| 73 |
test_import_hangers() |
| 74 |
|
| 75 |
if __name__ == "__main__": |
| 76 |
test_main() |