blob: a2cb5c790f23e652a132ce3dd0eb6dc9f0b0c1d8 [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
Serhiy Storchaka76249ea2014-02-07 10:06:05 +020016# Skip this test if the _testcapi module isn't available.
17_testcapi = test_support.import_module('_testcapi')
18
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
45 if False and test_support.verbose:
46 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)))
54 if False and test_support.verbose:
55 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
70 for i in range(context.nThreads):
71 t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000072 t.start()
73 threads.append(t)
74
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000075 self.pendingcalls_wait(context.l, n, context)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000076
77 for t in threads:
78 t.join()
79
Kristján Valur Jónsson60e79ce2009-01-18 10:58:44 +000080 def pendingcalls_thread(self, context):
81 try:
82 self.pendingcalls_submit(context.l, context.n)
83 finally:
84 with context.lock:
85 context.nFinished += 1
86 nFinished = context.nFinished
87 if False and test_support.verbose:
88 print "finished threads: ", nFinished
89 if nFinished == context.nThreads:
90 context.event.set()
91
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000092 def test_pendingcalls_non_threaded(self):
Ezio Melottic2077b02011-03-16 12:34:31 +020093 #again, just using the main thread, likely they will all be dispatched at
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +000094 #once. It is ok to ask for too many, because we loop until we find a slot.
95 #the loop can be interrupted to dispatch.
96 #there are only 32 dispatch slots, so we go for twice that!
97 l = []
98 n = 64
99 self.pendingcalls_submit(l, n)
100 self.pendingcalls_wait(l, n)
101
102
Ezio Melottief1db542013-02-23 06:33:51 +0200103@unittest.skipUnless(threading and thread, 'Threading required for this test.')
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200104class TestThreadState(unittest.TestCase):
Tim Peters59b96c12006-03-21 03:58:41 +0000105
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200106 @test_support.reap_threads
107 def test_thread_state(self):
108 # some extra thread-state tests driven via _testcapi
109 def target():
110 idents = []
111
112 def callback():
113 idents.append(thread.get_ident())
114
115 _testcapi._test_thread_state(callback)
116 a = b = callback
117 time.sleep(1)
118 # Check our main thread is in the list exactly 3 times.
119 self.assertEqual(idents.count(thread.get_ident()), 3,
120 "Couldn't find main thread correctly in the list")
121
122 target()
123 t = threading.Thread(target=target)
124 t.start()
125 t.join()
126
127
128def test_main():
Tim Peters59b96c12006-03-21 03:58:41 +0000129 for name in dir(_testcapi):
130 if name.startswith('test_'):
131 test = getattr(_testcapi, name)
132 if test_support.verbose:
133 print "internal", name
134 try:
135 test()
136 except _testcapi.error:
137 raise test_support.TestFailed, sys.exc_info()[1]
138
Ezio Melotti2fddfd82013-02-23 05:45:37 +0200139 test_support.run_unittest(TestPendingCalls, TestThreadState)
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000140
Tim Peters59b96c12006-03-21 03:58:41 +0000141if __name__ == "__main__":
142 test_main()