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