blob: 944e9603cc5b67ec958ae9e39bb16a23ab122dcd [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
Serhiy Storchakabd8c6292015-04-01 12:56:39 +03009from test import test_support as 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
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020016# Skip this test if the _testcapi module isn't available.
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030017_testcapi = support.import_module('_testcapi')
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020018
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +030019class CAPITest(unittest.TestCase):
20
21 def test_buildvalue_N(self):
22 _testcapi.test_buildvalue_N()
23
Tim Peters9ea17ac2001-02-02 05:57:15 +000024
Victor Stinnerbe595d32010-04-27 23:01:29 +000025@unittest.skipUnless(threading, 'Threading required for this test.')
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000026class TestPendingCalls(unittest.TestCase):
27
28 def pendingcalls_submit(self, l, n):
29 def callback():
30 #this function can be interrupted by thread switching so let's
31 #use an atomic operation
32 l.append(None)
33
34 for i in range(n):
35 time.sleep(random.random()*0.02) #0.01 secs on average
36 #try submitting callback until successful.
37 #rely on regular interrupt to flush queue if we are
38 #unsuccessful.
39 while True:
40 if _testcapi._pending_threadfunc(callback):
41 break;
42
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000043 def pendingcalls_wait(self, l, n, context = None):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000044 #now, stick around until l[0] has grown to 10
45 count = 0;
46 while len(l) != n:
47 #this busy loop is where we expect to be interrupted to
48 #run our callbacks. Note that callbacks are only run on the
49 #main thread
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030050 if False and support.verbose:
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000051 print "(%i)"%(len(l),),
52 for i in xrange(1000):
53 a = i*i
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000054 if context and not context.event.is_set():
55 continue
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000056 count += 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +000057 self.assertTrue(count < 10000,
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000058 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030059 if False and support.verbose:
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000060 print "(%i)"%(len(l),)
61
62 def test_pendingcalls_threaded(self):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000063 #do every callback on a separate thread
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000064 n = 32 #total callbacks
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000065 threads = []
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000066 class foo(object):pass
67 context = foo()
68 context.l = []
69 context.n = 2 #submits per thread
Ezio Melottidde5b942010-02-03 05:37:26 +000070 context.nThreads = n // context.n
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000071 context.nFinished = 0
72 context.lock = threading.Lock()
73 context.event = threading.Event()
74
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030075 threads = [threading.Thread(target=self.pendingcalls_thread,
76 args=(context,))
77 for i in range(context.nThreads)]
78 with support.start_threads(threads):
79 self.pendingcalls_wait(context.l, n, context)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000080
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000081 def pendingcalls_thread(self, context):
82 try:
83 self.pendingcalls_submit(context.l, context.n)
84 finally:
85 with context.lock:
86 context.nFinished += 1
87 nFinished = context.nFinished
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030088 if False and support.verbose:
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000089 print "finished threads: ", nFinished
90 if nFinished == context.nThreads:
91 context.event.set()
92
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000093 def test_pendingcalls_non_threaded(self):
Ezio Melottic2077b02011-03-16 12:34:31 +020094 #again, just using the main thread, likely they will all be dispatched at
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000095 #once. It is ok to ask for too many, because we loop until we find a slot.
96 #the loop can be interrupted to dispatch.
97 #there are only 32 dispatch slots, so we go for twice that!
98 l = []
99 n = 64
100 self.pendingcalls_submit(l, n)
101 self.pendingcalls_wait(l, n)
102
103
Ezio Melottief1db542013-02-23 06:33:51 +0200104@unittest.skipUnless(threading and thread, 'Threading required for this test.')
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200105class TestThreadState(unittest.TestCase):
Tim Peters59b96c12006-03-21 03:58:41 +0000106
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300107 @support.reap_threads
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200108 def test_thread_state(self):
109 # some extra thread-state tests driven via _testcapi
110 def target():
111 idents = []
112
113 def callback():
114 idents.append(thread.get_ident())
115
116 _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.
120 self.assertEqual(idents.count(thread.get_ident()), 3,
121 "Couldn't find main thread correctly in the list")
122
123 target()
124 t = threading.Thread(target=target)
125 t.start()
126 t.join()
127
128
129def test_main():
Tim Peters59b96c12006-03-21 03:58:41 +0000130 for name in dir(_testcapi):
131 if name.startswith('test_'):
132 test = getattr(_testcapi, name)
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300133 if support.verbose:
Tim Peters59b96c12006-03-21 03:58:41 +0000134 print "internal", name
135 try:
136 test()
137 except _testcapi.error:
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300138 raise support.TestFailed, sys.exc_info()[1]
Tim Peters59b96c12006-03-21 03:58:41 +0000139
Serhiy Storchaka12cf60c2016-05-20 22:31:24 +0300140 support.run_unittest(CAPITest, TestPendingCalls, TestThreadState)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000141
Tim Peters59b96c12006-03-21 03:58:41 +0000142if __name__ == "__main__":
143 test_main()