blob: 29f7a71b155c8abc69cf58f2b14c476476c43924 [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 Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Victor Stinner45df8202010-04-28 22:31:17 +000010try:
11 import threading
12except ImportError:
13 threading = None
Tim Petersd66595f2001-02-04 03:09:53 +000014import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +000015
Benjamin Petersona54c9092009-01-13 02:11:23 +000016
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000017def testfunction(self):
18 """some doc"""
19 return self
20
21class InstanceMethod:
22 id = _testcapi.instancemethod(id)
23 testfunction = _testcapi.instancemethod(testfunction)
24
25class CAPITest(unittest.TestCase):
26
27 def test_instancemethod(self):
28 inst = InstanceMethod()
29 self.assertEqual(id(inst), inst.id())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000030 self.assertTrue(inst.testfunction() is inst)
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000031 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
32 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
33
34 InstanceMethod.testfunction.attribute = "test"
35 self.assertEqual(testfunction.attribute, "test")
36 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
37
38
Victor Stinner45df8202010-04-28 22:31:17 +000039@unittest.skipUnless(threading, 'Threading required for this test.')
Benjamin Petersona54c9092009-01-13 02:11:23 +000040class TestPendingCalls(unittest.TestCase):
41
42 def pendingcalls_submit(self, l, n):
43 def callback():
44 #this function can be interrupted by thread switching so let's
45 #use an atomic operation
46 l.append(None)
47
48 for i in range(n):
49 time.sleep(random.random()*0.02) #0.01 secs on average
50 #try submitting callback until successful.
51 #rely on regular interrupt to flush queue if we are
52 #unsuccessful.
53 while True:
54 if _testcapi._pending_threadfunc(callback):
55 break;
56
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000057 def pendingcalls_wait(self, l, n, context = None):
Benjamin Petersona54c9092009-01-13 02:11:23 +000058 #now, stick around until l[0] has grown to 10
59 count = 0;
60 while len(l) != n:
61 #this busy loop is where we expect to be interrupted to
62 #run our callbacks. Note that callbacks are only run on the
63 #main thread
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000064 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +000065 print("(%i)"%(len(l),),)
66 for i in range(1000):
67 a = i*i
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000068 if context and not context.event.is_set():
69 continue
Benjamin Petersona54c9092009-01-13 02:11:23 +000070 count += 1
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000071 self.assertTrue(count < 10000,
Benjamin Petersona54c9092009-01-13 02:11:23 +000072 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000073 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +000074 print("(%i)"%(len(l),))
75
76 def test_pendingcalls_threaded(self):
Benjamin Petersona54c9092009-01-13 02:11:23 +000077
78 #do every callback on a separate thread
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000079 n = 32 #total callbacks
Benjamin Petersona54c9092009-01-13 02:11:23 +000080 threads = []
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000081 class foo(object):pass
82 context = foo()
83 context.l = []
84 context.n = 2 #submits per thread
85 context.nThreads = n // context.n
86 context.nFinished = 0
87 context.lock = threading.Lock()
88 context.event = threading.Event()
89
90 for i in range(context.nThreads):
91 t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
Benjamin Petersona54c9092009-01-13 02:11:23 +000092 t.start()
93 threads.append(t)
94
Benjamin Petersone1cdfd72009-01-18 21:02:37 +000095 self.pendingcalls_wait(context.l, n, context)
Benjamin Petersona54c9092009-01-13 02:11:23 +000096
97 for t in threads:
98 t.join()
99
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000100 def pendingcalls_thread(self, context):
101 try:
102 self.pendingcalls_submit(context.l, context.n)
103 finally:
104 with context.lock:
105 context.nFinished += 1
106 nFinished = context.nFinished
107 if False and support.verbose:
108 print("finished threads: ", nFinished)
109 if nFinished == context.nThreads:
110 context.event.set()
111
Benjamin Petersona54c9092009-01-13 02:11:23 +0000112 def test_pendingcalls_non_threaded(self):
113 #again, just using the main thread, likely they will all be dispathced at
114 #once. It is ok to ask for too many, because we loop until we find a slot.
115 #the loop can be interrupted to dispatch.
116 #there are only 32 dispatch slots, so we go for twice that!
117 l = []
118 n = 64
119 self.pendingcalls_submit(l, n)
120 self.pendingcalls_wait(l, n)
121
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000122# Bug #6012
123class Test6012(unittest.TestCase):
124 def test(self):
125 self.assertEqual(_testcapi.argparsing("Hello", "World"), 1)
Benjamin Petersona54c9092009-01-13 02:11:23 +0000126
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127def test_main():
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000128 support.run_unittest(CAPITest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129
130 for name in dir(_testcapi):
131 if name.startswith('test_'):
132 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000133 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000134 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +0000135 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136
137 # some extra thread-state tests driven via _testcapi
138 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000139 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000140 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000141
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000143
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +0000145 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000146
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 _testcapi._test_thread_state(callback)
148 a = b = callback
149 time.sleep(1)
150 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +0000151 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000152 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +0000153 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000154
Victor Stinner45df8202010-04-28 22:31:17 +0000155 if threading:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000156 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +0000157 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 TestThreadState()
Georg Brandl2067bfd2008-05-25 13:05:15 +0000159 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 t.start()
161 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000162
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000163 support.run_unittest(TestPendingCalls, Test6012)
Benjamin Petersona54c9092009-01-13 02:11:23 +0000164
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +0000165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166if __name__ == "__main__":
167 test_main()