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 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +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 |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 6 | import time |
| 7 | import random |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 8 | import unittest |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 9 | import threading |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import 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 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 14 | def testfunction(self): |
| 15 | """some doc""" |
| 16 | return self |
| 17 | |
| 18 | class InstanceMethod: |
| 19 | id = _testcapi.instancemethod(id) |
| 20 | testfunction = _testcapi.instancemethod(testfunction) |
| 21 | |
| 22 | class CAPITest(unittest.TestCase): |
| 23 | |
| 24 | def test_instancemethod(self): |
| 25 | inst = InstanceMethod() |
| 26 | self.assertEqual(id(inst), inst.id()) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 27 | self.assertTrue(inst.testfunction() is inst) |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 28 | self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__) |
| 29 | self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__) |
| 30 | |
| 31 | InstanceMethod.testfunction.attribute = "test" |
| 32 | self.assertEqual(testfunction.attribute, "test") |
| 33 | self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test") |
| 34 | |
| 35 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 36 | class TestPendingCalls(unittest.TestCase): |
| 37 | |
| 38 | def pendingcalls_submit(self, l, n): |
| 39 | def callback(): |
| 40 | #this function can be interrupted by thread switching so let's |
| 41 | #use an atomic operation |
| 42 | l.append(None) |
| 43 | |
| 44 | for i in range(n): |
| 45 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 46 | #try submitting callback until successful. |
| 47 | #rely on regular interrupt to flush queue if we are |
| 48 | #unsuccessful. |
| 49 | while True: |
| 50 | if _testcapi._pending_threadfunc(callback): |
| 51 | break; |
| 52 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 53 | def pendingcalls_wait(self, l, n, context = None): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 54 | #now, stick around until l[0] has grown to 10 |
| 55 | count = 0; |
| 56 | while len(l) != n: |
| 57 | #this busy loop is where we expect to be interrupted to |
| 58 | #run our callbacks. Note that callbacks are only run on the |
| 59 | #main thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 60 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 61 | print("(%i)"%(len(l),),) |
| 62 | for i in range(1000): |
| 63 | a = i*i |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 64 | if context and not context.event.is_set(): |
| 65 | continue |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 66 | count += 1 |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 67 | self.assertTrue(count < 10000, |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 68 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 69 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 70 | print("(%i)"%(len(l),)) |
| 71 | |
| 72 | def test_pendingcalls_threaded(self): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 73 | |
| 74 | #do every callback on a separate thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 75 | n = 32 #total callbacks |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 76 | threads = [] |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 77 | class foo(object):pass |
| 78 | context = foo() |
| 79 | context.l = [] |
| 80 | context.n = 2 #submits per thread |
| 81 | context.nThreads = n // context.n |
| 82 | context.nFinished = 0 |
| 83 | context.lock = threading.Lock() |
| 84 | context.event = threading.Event() |
| 85 | |
| 86 | for i in range(context.nThreads): |
| 87 | t = threading.Thread(target=self.pendingcalls_thread, args = (context,)) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 88 | t.start() |
| 89 | threads.append(t) |
| 90 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 91 | self.pendingcalls_wait(context.l, n, context) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 92 | |
| 93 | for t in threads: |
| 94 | t.join() |
| 95 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 96 | def pendingcalls_thread(self, context): |
| 97 | try: |
| 98 | self.pendingcalls_submit(context.l, context.n) |
| 99 | finally: |
| 100 | with context.lock: |
| 101 | context.nFinished += 1 |
| 102 | nFinished = context.nFinished |
| 103 | if False and support.verbose: |
| 104 | print("finished threads: ", nFinished) |
| 105 | if nFinished == context.nThreads: |
| 106 | context.event.set() |
| 107 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 108 | def test_pendingcalls_non_threaded(self): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 109 | #again, just using the main thread, likely they will all be dispatched at |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 110 | #once. It is ok to ask for too many, because we loop until we find a slot. |
| 111 | #the loop can be interrupted to dispatch. |
| 112 | #there are only 32 dispatch slots, so we go for twice that! |
| 113 | l = [] |
| 114 | n = 64 |
| 115 | self.pendingcalls_submit(l, n) |
| 116 | self.pendingcalls_wait(l, n) |
| 117 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 118 | # Bug #6012 |
| 119 | class Test6012(unittest.TestCase): |
| 120 | def test(self): |
| 121 | self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 122 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | def test_main(): |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 124 | support.run_unittest(CAPITest) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 125 | |
| 126 | for name in dir(_testcapi): |
| 127 | if name.startswith('test_'): |
| 128 | test = getattr(_testcapi, name) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 129 | if support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 130 | print("internal", name) |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 131 | test() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 132 | |
| 133 | # some extra thread-state tests driven via _testcapi |
| 134 | def TestThreadState(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 135 | if support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 136 | print("auto-thread-state") |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 137 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 138 | idents = [] |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 139 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 140 | def callback(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 141 | idents.append(_thread.get_ident()) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 142 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 143 | _testcapi._test_thread_state(callback) |
| 144 | a = b = callback |
| 145 | time.sleep(1) |
| 146 | # Check our main thread is in the list exactly 3 times. |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 147 | if idents.count(_thread.get_ident()) != 3: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 148 | raise support.TestFailed( |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 149 | "Couldn't find main thread correctly in the list") |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 150 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 151 | try: |
| 152 | _testcapi._test_thread_state |
| 153 | have_thread_state = True |
| 154 | except AttributeError: |
| 155 | have_thread_state = False |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 156 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 157 | if have_thread_state: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 158 | import _thread |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 159 | import time |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 160 | TestThreadState() |
| 161 | import threading |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 162 | t = threading.Thread(target=TestThreadState) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | t.start() |
| 164 | t.join() |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 166 | support.run_unittest(TestPendingCalls, Test6012) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 167 | |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 169 | if __name__ == "__main__": |
| 170 | test_main() |