blob: 4d37687639655cc18b6e62b92e9672a37806f046 [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
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00004import sys
Benjamin Petersona54c9092009-01-13 02:11:23 +00005import time
6import random
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +00007import unittest
Benjamin Petersona54c9092009-01-13 02:11:23 +00008import threading
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Tim Petersd66595f2001-02-04 03:09:53 +000010import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +000011
Benjamin Petersona54c9092009-01-13 02:11:23 +000012
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000013def testfunction(self):
14 """some doc"""
15 return self
16
17class InstanceMethod:
18 id = _testcapi.instancemethod(id)
19 testfunction = _testcapi.instancemethod(testfunction)
20
21class CAPITest(unittest.TestCase):
22
23 def test_instancemethod(self):
24 inst = InstanceMethod()
25 self.assertEqual(id(inst), inst.id())
26 self.assert_(inst.testfunction() is inst)
27 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
28 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
29
30 InstanceMethod.testfunction.attribute = "test"
31 self.assertEqual(testfunction.attribute, "test")
32 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
33
34
Benjamin Petersona54c9092009-01-13 02:11:23 +000035class TestPendingCalls(unittest.TestCase):
36
37 def pendingcalls_submit(self, l, n):
38 def callback():
39 #this function can be interrupted by thread switching so let's
40 #use an atomic operation
41 l.append(None)
42
43 for i in range(n):
44 time.sleep(random.random()*0.02) #0.01 secs on average
45 #try submitting callback until successful.
46 #rely on regular interrupt to flush queue if we are
47 #unsuccessful.
48 while True:
49 if _testcapi._pending_threadfunc(callback):
50 break;
51
52 def pendingcalls_wait(self, l, n):
53 #now, stick around until l[0] has grown to 10
54 count = 0;
55 while len(l) != n:
56 #this busy loop is where we expect to be interrupted to
57 #run our callbacks. Note that callbacks are only run on the
58 #main thread
59 if False and test_support.verbose:
60 print("(%i)"%(len(l),),)
61 for i in range(1000):
62 a = i*i
63 count += 1
64 self.failUnless(count < 10000,
65 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
66 if False and test_support.verbose:
67 print("(%i)"%(len(l),))
68
69 def test_pendingcalls_threaded(self):
70 l = []
71
72 #do every callback on a separate thread
73 n = 32
74 threads = []
75 for i in range(n):
76 t = threading.Thread(target=self.pendingcalls_submit, args = (l, 1))
77 t.start()
78 threads.append(t)
79
80 self.pendingcalls_wait(l, n)
81
82 for t in threads:
83 t.join()
84
85 def test_pendingcalls_non_threaded(self):
86 #again, just using the main thread, likely they will all be dispathced at
87 #once. It is ok to ask for too many, because we loop until we find a slot.
88 #the loop can be interrupted to dispatch.
89 #there are only 32 dispatch slots, so we go for twice that!
90 l = []
91 n = 64
92 self.pendingcalls_submit(l, n)
93 self.pendingcalls_wait(l, n)
94
95
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096def test_main():
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000097 support.run_unittest(CAPITest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098
99 for name in dir(_testcapi):
100 if name.startswith('test_'):
101 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000102 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000103 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +0000104 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105
106 # some extra thread-state tests driven via _testcapi
107 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000108 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000109 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000110
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000112
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +0000114 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 _testcapi._test_thread_state(callback)
117 a = b = callback
118 time.sleep(1)
119 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +0000120 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000121 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +0000122 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 try:
125 _testcapi._test_thread_state
126 have_thread_state = True
127 except AttributeError:
128 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +0000129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 if have_thread_state:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000131 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +0000132 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 TestThreadState()
134 import threading
Georg Brandl2067bfd2008-05-25 13:05:15 +0000135 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 t.start()
137 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000138
Benjamin Petersona54c9092009-01-13 02:11:23 +0000139 support.run_unittest(TestPendingCalls)
140
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000141
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142if __name__ == "__main__":
143 test_main()