1
# Run the _testcapi module tests (tests for the Python/C API): by defn,
2
# these are all functions _testcapi exports whose name begins with 'test_'.
4
from __future__ import with_statement
10
from test import support
14
def testfunction(self):
19
id = _testcapi.instancemethod(id)
20
testfunction = _testcapi.instancemethod(testfunction)
22
class CAPITest(unittest.TestCase):
24
def test_instancemethod(self):
25
inst = InstanceMethod()
26
self.assertEqual(id(inst), inst.id())
27
self.assert_(inst.testfunction() is inst)
28
self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
29
self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
31
InstanceMethod.testfunction.attribute = "test"
32
self.assertEqual(testfunction.attribute, "test")
33
self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
36
class TestPendingCalls(unittest.TestCase):
38
def pendingcalls_submit(self, l, n):
40
#this function can be interrupted by thread switching so let's
41
#use an atomic operation
45
time.sleep(random.random()*0.02) #0.01 secs on average
46
#try submitting callback until successful.
47
#rely on regular interrupt to flush queue if we are
50
if _testcapi._pending_threadfunc(callback):
53
def pendingcalls_wait(self, l, n, context = None):
54
#now, stick around until l[0] has grown to 10
57
#this busy loop is where we expect to be interrupted to
58
#run our callbacks. Note that callbacks are only run on the
60
if False and support.verbose:
61
print("(%i)"%(len(l),),)
64
if context and not context.event.is_set():
67
self.failUnless(count < 10000,
68
"timeout waiting for %i callbacks, got %i"%(n, len(l)))
69
if False and support.verbose:
70
print("(%i)"%(len(l),))
72
def test_pendingcalls_threaded(self):
74
#do every callback on a separate thread
75
n = 32 #total callbacks
77
class foo(object):pass
80
context.n = 2 #submits per thread
81
context.nThreads = n // context.n
83
context.lock = threading.Lock()
84
context.event = threading.Event()
86
for i in range(context.nThreads):
87
t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
91
self.pendingcalls_wait(context.l, n, context)
96
def pendingcalls_thread(self, context):
98
self.pendingcalls_submit(context.l, context.n)
101
context.nFinished += 1
102
nFinished = context.nFinished
103
if False and support.verbose:
104
print("finished threads: ", nFinished)
105
if nFinished == context.nThreads:
108
def test_pendingcalls_non_threaded(self):
109
#again, just using the main thread, likely they will all be dispathced at
110
#once. It is ok to ask for too many, because we loop until we find a slot.
111
#the loop can be interrupted to dispatch.
112
#there are only 32 dispatch slots, so we go for twice that!
115
self.pendingcalls_submit(l, n)
116
self.pendingcalls_wait(l, n)
120
support.run_unittest(CAPITest)
122
for name in dir(_testcapi):
123
if name.startswith('test_'):
124
test = getattr(_testcapi, name)
126
print("internal", name)
129
# some extra thread-state tests driven via _testcapi
130
def TestThreadState():
132
print("auto-thread-state")
137
idents.append(_thread.get_ident())
139
_testcapi._test_thread_state(callback)
142
# Check our main thread is in the list exactly 3 times.
143
if idents.count(_thread.get_ident()) != 3:
144
raise support.TestFailed(
145
"Couldn't find main thread correctly in the list")
148
_testcapi._test_thread_state
149
have_thread_state = True
150
except AttributeError:
151
have_thread_state = False
153
if have_thread_state:
158
t = threading.Thread(target=TestThreadState)
162
support.run_unittest(TestPendingCalls)
165
if __name__ == "__main__":