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