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