[pypy-svn] r59622 - in pypy/build/testrunner: . test

pedronis at codespeak.net pedronis at codespeak.net
Sat Nov 1 14:14:29 CET 2008


Author: pedronis
Date: Sat Nov  1 14:14:27 2008
New Revision: 59622

Modified:
   pypy/build/testrunner/runner.py
   pypy/build/testrunner/test/test_runner.py
Log:
posix support for timeouts in run itself, next windows



Modified: pypy/build/testrunner/runner.py
==============================================================================
--- pypy/build/testrunner/runner.py	(original)
+++ pypy/build/testrunner/runner.py	Sat Nov  1 14:14:27 2008
@@ -1,12 +1,36 @@
-import sys, os, signal, thread, Queue
+import sys, os, signal, thread, Queue, time
 import py
 from py.compat import subprocess, optparse
 
 
-def run(args, cwd, out):
+def _kill(pid, sig):
+    try:
+        os.kill(pid, sig)
+    except OSError:
+        pass
+
+def run(args, cwd, out, timeout=None):
     f = out.open('w')
     try:
-        return subprocess.call(args, cwd=str(cwd), stdout=f, stderr=f)
+        p = subprocess.Popen(args, cwd=str(cwd), stdout=f, stderr=f)
+        if timeout is None:
+            return p.wait()
+        else:
+            timedout = None
+            t0 = time.time()
+            while True:
+                returncode = p.poll()
+                if returncode is not None:
+                    return timedout or returncode
+                tnow = time.time()
+                if (tnow-t0) > timeout:
+                    if timedout:
+                        _kill(p.pid, signal.SIGKILL)
+                        return -999
+                    else:
+                        timedout = -999
+                        _kill(p.pid, signal.SIGTERM)
+                time.sleep(min(timeout, 10))
     finally:
         f.close()
 

Modified: pypy/build/testrunner/test/test_runner.py
==============================================================================
--- pypy/build/testrunner/test/test_runner.py	(original)
+++ pypy/build/testrunner/test/test_runner.py	Sat Nov  1 14:14:27 2008
@@ -13,7 +13,8 @@
         os.unlink(self.fn)
 
     def test_run(self):
-        res = runner.run([sys.executable, "-c", "print 42"], '.', py.path.local(self.fn))
+        res = runner.run([sys.executable, "-c", "print 42"], '.',
+                         py.path.local(self.fn))
         assert res == 0
         out = py.path.local(self.fn).read('r')
         assert out == "42\n"
@@ -28,6 +29,25 @@
         res = runner.run([sys.executable, "-c", "import os; os.kill(os.getpid(), 9)"], '.', py.path.local(self.fn))
         assert res == -9
 
+    def test_timeout(self):
+        res = runner.run([sys.executable, "-c", "while True: pass"], '.', py.path.local(self.fn), timeout=3)
+        assert res == -999
+
+    def test_timeout_lock(self):
+        res = runner.run([sys.executable, "-c", "import threading; l=threading.Lock(); l.acquire(); l.acquire()"], '.', py.path.local(self.fn), timeout=3)
+        assert res == -999
+
+    def test_timeout_syscall(self):
+        res = runner.run([sys.executable, "-c", "import socket; s=s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind(('', 0)); s.recv(1000)"], '.', py.path.local(self.fn), timeout=3)
+        assert res == -999        
+
+    def test_timeout_success(self):
+        res = runner.run([sys.executable, "-c", "print 42"], '.',
+                         py.path.local(self.fn), timeout=2)
+        assert res == 0
+        out = py.path.local(self.fn).read('r')
+        assert out == "42\n"        
+
 
 class TestExecuteTest(object):
 



More information about the Pypy-commit mailing list