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 |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 5 | import time |
| 6 | import random |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 7 | import unittest |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 8 | import threading |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import 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 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 13 | def testfunction(self): |
| 14 | """some doc""" |
| 15 | return self |
| 16 | |
| 17 | class InstanceMethod: |
| 18 | id = _testcapi.instancemethod(id) |
| 19 | testfunction = _testcapi.instancemethod(testfunction) |
| 20 | |
| 21 | class CAPITest(unittest.TestCase): |
| 22 | |
| 23 | def test_instancemethod(self): |
| 24 | inst = InstanceMethod() |
| 25 | self.assertEqual(id(inst), inst.id()) |
| 26 | self.assert_(inst.testfunction() is inst) |
| 27 | self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__) |
| 28 | self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__) |
| 29 | |
| 30 | InstanceMethod.testfunction.attribute = "test" |
| 31 | self.assertEqual(testfunction.attribute, "test") |
| 32 | self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test") |
| 33 | |
| 34 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 35 | class TestPendingCalls(unittest.TestCase): |
| 36 | |
| 37 | def pendingcalls_submit(self, l, n): |
| 38 | def callback(): |
| 39 | #this function can be interrupted by thread switching so let's |
| 40 | #use an atomic operation |
| 41 | l.append(None) |
| 42 | |
| 43 | for i in range(n): |
| 44 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 45 | #try submitting callback until successful. |
| 46 | #rely on regular interrupt to flush queue if we are |
| 47 | #unsuccessful. |
| 48 | while True: |
| 49 | if _testcapi._pending_threadfunc(callback): |
| 50 | break; |
| 51 | |
| 52 | def pendingcalls_wait(self, l, n): |
| 53 | #now, stick around until l[0] has grown to 10 |
| 54 | count = 0; |
| 55 | while len(l) != n: |
| 56 | #this busy loop is where we expect to be interrupted to |
| 57 | #run our callbacks. Note that callbacks are only run on the |
| 58 | #main thread |
| 59 | if False and test_support.verbose: |
| 60 | print("(%i)"%(len(l),),) |
| 61 | for i in range(1000): |
| 62 | a = i*i |
| 63 | count += 1 |
| 64 | self.failUnless(count < 10000, |
| 65 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
| 66 | if False and test_support.verbose: |
| 67 | print("(%i)"%(len(l),)) |
| 68 | |
| 69 | def test_pendingcalls_threaded(self): |
| 70 | l = [] |
| 71 | |
| 72 | #do every callback on a separate thread |
| 73 | n = 32 |
| 74 | threads = [] |
| 75 | for i in range(n): |
| 76 | t = threading.Thread(target=self.pendingcalls_submit, args = (l, 1)) |
| 77 | t.start() |
| 78 | threads.append(t) |
| 79 | |
| 80 | self.pendingcalls_wait(l, n) |
| 81 | |
| 82 | for t in threads: |
| 83 | t.join() |
| 84 | |
| 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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 96 | def test_main(): |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 97 | support.run_unittest(CAPITest) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 98 | |
| 99 | for name in dir(_testcapi): |
| 100 | if name.startswith('test_'): |
| 101 | test = getattr(_testcapi, name) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 102 | if support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 103 | print("internal", name) |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 104 | test() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | |
| 106 | # some extra thread-state tests driven via _testcapi |
| 107 | def TestThreadState(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 108 | if support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 109 | print("auto-thread-state") |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | idents = [] |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 112 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | def callback(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 114 | idents.append(_thread.get_ident()) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 115 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 116 | _testcapi._test_thread_state(callback) |
| 117 | a = b = callback |
| 118 | time.sleep(1) |
| 119 | # Check our main thread is in the list exactly 3 times. |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 120 | if idents.count(_thread.get_ident()) != 3: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 121 | raise support.TestFailed( |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 122 | "Couldn't find main thread correctly in the list") |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 123 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 124 | try: |
| 125 | _testcapi._test_thread_state |
| 126 | have_thread_state = True |
| 127 | except AttributeError: |
| 128 | have_thread_state = False |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 129 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 130 | if have_thread_state: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 131 | import _thread |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 132 | import time |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 133 | TestThreadState() |
| 134 | import threading |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 135 | t = threading.Thread(target=TestThreadState) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 136 | t.start() |
| 137 | t.join() |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 138 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 139 | support.run_unittest(TestPendingCalls) |
| 140 | |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 141 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | if __name__ == "__main__": |
| 143 | test_main() |