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