2001-02-03 23:09:53 -04:00
|
|
|
# Run the _testcapi module tests (tests for the Python/C API): by defn,
|
2001-04-13 14:03:04 -03:00
|
|
|
# these are all functions _testcapi exports whose name begins with 'test_'.
|
2001-02-02 01:57:15 -04:00
|
|
|
|
2009-01-18 06:58:44 -04:00
|
|
|
from __future__ import with_statement
|
2005-03-03 08:26:35 -04:00
|
|
|
import sys
|
2009-01-09 17:35:16 -04:00
|
|
|
import time
|
|
|
|
import random
|
|
|
|
import unittest
|
2015-04-01 06:56:39 -03:00
|
|
|
from test import test_support as support
|
2010-04-27 20:01:29 -03:00
|
|
|
try:
|
2013-02-23 00:33:51 -04:00
|
|
|
import thread
|
2010-04-27 20:01:29 -03:00
|
|
|
import threading
|
|
|
|
except ImportError:
|
2013-02-23 00:33:51 -04:00
|
|
|
thread = None
|
2010-04-27 20:01:29 -03:00
|
|
|
threading = None
|
2014-02-07 04:06:05 -04:00
|
|
|
# Skip this test if the _testcapi module isn't available.
|
2015-04-01 06:56:39 -03:00
|
|
|
_testcapi = support.import_module('_testcapi')
|
2014-02-07 04:06:05 -04:00
|
|
|
|
2016-05-20 16:31:24 -03:00
|
|
|
class CAPITest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_buildvalue_N(self):
|
|
|
|
_testcapi.test_buildvalue_N()
|
|
|
|
|
2001-02-02 01:57:15 -04:00
|
|
|
|
2010-04-27 20:01:29 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
2009-01-09 17:35:16 -04:00
|
|
|
class TestPendingCalls(unittest.TestCase):
|
|
|
|
|
|
|
|
def pendingcalls_submit(self, l, n):
|
|
|
|
def callback():
|
|
|
|
#this function can be interrupted by thread switching so let's
|
|
|
|
#use an atomic operation
|
|
|
|
l.append(None)
|
|
|
|
|
|
|
|
for i in range(n):
|
|
|
|
time.sleep(random.random()*0.02) #0.01 secs on average
|
|
|
|
#try submitting callback until successful.
|
|
|
|
#rely on regular interrupt to flush queue if we are
|
|
|
|
#unsuccessful.
|
|
|
|
while True:
|
|
|
|
if _testcapi._pending_threadfunc(callback):
|
|
|
|
break;
|
|
|
|
|
2009-01-18 06:58:44 -04:00
|
|
|
def pendingcalls_wait(self, l, n, context = None):
|
2009-01-09 17:35:16 -04:00
|
|
|
#now, stick around until l[0] has grown to 10
|
|
|
|
count = 0;
|
|
|
|
while len(l) != n:
|
|
|
|
#this busy loop is where we expect to be interrupted to
|
|
|
|
#run our callbacks. Note that callbacks are only run on the
|
|
|
|
#main thread
|
2015-04-01 06:56:39 -03:00
|
|
|
if False and support.verbose:
|
2009-01-09 17:35:16 -04:00
|
|
|
print "(%i)"%(len(l),),
|
|
|
|
for i in xrange(1000):
|
|
|
|
a = i*i
|
2009-01-18 06:58:44 -04:00
|
|
|
if context and not context.event.is_set():
|
|
|
|
continue
|
2009-01-09 17:35:16 -04:00
|
|
|
count += 1
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(count < 10000,
|
2009-01-09 17:35:16 -04:00
|
|
|
"timeout waiting for %i callbacks, got %i"%(n, len(l)))
|
2015-04-01 06:56:39 -03:00
|
|
|
if False and support.verbose:
|
2009-01-09 17:35:16 -04:00
|
|
|
print "(%i)"%(len(l),)
|
|
|
|
|
|
|
|
def test_pendingcalls_threaded(self):
|
|
|
|
#do every callback on a separate thread
|
2009-01-18 06:58:44 -04:00
|
|
|
n = 32 #total callbacks
|
2009-01-09 17:35:16 -04:00
|
|
|
threads = []
|
2009-01-18 06:58:44 -04:00
|
|
|
class foo(object):pass
|
|
|
|
context = foo()
|
|
|
|
context.l = []
|
|
|
|
context.n = 2 #submits per thread
|
2010-02-03 01:37:26 -04:00
|
|
|
context.nThreads = n // context.n
|
2009-01-18 06:58:44 -04:00
|
|
|
context.nFinished = 0
|
|
|
|
context.lock = threading.Lock()
|
|
|
|
context.event = threading.Event()
|
|
|
|
|
2015-04-01 06:56:39 -03:00
|
|
|
threads = [threading.Thread(target=self.pendingcalls_thread,
|
|
|
|
args=(context,))
|
|
|
|
for i in range(context.nThreads)]
|
|
|
|
with support.start_threads(threads):
|
|
|
|
self.pendingcalls_wait(context.l, n, context)
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2009-01-18 06:58:44 -04:00
|
|
|
def pendingcalls_thread(self, context):
|
|
|
|
try:
|
|
|
|
self.pendingcalls_submit(context.l, context.n)
|
|
|
|
finally:
|
|
|
|
with context.lock:
|
|
|
|
context.nFinished += 1
|
|
|
|
nFinished = context.nFinished
|
2015-04-01 06:56:39 -03:00
|
|
|
if False and support.verbose:
|
2009-01-18 06:58:44 -04:00
|
|
|
print "finished threads: ", nFinished
|
|
|
|
if nFinished == context.nThreads:
|
|
|
|
context.event.set()
|
|
|
|
|
2009-01-09 17:35:16 -04:00
|
|
|
def test_pendingcalls_non_threaded(self):
|
2011-03-16 07:34:31 -03:00
|
|
|
#again, just using the main thread, likely they will all be dispatched at
|
2009-01-09 17:35:16 -04:00
|
|
|
#once. It is ok to ask for too many, because we loop until we find a slot.
|
|
|
|
#the loop can be interrupted to dispatch.
|
|
|
|
#there are only 32 dispatch slots, so we go for twice that!
|
|
|
|
l = []
|
|
|
|
n = 64
|
|
|
|
self.pendingcalls_submit(l, n)
|
|
|
|
self.pendingcalls_wait(l, n)
|
|
|
|
|
|
|
|
|
2013-02-23 00:33:51 -04:00
|
|
|
@unittest.skipUnless(threading and thread, 'Threading required for this test.')
|
2013-02-22 23:45:37 -04:00
|
|
|
class TestThreadState(unittest.TestCase):
|
|
|
|
|
2015-04-01 06:56:39 -03:00
|
|
|
@support.reap_threads
|
2013-02-22 23:45:37 -04:00
|
|
|
def test_thread_state(self):
|
|
|
|
# some extra thread-state tests driven via _testcapi
|
|
|
|
def target():
|
|
|
|
idents = []
|
|
|
|
|
|
|
|
def callback():
|
|
|
|
idents.append(thread.get_ident())
|
|
|
|
|
|
|
|
_testcapi._test_thread_state(callback)
|
|
|
|
a = b = callback
|
|
|
|
time.sleep(1)
|
|
|
|
# Check our main thread is in the list exactly 3 times.
|
|
|
|
self.assertEqual(idents.count(thread.get_ident()), 3,
|
|
|
|
"Couldn't find main thread correctly in the list")
|
|
|
|
|
|
|
|
target()
|
|
|
|
t = threading.Thread(target=target)
|
|
|
|
t.start()
|
|
|
|
t.join()
|
|
|
|
|
2006-03-20 23:58:41 -04:00
|
|
|
|
2013-02-22 23:45:37 -04:00
|
|
|
def test_main():
|
2006-03-20 23:58:41 -04:00
|
|
|
for name in dir(_testcapi):
|
|
|
|
if name.startswith('test_'):
|
|
|
|
test = getattr(_testcapi, name)
|
2015-04-01 06:56:39 -03:00
|
|
|
if support.verbose:
|
2006-03-20 23:58:41 -04:00
|
|
|
print "internal", name
|
|
|
|
try:
|
|
|
|
test()
|
|
|
|
except _testcapi.error:
|
2015-04-01 06:56:39 -03:00
|
|
|
raise support.TestFailed, sys.exc_info()[1]
|
2006-03-20 23:58:41 -04:00
|
|
|
|
2016-05-20 16:31:24 -03:00
|
|
|
support.run_unittest(CAPITest, TestPendingCalls, TestThreadState)
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2006-03-20 23:58:41 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|