blob: bf8b8a8579374eac9e4d33bd3917fe9e57e61e24 [file] [log] [blame]
Tim Petersd66595f2001-02-04 03:09:53 +00001# Run the _testcapi module tests (tests for the Python/C API): by defn,
Guido van Rossum361c5352001-04-13 17:03:04 +00002# these are all functions _testcapi exports whose name begins with 'test_'.
Tim Peters9ea17ac2001-02-02 05:57:15 +00003
Benjamin Petersone1cdfd72009-01-18 21:02:37 +00004from __future__ import with_statement
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00005import sys
Benjamin Petersona54c9092009-01-13 02:11:23 +00006import time
7import random
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +00008import unittest
Benjamin Petersona54c9092009-01-13 02:11:23 +00009import threading
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
Tim Petersd66595f2001-02-04 03:09:53 +000011import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +000012
Benjamin Petersona54c9092009-01-13 02:11:23 +000013
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000014def testfunction(self):
15 """some doc"""
16 return self
17
18class InstanceMethod:
19 id = _testcapi.instancemethod(id)
20 testfunction = _testcapi.instancemethod(testfunction)
21
22class CAPITest(unittest.TestCase):
23
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__)
30
31 InstanceMethod.testfunction.attribute = "test"
32 self.assertEqual(testfunction.attribute, "test")
33 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
34
35
Benjamin Petersona54c9092009-01-13 02:11:23 +000036class TestPendingCalls(unittest.TestCase):
37
38 def pendingcalls_submit(self, l, n):
39 def callback():
40 #this function can be interrupted by thread switching so let's
41 #use an atomic operation
42 l.append(None)
43
44 for i in range(n):
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
48 #unsuccessful.
49 while True:
50 if _testcapi._pending_threadfunc(callback):
51 break;
52
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000053 def pendingcalls_wait(self, l, n, context = None):
Benjamin Petersona54c9092009-01-13 02:11:23 +000054 #now, stick around until l[0] has grown to 10
55 count = 0;
56 while len(l) != n:
57 #this busy loop is where we expect to be interrupted to
58 #run our callbacks. Note that callbacks are only run on the
59 #main thread
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000060 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +000061 print("(%i)"%(len(l),),)
62 for i in range(1000):
63 a = i*i
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000064 if context and not context.event.is_set():
65 continue
Benjamin Petersona54c9092009-01-13 02:11:23 +000066 count += 1
67 self.failUnless(count < 10000,
68 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000069 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +000070 print("(%i)"%(len(l),))
71
72 def test_pendingcalls_threaded(self):
Benjamin Petersona54c9092009-01-13 02:11:23 +000073
74 #do every callback on a separate thread
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000075 n = 32 #total callbacks
Benjamin Petersona54c9092009-01-13 02:11:23 +000076 threads = []
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000077 class foo(object):pass
78 context = foo()
79 context.l = []
80 context.n = 2 #submits per thread
81 context.nThreads = n // context.n
82 context.nFinished = 0
83 context.lock = threading.Lock()
84 context.event = threading.Event()
85
86 for i in range(context.nThreads):
87 t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
Benjamin Petersona54c9092009-01-13 02:11:23 +000088 t.start()
89 threads.append(t)
90
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000091 self.pendingcalls_wait(context.l, n, context)
Benjamin Petersona54c9092009-01-13 02:11:23 +000092
93 for t in threads:
94 t.join()
95
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000096 def pendingcalls_thread(self, context):
97 try:
98 self.pendingcalls_submit(context.l, context.n)
99 finally:
100 with context.lock:
101 context.nFinished += 1
102 nFinished = context.nFinished
103 if False and support.verbose:
104 print("finished threads: ", nFinished)
105 if nFinished == context.nThreads:
106 context.event.set()
107
Benjamin Petersona54c9092009-01-13 02:11:23 +0000108 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!
113 l = []
114 n = 64
115 self.pendingcalls_submit(l, n)
116 self.pendingcalls_wait(l, n)
117
118
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119def test_main():
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000120 support.run_unittest(CAPITest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121
122 for name in dir(_testcapi):
123 if name.startswith('test_'):
124 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000125 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000126 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +0000127 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128
129 # some extra thread-state tests driven via _testcapi
130 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000131 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000132 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000135
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +0000137 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000138
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 _testcapi._test_thread_state(callback)
140 a = b = callback
141 time.sleep(1)
142 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +0000143 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000144 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +0000145 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000146
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 try:
148 _testcapi._test_thread_state
149 have_thread_state = True
150 except AttributeError:
151 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +0000152
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153 if have_thread_state:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000154 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +0000155 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 TestThreadState()
157 import threading
Georg Brandl2067bfd2008-05-25 13:05:15 +0000158 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 t.start()
160 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000161
Benjamin Petersona54c9092009-01-13 02:11:23 +0000162 support.run_unittest(TestPendingCalls)
163
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000164
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165if __name__ == "__main__":
166 test_main()