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 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +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 |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 6 | import time |
Ezio Melotti | 2fddfd8 | 2013-02-23 05:45:37 +0200 | [diff] [blame^] | 7 | import thread |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 8 | import random |
| 9 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 10 | from test import test_support |
Victor Stinner | be595d3 | 2010-04-27 23:01:29 +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 | |
Victor Stinner | be595d3 | 2010-04-27 23:01:29 +0000 | [diff] [blame] | 17 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 18 | class TestPendingCalls(unittest.TestCase): |
| 19 | |
| 20 | def pendingcalls_submit(self, l, n): |
| 21 | def callback(): |
| 22 | #this function can be interrupted by thread switching so let's |
| 23 | #use an atomic operation |
| 24 | l.append(None) |
| 25 | |
| 26 | for i in range(n): |
| 27 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 28 | #try submitting callback until successful. |
| 29 | #rely on regular interrupt to flush queue if we are |
| 30 | #unsuccessful. |
| 31 | while True: |
| 32 | if _testcapi._pending_threadfunc(callback): |
| 33 | break; |
| 34 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 35 | def pendingcalls_wait(self, l, n, context = None): |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 36 | #now, stick around until l[0] has grown to 10 |
| 37 | count = 0; |
| 38 | while len(l) != n: |
| 39 | #this busy loop is where we expect to be interrupted to |
| 40 | #run our callbacks. Note that callbacks are only run on the |
| 41 | #main thread |
| 42 | if False and test_support.verbose: |
| 43 | print "(%i)"%(len(l),), |
| 44 | for i in xrange(1000): |
| 45 | a = i*i |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 46 | if context and not context.event.is_set(): |
| 47 | continue |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 48 | count += 1 |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 49 | self.assertTrue(count < 10000, |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 50 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
| 51 | if False and test_support.verbose: |
| 52 | print "(%i)"%(len(l),) |
| 53 | |
| 54 | def test_pendingcalls_threaded(self): |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 55 | #do every callback on a separate thread |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 56 | n = 32 #total callbacks |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 57 | threads = [] |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 58 | class foo(object):pass |
| 59 | context = foo() |
| 60 | context.l = [] |
| 61 | context.n = 2 #submits per thread |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 62 | context.nThreads = n // context.n |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 63 | context.nFinished = 0 |
| 64 | context.lock = threading.Lock() |
| 65 | context.event = threading.Event() |
| 66 | |
| 67 | for i in range(context.nThreads): |
| 68 | t = threading.Thread(target=self.pendingcalls_thread, args = (context,)) |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 69 | t.start() |
| 70 | threads.append(t) |
| 71 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 72 | self.pendingcalls_wait(context.l, n, context) |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 73 | |
| 74 | for t in threads: |
| 75 | t.join() |
| 76 | |
Kristján Valur Jónsson | 60e79ce | 2009-01-18 10:58:44 +0000 | [diff] [blame] | 77 | def pendingcalls_thread(self, context): |
| 78 | try: |
| 79 | self.pendingcalls_submit(context.l, context.n) |
| 80 | finally: |
| 81 | with context.lock: |
| 82 | context.nFinished += 1 |
| 83 | nFinished = context.nFinished |
| 84 | if False and test_support.verbose: |
| 85 | print "finished threads: ", nFinished |
| 86 | if nFinished == context.nThreads: |
| 87 | context.event.set() |
| 88 | |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 89 | def test_pendingcalls_non_threaded(self): |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 90 | #again, just using the main thread, likely they will all be dispatched at |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 91 | #once. It is ok to ask for too many, because we loop until we find a slot. |
| 92 | #the loop can be interrupted to dispatch. |
| 93 | #there are only 32 dispatch slots, so we go for twice that! |
| 94 | l = [] |
| 95 | n = 64 |
| 96 | self.pendingcalls_submit(l, n) |
| 97 | self.pendingcalls_wait(l, n) |
| 98 | |
| 99 | |
Ezio Melotti | 2fddfd8 | 2013-02-23 05:45:37 +0200 | [diff] [blame^] | 100 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 101 | class TestThreadState(unittest.TestCase): |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 102 | |
Ezio Melotti | 2fddfd8 | 2013-02-23 05:45:37 +0200 | [diff] [blame^] | 103 | @test_support.reap_threads |
| 104 | def test_thread_state(self): |
| 105 | # some extra thread-state tests driven via _testcapi |
| 106 | def target(): |
| 107 | idents = [] |
| 108 | |
| 109 | def callback(): |
| 110 | idents.append(thread.get_ident()) |
| 111 | |
| 112 | _testcapi._test_thread_state(callback) |
| 113 | a = b = callback |
| 114 | time.sleep(1) |
| 115 | # Check our main thread is in the list exactly 3 times. |
| 116 | self.assertEqual(idents.count(thread.get_ident()), 3, |
| 117 | "Couldn't find main thread correctly in the list") |
| 118 | |
| 119 | target() |
| 120 | t = threading.Thread(target=target) |
| 121 | t.start() |
| 122 | t.join() |
| 123 | |
| 124 | |
| 125 | def test_main(): |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 126 | for name in dir(_testcapi): |
| 127 | if name.startswith('test_'): |
| 128 | test = getattr(_testcapi, name) |
| 129 | if test_support.verbose: |
| 130 | print "internal", name |
| 131 | try: |
| 132 | test() |
| 133 | except _testcapi.error: |
| 134 | raise test_support.TestFailed, sys.exc_info()[1] |
| 135 | |
Ezio Melotti | 2fddfd8 | 2013-02-23 05:45:37 +0200 | [diff] [blame^] | 136 | test_support.run_unittest(TestPendingCalls, TestThreadState) |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 137 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 138 | if __name__ == "__main__": |
| 139 | test_main() |