blob: 0edb3bf4836c12d4c821bb6117e76f37da6ed51c [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
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +00004from __future__ import with_statement
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00005import sys
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +00006import time
7import random
8import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Victor Stinnerbe595d32010-04-27 23:01:29 +000010try:
Ezio Melottief1db542013-02-23 06:33:51 +020011 import thread
Victor Stinnerbe595d32010-04-27 23:01:29 +000012 import threading
13except ImportError:
Ezio Melottief1db542013-02-23 06:33:51 +020014 thread = None
Victor Stinnerbe595d32010-04-27 23:01:29 +000015 threading = None
Tim Petersd66595f2001-02-04 03:09:53 +000016import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +000017
Victor Stinnerbe595d32010-04-27 23:01:29 +000018@unittest.skipUnless(threading, 'Threading required for this test.')
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000019class TestPendingCalls(unittest.TestCase):
20
21 def pendingcalls_submit(self, l, n):
22 def callback():
23 #this function can be interrupted by thread switching so let's
24 #use an atomic operation
25 l.append(None)
26
27 for i in range(n):
28 time.sleep(random.random()*0.02) #0.01 secs on average
29 #try submitting callback until successful.
30 #rely on regular interrupt to flush queue if we are
31 #unsuccessful.
32 while True:
33 if _testcapi._pending_threadfunc(callback):
34 break;
35
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000036 def pendingcalls_wait(self, l, n, context = None):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000037 #now, stick around until l[0] has grown to 10
38 count = 0;
39 while len(l) != n:
40 #this busy loop is where we expect to be interrupted to
41 #run our callbacks. Note that callbacks are only run on the
42 #main thread
43 if False and test_support.verbose:
44 print "(%i)"%(len(l),),
45 for i in xrange(1000):
46 a = i*i
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000047 if context and not context.event.is_set():
48 continue
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000049 count += 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +000050 self.assertTrue(count < 10000,
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000051 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
52 if False and test_support.verbose:
53 print "(%i)"%(len(l),)
54
55 def test_pendingcalls_threaded(self):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000056 #do every callback on a separate thread
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000057 n = 32 #total callbacks
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000058 threads = []
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000059 class foo(object):pass
60 context = foo()
61 context.l = []
62 context.n = 2 #submits per thread
Ezio Melottidde5b942010-02-03 05:37:26 +000063 context.nThreads = n // context.n
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000064 context.nFinished = 0
65 context.lock = threading.Lock()
66 context.event = threading.Event()
67
68 for i in range(context.nThreads):
69 t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000070 t.start()
71 threads.append(t)
72
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000073 self.pendingcalls_wait(context.l, n, context)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000074
75 for t in threads:
76 t.join()
77
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000078 def pendingcalls_thread(self, context):
79 try:
80 self.pendingcalls_submit(context.l, context.n)
81 finally:
82 with context.lock:
83 context.nFinished += 1
84 nFinished = context.nFinished
85 if False and test_support.verbose:
86 print "finished threads: ", nFinished
87 if nFinished == context.nThreads:
88 context.event.set()
89
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000090 def test_pendingcalls_non_threaded(self):
Ezio Melottic2077b02011-03-16 12:34:31 +020091 #again, just using the main thread, likely they will all be dispatched at
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000092 #once. It is ok to ask for too many, because we loop until we find a slot.
93 #the loop can be interrupted to dispatch.
94 #there are only 32 dispatch slots, so we go for twice that!
95 l = []
96 n = 64
97 self.pendingcalls_submit(l, n)
98 self.pendingcalls_wait(l, n)
99
100
Ezio Melottief1db542013-02-23 06:33:51 +0200101@unittest.skipUnless(threading and thread, 'Threading required for this test.')
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200102class TestThreadState(unittest.TestCase):
Tim Peters59b96c12006-03-21 03:58:41 +0000103
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200104 @test_support.reap_threads
105 def test_thread_state(self):
106 # some extra thread-state tests driven via _testcapi
107 def target():
108 idents = []
109
110 def callback():
111 idents.append(thread.get_ident())
112
113 _testcapi._test_thread_state(callback)
114 a = b = callback
115 time.sleep(1)
116 # Check our main thread is in the list exactly 3 times.
117 self.assertEqual(idents.count(thread.get_ident()), 3,
118 "Couldn't find main thread correctly in the list")
119
120 target()
121 t = threading.Thread(target=target)
122 t.start()
123 t.join()
124
125
126def test_main():
Tim Peters59b96c12006-03-21 03:58:41 +0000127 for name in dir(_testcapi):
128 if name.startswith('test_'):
129 test = getattr(_testcapi, name)
130 if test_support.verbose:
131 print "internal", name
132 try:
133 test()
134 except _testcapi.error:
135 raise test_support.TestFailed, sys.exc_info()[1]
136
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200137 test_support.run_unittest(TestPendingCalls, TestThreadState)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000138
Tim Peters59b96c12006-03-21 03:58:41 +0000139if __name__ == "__main__":
140 test_main()