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