blob: 17c4bafd1713b861ac19c0d132f9c9b3e7b720ab [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
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +00005import time
6import random
7import unittest
8import threading
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Tim Petersd66595f2001-02-04 03:09:53 +000010import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +000011
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000012class TestPendingCalls(unittest.TestCase):
13
14 def pendingcalls_submit(self, l, n):
15 def callback():
16 #this function can be interrupted by thread switching so let's
17 #use an atomic operation
18 l.append(None)
19
20 for i in range(n):
21 time.sleep(random.random()*0.02) #0.01 secs on average
22 #try submitting callback until successful.
23 #rely on regular interrupt to flush queue if we are
24 #unsuccessful.
25 while True:
26 if _testcapi._pending_threadfunc(callback):
27 break;
28
29 def pendingcalls_wait(self, l, n):
30 #now, stick around until l[0] has grown to 10
31 count = 0;
32 while len(l) != n:
33 #this busy loop is where we expect to be interrupted to
34 #run our callbacks. Note that callbacks are only run on the
35 #main thread
36 if False and test_support.verbose:
37 print "(%i)"%(len(l),),
38 for i in xrange(1000):
39 a = i*i
40 count += 1
41 self.failUnless(count < 10000,
42 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
43 if False and test_support.verbose:
44 print "(%i)"%(len(l),)
45
46 def test_pendingcalls_threaded(self):
47 l = []
48
49 #do every callback on a separate thread
50 n = 32
51 threads = []
52 for i in range(n):
53 t = threading.Thread(target=self.pendingcalls_submit, args = (l, 1))
54 t.start()
55 threads.append(t)
56
57 self.pendingcalls_wait(l, n)
58
59 for t in threads:
60 t.join()
61
62 def test_pendingcalls_non_threaded(self):
63 #again, just using the main thread, likely they will all be dispathced at
64 #once. It is ok to ask for too many, because we loop until we find a slot.
65 #the loop can be interrupted to dispatch.
66 #there are only 32 dispatch slots, so we go for twice that!
67 l = []
68 n = 64
69 self.pendingcalls_submit(l, n)
70 self.pendingcalls_wait(l, n)
71
72
Tim Peters59b96c12006-03-21 03:58:41 +000073def test_main():
74
75 for name in dir(_testcapi):
76 if name.startswith('test_'):
77 test = getattr(_testcapi, name)
78 if test_support.verbose:
79 print "internal", name
80 try:
81 test()
82 except _testcapi.error:
83 raise test_support.TestFailed, sys.exc_info()[1]
84
85 # some extra thread-state tests driven via _testcapi
86 def TestThreadState():
Tim Peters9ea17ac2001-02-02 05:57:15 +000087 if test_support.verbose:
Tim Peters59b96c12006-03-21 03:58:41 +000088 print "auto-thread-state"
Mark Hammond8d98d2c2003-04-19 15:41:53 +000089
Tim Peters59b96c12006-03-21 03:58:41 +000090 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +000091
Tim Peters59b96c12006-03-21 03:58:41 +000092 def callback():
93 idents.append(thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +000094
Tim Peters59b96c12006-03-21 03:58:41 +000095 _testcapi._test_thread_state(callback)
96 a = b = callback
97 time.sleep(1)
98 # Check our main thread is in the list exactly 3 times.
99 if idents.count(thread.get_ident()) != 3:
100 raise test_support.TestFailed, \
101 "Couldn't find main thread correctly in the list"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000102
Tim Peters59b96c12006-03-21 03:58:41 +0000103 try:
104 _testcapi._test_thread_state
105 have_thread_state = True
106 except AttributeError:
107 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +0000108
Tim Peters59b96c12006-03-21 03:58:41 +0000109 if have_thread_state:
Amaury Forgeot d'Arc4b798bd2008-04-08 21:27:42 +0000110 import thread
111 import time
Tim Peters59b96c12006-03-21 03:58:41 +0000112 TestThreadState()
113 import threading
114 t=threading.Thread(target=TestThreadState)
115 t.start()
116 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000117
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000118 test_support.run_unittest(TestPendingCalls)
119
Tim Peters59b96c12006-03-21 03:58:41 +0000120if __name__ == "__main__":
121 test_main()