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