# HG changeset patch # User Vadim Gelfer # Date 1152211534 25200 # Node ID 83cfd95eafb50cd8ee66d8c3983b1cd50996135c # Parent 2264b2b077a1091f56335fcf1e30d9fefe3afa27 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. diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -2649,7 +2649,7 @@ def serve(ui, repo, **opts): 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']: diff --git a/tests/run-tests.py b/tests/run-tests.py --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -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 @@ def output_coverage(): 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 @@ def run(cmd): 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 @@ def run_one(test): 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 @@ def run_one(test): 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 @@ os.environ["HGRCPATH"] = "" 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: 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 diff --git a/tests/test-archive b/tests/test-archive --- a/tests/test-archive +++ b/tests/test-archive @@ -17,6 +17,7 @@ echo "[web]" >> .hg/hgrc 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" 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 diff --git a/tests/test-http-proxy b/tests/test-http-proxy --- a/tests/test-http-proxy +++ b/tests/test-http-proxy @@ -5,10 +5,12 @@ cd a 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 proxy.log 2>&1 proxy.pid) +cat proxy.pid >> $DAEMON_PIDS sleep 2 echo %% url for proxy @@ -26,5 +28,4 @@ http_proxy=http://user:passwd@localhost: 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 diff --git a/tests/test-incoming-outgoing b/tests/test-incoming-outgoing --- a/tests/test-incoming-outgoing +++ b/tests/test-incoming-outgoing @@ -9,6 +9,7 @@ for i in 0 1 2 3 4 5 6 7 8; do 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 @@ cd .. 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` diff --git a/tests/test-pull b/tests/test-pull --- a/tests/test-pull +++ b/tests/test-pull @@ -8,6 +8,7 @@ hg addremove 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 @@ hg co cat foo hg manifest hg pull - -kill `cat ../test/hg.pid` diff --git a/tests/test-push-http b/tests/test-push-http --- a/tests/test-push-http +++ b/tests/test-push-http @@ -15,6 +15,7 @@ cd ../test 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 % expect authorization error 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 @@ hg rollback 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` diff --git a/tests/test-webraw b/tests/test-webraw --- a/tests/test-webraw +++ b/tests/test-webraw @@ -11,6 +11,7 @@ ENDSOME 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