Debian Codegraph @

pypy/module/thread/test/test_import_lock.py

raw symbols SCIP
1 from rpython.tool.udir import udir
2 from pypy.module.thread.test.support import GenericTestThread
3
4
5 class AppTestThread(GenericTestThread):
6
7 def setup_class(cls):
8 GenericTestThread.setup_class.im_func(cls)
9 tmpdir = str(udir.ensure('test_import_lock', dir=1))
10 cls.w_tmpdir = cls.space.wrap(tmpdir)
11
12 def test_import_lock(self):
13 # XXX XXX XXX this test fails if run together with all other tests
14 # of this directory, but not when run alone
15 import _thread, imp
16 assert not imp.lock_held()
17 done = []
18 def f(i):
19 print('[ENTER %d]' % i)
20 from imghdr import testall
21 print('[LEAVE %d]' % i)
22 done.append(1)
23 for i in range(5):
24 print('[RUN %d]' % i)
25 _thread.start_new_thread(f, (i,))
26 self.waitfor(lambda: len(done) == 5)
27 assert len(done) == 5
28
29 def test_with_many_dependencies(self):
30 import _thread
31 import re # -> causes nested imports
32
33 def test_manual_locking(self):
34 import _thread, os, imp, time, sys
35 f = open(os.path.join(self.tmpdir, 'foobaz2.py'), 'w')
36 f.close() # empty
37 done = []
38 def f():
39 sys.path.insert(0, self.tmpdir)
40 import foobaz2
41 p = sys.path.pop(0)
42 assert p == self.tmpdir
43 done.append(1)
44 assert not imp.lock_held()
45 imp.acquire_lock()
46 assert imp.lock_held()
47 _thread.start_new_thread(f, ())
48 time.sleep(0.9)
49 assert not done
50 assert imp.lock_held()
51 # check that it's a recursive lock
52 imp.acquire_lock()
53 assert imp.lock_held()
54 imp.acquire_lock()
55 assert imp.lock_held()
56 imp.release_lock()
57 assert imp.lock_held()
58 imp.release_lock()
59 assert imp.lock_held()
60 imp.release_lock()
61 assert not imp.lock_held()
62 self.waitfor(lambda: done)
63 assert done
64
65 def test_lock_held_by_another_thread(self):
66 import _thread as thread, imp
67 lock_held = thread.allocate_lock()
68 test_complete = thread.allocate_lock()
69 lock_released = thread.allocate_lock()
70 def other_thread():
71 imp.acquire_lock() # 3
72 assert imp.lock_held()
73 lock_held.release() # 4
74 test_complete.acquire() # 7
75 imp.release_lock() # 8
76 lock_released.release() # 9
77 lock_held.acquire()
78 test_complete.acquire()
79 lock_released.acquire()
80 #
81 thread.start_new_thread(other_thread, ()) # 1
82 lock_held.acquire() # 2
83 assert imp.lock_held() # 5
84 test_complete.release() # 6
85 lock_released.acquire() # 10
86
87 class TestImportLock:
88 def test_lock(self, space, monkeypatch):
89 from pypy.module.imp.importing import getimportlock, importhook
90
91 # Reset the import lock OS mutex in case a thread from a prior test
92 # is still holding it (the space is shared across tests via _SPACECACHE).
93 importlock = getimportlock(space)
94 importlock.lock = None
95 importlock.lockowner = None
96 importlock.lockcounter = 0
97 original_acquire = importlock.acquire_lock
98 def acquire_lock():
99 importlock.count += 1
100 original_acquire()
101 importlock.count = 0
102 monkeypatch.setattr(importlock, 'acquire_lock', acquire_lock)
103
104 # An already imported module
105 importhook(space, 'sys')
106 assert importlock.count == 0
107 # A new module
108 importhook(space, '_codecs')
109 assert importlock.count >= 1
110 # Import it again
111 previous_count = importlock.count
112 importhook(space, "_codecs")
113 assert importlock.count == previous_count