Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1 | # Run the _testcapi module tests (tests for the Python/C API): by defn, |
Guido van Rossum | 361c535 | 2001-04-13 17:03:04 +0000 | [diff] [blame] | 2 | # these are all functions _testcapi exports whose name begins with 'test_'. |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 4 | from __future__ import with_statement |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 5 | import sys |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 6 | import time |
| 7 | import random |
| 8 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 9 | from test import test_support |
Victor Stinner | be595d3 | 2010-04-27 23:01:29 +0000 | [diff] [blame] | 10 | try: |
| 11 | import threading |
| 12 | except ImportError: |
| 13 | threading = None |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 14 | import _testcapi |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 15 | |
Victor Stinner | be595d3 | 2010-04-27 23:01:29 +0000 | [diff] [blame] | 16 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 17 | class TestPendingCalls(unittest.TestCase): |
| 18 | |
| 19 | def pendingcalls_submit(self, l, n): |
| 20 | def callback(): |
| 21 | #this function can be interrupted by thread switching so let's |
| 22 | #use an atomic operation |
| 23 | l.append(None) |
| 24 | |
| 25 | for i in range(n): |
| 26 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 27 | #try submitting callback until successful. |
| 28 | #rely on regular interrupt to flush queue if we are |
| 29 | #unsuccessful. |
| 30 | while True: |
| 31 | if _testcapi._pending_threadfunc(callback): |
| 32 | break; |
| 33 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 34 | def pendingcalls_wait(self, l, n, context = None): |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 35 | #now, stick around until l[0] has grown to 10 |
| 36 | count = 0; |
| 37 | while len(l) != n: |
| 38 | #this busy loop is where we expect to be interrupted to |
| 39 | #run our callbacks. Note that callbacks are only run on the |
| 40 | #main thread |
| 41 | if False and test_support.verbose: |
| 42 | print "(%i)"%(len(l),), |
| 43 | for i in xrange(1000): |
| 44 | a = i*i |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 45 | if context and not context.event.is_set(): |
| 46 | continue |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 47 | count += 1 |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 48 | self.assertTrue(count < 10000, |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 49 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
| 50 | if False and test_support.verbose: |
| 51 | print "(%i)"%(len(l),) |
| 52 | |
| 53 | def test_pendingcalls_threaded(self): |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 54 | #do every callback on a separate thread |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 55 | n = 32 #total callbacks |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 56 | threads = [] |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 57 | class foo(object):pass |
| 58 | context = foo() |
| 59 | context.l = [] |
| 60 | context.n = 2 #submits per thread |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 61 | context.nThreads = n // context.n |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 62 | context.nFinished = 0 |
| 63 | context.lock = threading.Lock() |
| 64 | context.event = threading.Event() |
| 65 | |
| 66 | for i in range(context.nThreads): |
| 67 | t = threading.Thread(target=self.pendingcalls_thread, args = (context,)) |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 68 | t.start() |
| 69 | threads.append(t) |
| 70 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 71 | self.pendingcalls_wait(context.l, n, context) |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 72 | |
| 73 | for t in threads: |
| 74 | t.join() |
| 75 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 76 | 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 |
| 83 | if False and test_support.verbose: |
| 84 | print "finished threads: ", nFinished |
| 85 | if nFinished == context.nThreads: |
| 86 | context.event.set() |
| 87 | |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 88 | def test_pendingcalls_non_threaded(self): |
| 89 | #again, just using the main thread, likely they will all be dispathced at |
| 90 | #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 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 99 | def test_main(): |
| 100 | |
| 101 | for name in dir(_testcapi): |
| 102 | if name.startswith('test_'): |
| 103 | test = getattr(_testcapi, name) |
| 104 | if test_support.verbose: |
| 105 | print "internal", name |
| 106 | try: |
| 107 | test() |
| 108 | except _testcapi.error: |
| 109 | raise test_support.TestFailed, sys.exc_info()[1] |
| 110 | |
| 111 | # some extra thread-state tests driven via _testcapi |
| 112 | def TestThreadState(): |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 113 | if test_support.verbose: |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 114 | print "auto-thread-state" |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 115 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 116 | idents = [] |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 117 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 118 | def callback(): |
| 119 | idents.append(thread.get_ident()) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 120 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 121 | _testcapi._test_thread_state(callback) |
| 122 | a = b = callback |
| 123 | time.sleep(1) |
| 124 | # Check our main thread is in the list exactly 3 times. |
| 125 | if idents.count(thread.get_ident()) != 3: |
| 126 | raise test_support.TestFailed, \ |
| 127 | "Couldn't find main thread correctly in the list" |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 128 | |
Victor Stinner | be595d3 | 2010-04-27 23:01:29 +0000 | [diff] [blame] | 129 | if threading: |
Amaury Forgeot d'Arc | 4b798bd | 2008-04-08 21:27:42 +0000 | [diff] [blame] | 130 | import thread |
| 131 | import time |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 132 | TestThreadState() |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 133 | t=threading.Thread(target=TestThreadState) |
| 134 | t.start() |
| 135 | t.join() |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 136 | |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 137 | test_support.run_unittest(TestPendingCalls) |
| 138 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 139 | if __name__ == "__main__": |
| 140 | test_main() |