[py-svn] r31860 - in py/branch/distributed/py/test/rsession: . testing

fijal at codespeak.net fijal at codespeak.net
Wed Aug 30 23:23:15 CEST 2006


Author: fijal
Date: Wed Aug 30 23:23:14 2006
New Revision: 31860

Modified:
   py/branch/distributed/py/test/rsession/box.py
   py/branch/distributed/py/test/rsession/testing/example2.py
   py/branch/distributed/py/test/rsession/testing/test_boxing.py
Log:
Fixed boxing subtle race conditions. Now I hope RSession should be able to run all pypy tests.


Modified: py/branch/distributed/py/test/rsession/box.py
==============================================================================
--- py/branch/distributed/py/test/rsession/box.py	(original)
+++ py/branch/distributed/py/test/rsession/box.py	Wed Aug 30 23:23:14 2006
@@ -7,6 +7,7 @@
 import os
 import sys
 import marshal
+import thread
 
 PYTESTSTDOUT = "pyteststdout"
 PYTESTSTDERR = "pyteststderr"
@@ -15,7 +16,7 @@
 import tempfile
 from StringIO import StringIO
 
-class Box(object):
+class SimpleBox(object):
     def __init__(self, fun, args = [], kwargs = {}):
         self.fun = fun
         self.args = args
@@ -33,7 +34,7 @@
         self.exitstat = 0
         return 123
 
-class XXXBox(object):
+class Box(object):
     def __init__(self, fun, args = [], kwargs = {}):
         self.fun = fun
         self.args = args
@@ -61,6 +62,8 @@
                     print str(i)[2:-1]
                 print excinfo
                 os._exit(1)
+            os.close(1)
+            os.close(2)
             os._exit(0)
         return pid
     
@@ -82,23 +85,37 @@
         retvalf.close()
     
     def parent(self):
-        self.fdstdout = open(self.PYTESTSTDOUT, "r")
-        self.fdstderr = open(self.PYTESTSTDERR, "r")
+        stdoutlock = thread.allocate_lock()
+        stdoutlock.acquire()
+        stderrlock = thread.allocate_lock()
+        stderrlock.acquire()
+
+        def readsome(name, field, lock):
+            fd = open(name, "r")
+            setattr(self, field, fd.read())
+            fd.close()
+            lock.release()
+        
+        thread.start_new_thread(readsome, (self.PYTESTSTDOUT, 'stdoutrepr', stdoutlock))
+        thread.start_new_thread(readsome, (self.PYTESTSTDERR, 'stderrrepr', stderrlock))
+        
         retval = open(self.PYTESTRETVAL, "r")
         pid, exitstat = os.wait()
         self.signal = exitstat & 0x7f
         self.exitstat = exitstat & 0xff00
+
         if not exitstat:
             retval_data = retval.read()
-            print retval_data
             self.retval = marshal.loads(retval_data)
             retval.close()
         else:
             self.retval = None
-        self.stdoutrepr = self.fdstdout.read()
-        self.stderrrepr = self.fdstderr.read()
-        self.fdstdout.close()
-        self.fdstderr.close()
+        
+        stdoutlock.acquire()
+        stdoutlock.release()
+        stderrlock.acquire()
+        stderrlock.release()
+        
         self.clear()
         return self.stdoutrepr, self.stderrrepr
     
@@ -110,3 +127,5 @@
             os.rmdir(self.dirname)
         except OSError:
             pass
+
+RealBox = Box

Modified: py/branch/distributed/py/test/rsession/testing/example2.py
==============================================================================
--- py/branch/distributed/py/test/rsession/testing/example2.py	(original)
+++ py/branch/distributed/py/test/rsession/testing/example2.py	Wed Aug 30 23:23:14 2006
@@ -17,3 +17,14 @@
 
 def boxseg():
     os.kill(os.getpid(), 11)
+
+def boxhuge():
+    os.write(1, " " * 10000)
+    os.write(2, " " * 10000)
+    os.write(1, " " * 10000)
+    
+    os.write(1, " " * 10000)
+    os.write(2, " " * 10000)
+    os.write(2, " " * 10000)
+    os.write(1, " " * 10000)
+    return 3

Modified: py/branch/distributed/py/test/rsession/testing/test_boxing.py
==============================================================================
--- py/branch/distributed/py/test/rsession/testing/test_boxing.py	(original)
+++ py/branch/distributed/py/test/rsession/testing/test_boxing.py	Wed Aug 30 23:23:14 2006
@@ -4,7 +4,7 @@
 
 import py, sys
 
-from py.__.test.rsession.box import Box
+from py.__.test.rsession.box import Box, RealBox
 from py.__.test.rsession.testing import example2
 from py.__.test.rsession.conftest import option 
 
@@ -21,8 +21,7 @@
     assert b.retval == 1
 
 def test_boxing_on_fds():
-    py.test.skip("Box WIP")
-    b = Box(example2.boxf2)
+    b = RealBox(example2.boxf2)
     b.run()
     assert b.stdoutrepr == "someout"
     assert b.stderrrepr == "someerr"
@@ -31,8 +30,25 @@
     assert b.retval == 2
 
 def test_boxing_signal():
-    py.test.skip("Box WIP")
-    b = Box(example2.boxseg)
+    b = RealBox(example2.boxseg)
     b.run()
     assert b.signal == 11
     assert b.retval is None
+
+def test_boxing_huge_data():
+    b = RealBox(example2.boxhuge)
+    b.run()
+    assert b.stdoutrepr
+    assert b.exitstat == 0
+    assert b.signal == 0
+    assert b.retval == 3
+
+def test_box_seq():
+    # we run many boxes with huge data, just one after another
+    for i in xrange(100):
+        b = RealBox(example2.boxhuge)
+        b.run()
+        assert b.stdoutrepr
+        assert b.exitstat == 0
+        assert b.signal == 0
+        assert b.retval == 3



More information about the pytest-commit mailing list