changeset 2571:83cfd95eafb5

tests: add timeouts, make run-tests.py clean up dead daemon processes test timeout feature is needed for test with python 2.5 beta. if test does not complete in time (30 seconds is default), it is killed. some times daemon process used in test can be alive after the test is killed by user or by timeout. tests now record daemon pids into $DAEMON_PIDS and run-tests.py kills all living daemons after every test. final little change is to add newline to end of pid file printed by "hg serve", else "cat hg.pid >> $DAEMON_FILES" gives garbage.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Thu, 06 Jul 2006 11:45:34 -0700
parents 2264b2b077a1
children d22d730c96ed
files mercurial/commands.py tests/run-tests.py tests/test-archive tests/test-http-proxy tests/test-incoming-outgoing tests/test-pull tests/test-push-http tests/test-webraw
diffstat 8 files changed, 86 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Jul 06 10:09:24 2006 -0700
+++ b/mercurial/commands.py	Thu Jul 06 11:45:34 2006 -0700
@@ -2649,7 +2649,7 @@
 
     if opts['pid_file']:
         fp = open(opts['pid_file'], 'w')
-        fp.write(str(os.getpid()))
+        fp.write(str(os.getpid()) + '\n')
         fp.close()
 
     if opts['daemon_pipefds']:
--- a/tests/run-tests.py	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/run-tests.py	Thu Jul 06 11:45:34 2006 -0700
@@ -7,23 +7,32 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import os, sys, shutil, re
+import difflib
+import errno
+import optparse
+import os
+import popen2
+import re
+import shutil
+import signal
+import sys
 import tempfile
-import difflib
-import popen2
-from optparse import OptionParser
+import time
 
 required_tools = ["python", "diff", "grep", "unzip", "gunzip", "bunzip2", "sed"]
 
-parser = OptionParser("%prog [options] [tests]")
+parser = optparse.OptionParser("%prog [options] [tests]")
 parser.add_option("-v", "--verbose", action="store_true",
     help="output verbose messages")
+parser.add_option("-t", "--timeout", type="int",
+    help="output verbose messages")
 parser.add_option("-c", "--cover", action="store_true",
     help="print a test coverage report")
 parser.add_option("-s", "--cover_stdlib", action="store_true",
     help="print a test coverage report inc. standard libraries")
 parser.add_option("-C", "--annotate", action="store_true",
     help="output files annotated with coverage")
+parser.set_defaults(timeout=30)
 (options, args) = parser.parse_args()
 verbose = options.verbose
 coverage = options.cover or options.cover_stdlib or options.annotate
@@ -159,6 +168,12 @@
         vlog("# Running: "+cmd)
         os.system(cmd)
 
+class Timeout(Exception):
+    pass
+
+def alarmed(signum, frame):
+    raise Timeout
+
 def run(cmd):
     """Run command in a sub-process, capturing the output (stdout and stderr).
     Return the exist code, and output."""
@@ -172,9 +187,17 @@
             ret = 0
     else:
         proc = popen2.Popen4(cmd)
-        proc.tochild.close()
-        output = proc.fromchild.read()
-        ret = proc.wait()
+        try:
+            output = ''
+            proc.tochild.close()
+            output = proc.fromchild.read()
+            ret = proc.wait()
+        except Timeout:
+            vlog('# Process %d timed out - killing it' % proc.pid)
+            os.kill(proc.pid, signal.SIGTERM)
+            ret = proc.wait()
+            if ret == 0:
+                ret = signal.SIGTERM << 8
     return ret, splitnewlines(output)
 
 def run_one(test):
@@ -204,10 +227,16 @@
     if os.name == 'nt' and test.endswith(".bat"):
         cmd = 'cmd /c call "%s"' % (os.path.join(TESTDIR, test))
 
+    if options.timeout > 0:
+        signal.alarm(options.timeout)
+
     vlog("# Running", cmd)
     ret, out = run(cmd)
     vlog("# Ret was:", ret)
 
+    if options.timeout > 0:
+        signal.alarm(0)
+
     diffret = 0
     # If reference output file exists, check test output against it
     if os.path.exists(ref):
@@ -231,6 +260,30 @@
             f.write(line)
         f.close()
 
+    # Kill off any leftover daemon processes
+    try:
+        fp = file(DAEMON_PIDS)
+        for line in fp:
+            try:
+                pid = int(line)
+            except ValueError:
+                continue
+            try:
+                os.kill(pid, 0)
+                vlog('# Killing daemon process %d' % pid)
+                os.kill(pid, signal.SIGTERM)
+                time.sleep(0.25)
+                os.kill(pid, 0)
+                vlog('# Daemon process %d is stuck - really killing it' % pid)
+                os.kill(pid, signal.SIGKILL)
+            except OSError, err:
+                if err.errno != errno.ESRCH:
+                    raise
+        fp.close()
+        os.unlink(DAEMON_PIDS)
+    except IOError:
+        pass
+
     os.chdir(TESTDIR)
     shutil.rmtree(tmpd, True)
     return ret == 0
@@ -252,6 +305,8 @@
 
 TESTDIR = os.environ["TESTDIR"] = os.getcwd()
 HGTMP   = os.environ["HGTMP"]   = tempfile.mkdtemp("", "hgtests.")
+DAEMON_PIDS = os.environ["DAEMON_PIDS"] = os.path.join(HGTMP, 'daemon.pids')
+
 vlog("# Using TESTDIR", TESTDIR)
 vlog("# Using HGTMP", HGTMP)
 
@@ -264,6 +319,15 @@
     try:
         install_hg()
 
+        if options.timeout > 0:
+            try:
+                signal.signal(signal.SIGALRM, alarmed)
+                vlog('# Running tests with %d-second timeout' %
+                     options.timeout)
+            except AttributeError:
+                print 'WARNING: cannot run tests with timeouts'
+                options.timeout = 0
+
         tests = 0
         failed = 0
 
--- a/tests/test-archive	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-archive	Thu Jul 06 11:45:34 2006 -0700
@@ -17,6 +17,7 @@
 echo "name = test-archive" >> .hg/hgrc
 echo "allow_archive = gz bz2, zip" >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 
 TIP=`hg id -v | cut -f1 -d' '`
 QTIP=`hg id -q`
@@ -32,9 +33,6 @@
 http_proxy= python getarchive.py "$TIP" zip > archive.zip
 unzip -t archive.zip | sed "s/$QTIP/TIP/"
 
-kill `cat hg.pid`
-sleep 1 # wait for server to scream and die
-
 hg archive -t tar test.tar
 tar tf test.tar
 
--- a/tests/test-http-proxy	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-http-proxy	Thu Jul 06 11:45:34 2006 -0700
@@ -5,10 +5,12 @@
 echo a > a
 hg ci -Ama -d '1123456789 0'
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 
 cd ..
-("$TESTDIR/tinyproxy.py" 20060 localhost >/dev/null 2>&1 </dev/null &
+("$TESTDIR/tinyproxy.py" 20060 localhost >proxy.log 2>&1 </dev/null &
 echo $! > proxy.pid)
+cat proxy.pid >> $DAEMON_PIDS
 sleep 2
 
 echo %% url for proxy
@@ -26,5 +28,4 @@
 echo %% bad host:port for proxy
 http_proxy=localhost:20061 hg clone --config http_proxy.always=True http://localhost:20059/ f
 
-kill `cat proxy.pid a/hg.pid`
 exit 0
--- a/tests/test-incoming-outgoing	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-incoming-outgoing	Thu Jul 06 11:45:34 2006 -0700
@@ -9,6 +9,7 @@
 done
 hg verify
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 cd ..
 
 hg init new
@@ -45,5 +46,3 @@
 hg -R test-dev outgoing test
 http_proxy= hg -R test-dev outgoing http://localhost:20059/
 http_proxy= hg -R test-dev outgoing -r 11 http://localhost:20059/
-
-kill `cat test/hg.pid`
--- a/tests/test-pull	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-pull	Thu Jul 06 11:45:34 2006 -0700
@@ -8,6 +8,7 @@
 hg commit -m 1
 hg verify
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 cd ..
 
 http_proxy= hg clone http://localhost:20059/ copy
@@ -17,5 +18,3 @@
 cat foo
 hg manifest
 hg pull
-
-kill `cat ../test/hg.pid`
--- a/tests/test-push-http	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-push-http	Thu Jul 06 11:45:34 2006 -0700
@@ -15,6 +15,7 @@
 
 echo % expect ssl error
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
 
@@ -22,18 +23,21 @@
 echo '[web]' > .hg/hgrc
 echo 'push_ssl = false' >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
 
 echo % expect authorization error: must have authorized user
 echo 'allow_push = unperson' >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
 
 echo % expect success
 echo 'allow_push = *' >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
 hg rollback
@@ -41,11 +45,13 @@
 echo % expect authorization error: all users denied
 echo 'deny_push = *' >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
 
 echo % expect authorization error: some users denied, users must be authenticated
 echo 'deny_push = unperson' >> .hg/hgrc
 hg serve -p 20059 -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 hg --cwd ../test2 push http://localhost:20059/
 kill `cat hg.pid`
--- a/tests/test-webraw	Thu Jul 06 10:09:24 2006 -0700
+++ b/tests/test-webraw	Thu Jul 06 11:45:34 2006 -0700
@@ -11,6 +11,7 @@
 hg add sometext.txt
 hg commit -d "1 0" -m "Just some text"
 hg serve -p 20059 -A access.log -E error.log -d --pid-file=hg.pid
+cat hg.pid >> $DAEMON_PIDS
 ("$TESTDIR/get-with-headers.py" localhost:20059 '/?f=f165dc289438;file=sometext.txt;style=raw' content-type content-length content-disposition) >getoutput.txt &
 
 sleep 5