##// END OF EJS Templates
tests: pull common http server setup out of individual tests...
Mike Hommey -
r22959:10116463 default
parent child Browse files
Show More
@@ -0,0 +1,54 b''
1 #!/usr/bin/env python
2
3 """
4 Small and dumb HTTP server for use in tests.
5 """
6
7 from optparse import OptionParser
8 import BaseHTTPServer, SimpleHTTPServer, os, signal, subprocess, sys
9
10
11 def run(server_class=BaseHTTPServer.HTTPServer,
12 handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler,
13 server_address=('localhost', 8000)):
14 httpd = server_class(server_address, handler_class)
15 httpd.serve_forever()
16
17
18 if __name__ == '__main__':
19 parser = OptionParser()
20 parser.add_option('-p', '--port', dest='port', type='int', default=8000,
21 help='TCP port to listen on', metavar='PORT')
22 parser.add_option('-H', '--host', dest='host', default='localhost',
23 help='hostname or IP to listen on', metavar='HOST')
24 parser.add_option('--pid', dest='pid',
25 help='file name where the PID of the server is stored')
26 parser.add_option('-f', '--foreground', dest='foreground',
27 action='store_true',
28 help='do not start the HTTP server in the background')
29
30 (options, args) = parser.parse_args()
31
32 signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0))
33
34 if options.foreground and options.pid:
35 parser.error("options --pid and --foreground are mutually exclusive")
36
37 if options.foreground:
38 run(server_address=(options.host, options.port))
39 else:
40 # This doesn't attempt to cleanly detach the process, as it's not
41 # meant to be a long-lived, independent process. As a consequence,
42 # it's still part of the same process group, and keeps any file
43 # descriptors it might have inherited besided stdin/stdout/stderr.
44 # Trying to do things cleanly is more complicated, requires
45 # OS-dependent code, and is not worth the effort.
46 proc = subprocess.Popen([sys.executable, __file__, '-f',
47 '-H', options.host, '-p', str(options.port)],
48 stdin=open(os.devnull, 'r'),
49 stdout=open(os.devnull, 'w'),
50 stderr=subprocess.STDOUT)
51 if options.pid:
52 fp = file(options.pid, 'wb')
53 fp.write(str(proc.pid) + '\n')
54 fp.close()
@@ -1,4 +1,4 b''
1 #require serve
1 #require serve killdaemons
2 2
3 3 #if windows
4 4 $ hg clone http://localhost:$HGPORT/ copy
@@ -13,21 +13,9 b''
13 13 $ test -d copy
14 14 [1]
15 15
16 $ cat > dumb.py <<EOF
17 > import BaseHTTPServer, SimpleHTTPServer, os, signal
18 > def run(server_class=BaseHTTPServer.HTTPServer,
19 > handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler):
20 > server_address = ('localhost', int(os.environ['HGPORT']))
21 > httpd = server_class(server_address, handler_class)
22 > open("listening", "w")
23 > httpd.handle_request()
24 > run()
25 > EOF
26
27 $ python dumb.py 2> log &
28 $ P=$!
29 $ while [ ! -f listening ]; do sleep 0; done
16 $ python "$TESTDIR/dumbhttp.py" -p $HGPORT --pid dumb.pid
17 $ cat dumb.pid >> $DAEMON_PIDS
30 18 $ hg clone http://localhost:$HGPORT/foo copy2
31 19 abort: HTTP Error 404: * (glob)
32 20 [255]
33 $ wait $P
21 $ "$TESTDIR/killdaemons.py" $DAEMON_PIDS
@@ -15,36 +15,7 b''
15 15 This server doesn't do range requests so it's basically only good for
16 16 one pull
17 17
18 $ cat > dumb.py <<EOF
19 > import BaseHTTPServer, SimpleHTTPServer, os, signal, sys
20 >
21 > def run(server_class=BaseHTTPServer.HTTPServer,
22 > handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler):
23 > server_address = ('localhost', int(os.environ['HGPORT']))
24 > httpd = server_class(server_address, handler_class)
25 > httpd.serve_forever()
26 >
27 > signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0))
28 > fp = file('dumb.pid', 'wb')
29 > fp.write(str(os.getpid()) + '\n')
30 > fp.close()
31 > run()
32 > EOF
33 $ python dumb.py 2>/dev/null &
34
35 Cannot just read $!, it will not be set to the right value on Windows/MinGW
36
37 $ cat > wait.py <<EOF
38 > import time
39 > while True:
40 > try:
41 > if '\n' in file('dumb.pid', 'rb').read():
42 > break
43 > except IOError:
44 > pass
45 > time.sleep(0.2)
46 > EOF
47 $ python wait.py
18 $ python "$TESTDIR/dumbhttp.py" -p $HGPORT --pid dumb.pid
48 19 $ cat dumb.pid >> $DAEMON_PIDS
49 20 $ hg init remote
50 21 $ cd remote
General Comments 0
You need to be logged in to leave comments. Login now