summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi.py
diff options
context:
space:
mode:
authorKristján Valur Jónsson <kristjan@ccpgames.com>2009-01-09 21:35:16 (GMT)
committerKristján Valur Jónsson <kristjan@ccpgames.com>2009-01-09 21:35:16 (GMT)
commit0e2d8c36e3b52b7ff9d2926d1c2ad4be9df84710 (patch)
treecdfe6ca9439994206d86af13cc247383affeec8a /Lib/test/test_capi.py
parent0e91938e5851173821f917bf19157dde5f946c09 (diff)
downloadcpython-0e2d8c36e3b52b7ff9d2926d1c2ad4be9df84710.zip
cpython-0e2d8c36e3b52b7ff9d2926d1c2ad4be9df84710.tar.gz
cpython-0e2d8c36e3b52b7ff9d2926d1c2ad4be9df84710.tar.bz2
Issue 4293: Make Py_AddPendingCall() thread safe
Add test cases and documentation
Diffstat (limited to 'Lib/test/test_capi.py')
-rw-r--r--Lib/test/test_capi.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index eea41c1..17c4baf 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -2,9 +2,74 @@
# these are all functions _testcapi exports whose name begins with 'test_'.
import sys
+import time
+import random
+import unittest
+import threading
from test import test_support
import _testcapi
+class TestPendingCalls(unittest.TestCase):
+
+ def pendingcalls_submit(self, l, n):
+ def callback():
+ #this function can be interrupted by thread switching so let's
+ #use an atomic operation
+ l.append(None)
+
+ for i in range(n):
+ time.sleep(random.random()*0.02) #0.01 secs on average
+ #try submitting callback until successful.
+ #rely on regular interrupt to flush queue if we are
+ #unsuccessful.
+ while True:
+ if _testcapi._pending_threadfunc(callback):
+ break;
+
+ def pendingcalls_wait(self, l, n):
+ #now, stick around until l[0] has grown to 10
+ count = 0;
+ while len(l) != n:
+ #this busy loop is where we expect to be interrupted to
+ #run our callbacks. Note that callbacks are only run on the
+ #main thread
+ if False and test_support.verbose:
+ print "(%i)"%(len(l),),
+ for i in xrange(1000):
+ a = i*i
+ count += 1
+ self.failUnless(count < 10000,
+ "timeout waiting for %i callbacks, got %i"%(n, len(l)))
+ if False and test_support.verbose:
+ print "(%i)"%(len(l),)
+
+ def test_pendingcalls_threaded(self):
+ l = []
+
+ #do every callback on a separate thread
+ n = 32
+ threads = []
+ for i in range(n):
+ t = threading.Thread(target=self.pendingcalls_submit, args = (l, 1))
+ t.start()
+ threads.append(t)
+
+ self.pendingcalls_wait(l, n)
+
+ for t in threads:
+ t.join()
+
+ def test_pendingcalls_non_threaded(self):
+ #again, just using the main thread, likely they will all be dispathced at
+ #once. It is ok to ask for too many, because we loop until we find a slot.
+ #the loop can be interrupted to dispatch.
+ #there are only 32 dispatch slots, so we go for twice that!
+ l = []
+ n = 64
+ self.pendingcalls_submit(l, n)
+ self.pendingcalls_wait(l, n)
+
+
def test_main():
for name in dir(_testcapi):
@@ -50,5 +115,7 @@ def test_main():
t.start()
t.join()
+ test_support.run_unittest(TestPendingCalls)
+
if __name__ == "__main__":
test_main()