blob: 2029265e1cd2552a0e1e4a8604c062fb497c1af8 [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
Tim Peters9ea17ac2001-02-02 05:57:15 +000019
Victor Stinnerbe595d32010-04-27 23:01:29 +000020@unittest.skipUnless(threading, 'Threading required for this test.')
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000021class TestPendingCalls(unittest.TestCase):
22
23 def pendingcalls_submit(self, l, n):
24 def callback():
25 #this function can be interrupted by thread switching so let's
26 #use an atomic operation
27 l.append(None)
28
29 for i in range(n):
30 time.sleep(random.random()*0.02) #0.01 secs on average
31 #try submitting callback until successful.
32 #rely on regular interrupt to flush queue if we are
33 #unsuccessful.
34 while True:
35 if _testcapi._pending_threadfunc(callback):
36 break;
37
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000038 def pendingcalls_wait(self, l, n, context = None):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000039 #now, stick around until l[0] has grown to 10
40 count = 0;
41 while len(l) != n:
42 #this busy loop is where we expect to be interrupted to
43 #run our callbacks. Note that callbacks are only run on the
44 #main thread
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030045 if False and support.verbose:
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000046 print "(%i)"%(len(l),),
47 for i in xrange(1000):
48 a = i*i
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000049 if context and not context.event.is_set():
50 continue
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000051 count += 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +000052 self.assertTrue(count < 10000,
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000053 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030054 if False and support.verbose:
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000055 print "(%i)"%(len(l),)
56
57 def test_pendingcalls_threaded(self):
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000058 #do every callback on a separate thread
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000059 n = 32 #total callbacks
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000060 threads = []
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000061 class foo(object):pass
62 context = foo()
63 context.l = []
64 context.n = 2 #submits per thread
Ezio Melottidde5b942010-02-03 05:37:26 +000065 context.nThreads = n // context.n
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000066 context.nFinished = 0
67 context.lock = threading.Lock()
68 context.event = threading.Event()
69
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030070 threads = [threading.Thread(target=self.pendingcalls_thread,
71 args=(context,))
72 for i in range(context.nThreads)]
73 with support.start_threads(threads):
74 self.pendingcalls_wait(context.l, n, context)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000075
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000076 def pendingcalls_thread(self, context):
77 try:
78 self.pendingcalls_submit(context.l, context.n)
79 finally:
80 with context.lock:
81 context.nFinished += 1
82 nFinished = context.nFinished
Serhiy Storchakabd8c6292015-04-01 12:56:39 +030083 if False and support.verbose:
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000084 print "finished threads: ", nFinished
85 if nFinished == context.nThreads:
86 context.event.set()
87
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000088 def test_pendingcalls_non_threaded(self):
Ezio Melottic2077b02011-03-16 12:34:31 +020089 #again, just using the main thread, likely they will all be dispatched at
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000090 #once. It is ok to ask for too many, because we loop until we find a slot.
91 #the loop can be interrupted to dispatch.
92 #there are only 32 dispatch slots, so we go for twice that!
93 l = []
94 n = 64
95 self.pendingcalls_submit(l, n)
96 self.pendingcalls_wait(l, n)
97
98
Ezio Melottief1db542013-02-23 06:33:51 +020099@unittest.skipUnless(threading and thread, 'Threading required for this test.')
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200100class TestThreadState(unittest.TestCase):
Tim Peters59b96c12006-03-21 03:58:41 +0000101
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300102 @support.reap_threads
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200103 def test_thread_state(self):
104 # some extra thread-state tests driven via _testcapi
105 def target():
106 idents = []
107
108 def callback():
109 idents.append(thread.get_ident())
110
111 _testcapi._test_thread_state(callback)
112 a = b = callback
113 time.sleep(1)
114 # Check our main thread is in the list exactly 3 times.
115 self.assertEqual(idents.count(thread.get_ident()), 3,
116 "Couldn't find main thread correctly in the list")
117
118 target()
119 t = threading.Thread(target=target)
120 t.start()
121 t.join()
122
123
124def test_main():
Tim Peters59b96c12006-03-21 03:58:41 +0000125 for name in dir(_testcapi):
126 if name.startswith('test_'):
127 test = getattr(_testcapi, name)
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300128 if support.verbose:
Tim Peters59b96c12006-03-21 03:58:41 +0000129 print "internal", name
130 try:
131 test()
132 except _testcapi.error:
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300133 raise support.TestFailed, sys.exc_info()[1]
Tim Peters59b96c12006-03-21 03:58:41 +0000134
Serhiy Storchakabd8c6292015-04-01 12:56:39 +0300135 support.run_unittest(TestPendingCalls, TestThreadState)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000136
Tim Peters59b96c12006-03-21 03:58:41 +0000137if __name__ == "__main__":
138 test_main()