blob: d3d22263208a85461420dfd616c1b669c93929d0 [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
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000118# Bug #6012
119class Test6012(unittest.TestCase):
120 def test(self):
121 self.assertEqual(_testcapi.argparsing("Hello", "World"), 1)
Benjamin Petersona54c9092009-01-13 02:11:23 +0000122
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123def test_main():
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000124 support.run_unittest(CAPITest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125
126 for name in dir(_testcapi):
127 if name.startswith('test_'):
128 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000129 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +0000131 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132
133 # some extra thread-state tests driven via _testcapi
134 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000135 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000136 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000137
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000139
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +0000141 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000142
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 _testcapi._test_thread_state(callback)
144 a = b = callback
145 time.sleep(1)
146 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +0000147 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000148 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +0000149 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000150
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151 try:
152 _testcapi._test_thread_state
153 have_thread_state = True
154 except AttributeError:
155 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +0000156
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157 if have_thread_state:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000158 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +0000159 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 TestThreadState()
161 import threading
Georg Brandl2067bfd2008-05-25 13:05:15 +0000162 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 t.start()
164 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000165
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000166 support.run_unittest(TestPendingCalls, Test6012)
Benjamin Petersona54c9092009-01-13 02:11:23 +0000167
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000168
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169if __name__ == "__main__":
170 test_main()