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