diff options
author | yuzo@chromium.org <yuzo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-13 03:42:26 +0000 |
---|---|---|
committer | yuzo@chromium.org <yuzo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-13 03:42:26 +0000 |
commit | 6cc98b91450830b511f2972b2e0491dc72437466 (patch) | |
tree | 403c20891fa80f4e37876f66d1d6f2d84aa40abf /webkit | |
parent | 4b6f2bd72042ee5906e5050432fd6059d86358c4 (diff) | |
download | chromium_src-6cc98b91450830b511f2972b2e0491dc72437466.zip chromium_src-6cc98b91450830b511f2972b2e0491dc72437466.tar.gz chromium_src-6cc98b91450830b511f2972b2e0491dc72437466.tar.bz2 |
Add scripts to start/stop PyWebSocket server that is used for testing Web Socket.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/267002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28790 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
5 files changed, 172 insertions, 3 deletions
diff --git a/webkit/tools/layout_tests/layout_package/path_utils.py b/webkit/tools/layout_tests/layout_package/path_utils.py index da0c705..88612ea 100644 --- a/webkit/tools/layout_tests/layout_package/path_utils.py +++ b/webkit/tools/layout_tests/layout_package/path_utils.py @@ -183,21 +183,26 @@ def FilenameToUri(full_path): """Convert a test file to a URI.""" LAYOUTTEST_HTTP_DIR = "LayoutTests/http/tests/" PENDING_HTTP_DIR = "pending/http/tests/" + LAYOUTTEST_WEBSOCKET_DIR = "LayoutTests/websocket/tests/" relative_path = _WinPathToUnix(RelativeTestFilename(full_path)) port = None use_ssl = False - # LayoutTests/http/tests/ run off port 8000 and ssl/ off 8443 if relative_path.startswith(LAYOUTTEST_HTTP_DIR): + # LayoutTests/http/tests/ run off port 8000 and ssl/ off 8443 relative_path = relative_path[len(LAYOUTTEST_HTTP_DIR):] port = 8000 - # pending/http/tests/ run off port 9000 and ssl/ off 9443 elif relative_path.startswith(PENDING_HTTP_DIR): + # pending/http/tests/ run off port 9000 and ssl/ off 9443 relative_path = relative_path[len(PENDING_HTTP_DIR):] port = 9000 - # chrome/http/tests run off of port 8081 with the full path + elif relative_path.startswith(LAYOUTTEST_WEBSOCKET_DIR): + # LayoutTests/websocket/tests/ run off port 8880 + relative_path = relative_path[len(LAYOUTTEST_WEBSOCKET_DIR):] + port = 8880 elif relative_path.find("/http/") >= 0: + # chrome/http/tests run off of port 8081 with the full path port = 8081 # Make LayoutTests/http/tests/local run as local files. This is to mimic the diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_linux.py b/webkit/tools/layout_tests/layout_package/platform_utils_linux.py index d507204..6b3501c 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_linux.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_linux.py @@ -117,6 +117,14 @@ def ShutDownHTTPServer(server_process): else: os.kill(server_process.pid, signal.SIGTERM) +def KillProcess(pid): + """Forcefully kill the process. + + Args: + pid: The id of the process to be killed. + """ + os.kill(pid, signal.SIGKILL) + def KillAllTestShells(): """Kills all instances of the test_shell binary currently running.""" subprocess.Popen(('killall', '-TERM', 'test_shell'), diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_mac.py b/webkit/tools/layout_tests/layout_package/platform_utils_mac.py index 61a7862..dd9b190 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_mac.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_mac.py @@ -107,6 +107,14 @@ def ShutDownHTTPServer(server_process): else: os.kill(server_process.pid, signal.SIGTERM) +def KillProcess(pid): + """Forcefully kill the process. + + Args: + pid: The id of the process to be killed. + """ + os.kill(pid, signal.SIGKILL) + def KillAllTestShells(): """Kills all instances of the test_shell binary currently running.""" subprocess.Popen(('killall', '-TERM', 'test_shell'), diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_win.py b/webkit/tools/layout_tests/layout_package/platform_utils_win.py index bec63fb..fca8c8b 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_win.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_win.py @@ -109,6 +109,16 @@ def ShutDownHTTPServer(server_process): stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait() +def KillProcess(pid): + """Forcefully kill the process. + + Args: + pid: The id of the process to be killed. + """ + subprocess.call(('taskkill.exe', '/f', '/pid', str(pid)), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + def KillAllTestShells(self): """Kills all instances of the test_shell binary currently running.""" subprocess.Popen(('taskkill.exe', '/f', '/im', 'test_shell.exe'), diff --git a/webkit/tools/layout_tests/layout_package/websocket_server.py b/webkit/tools/layout_tests/layout_package/websocket_server.py new file mode 100644 index 0000000..3554e0a --- /dev/null +++ b/webkit/tools/layout_tests/layout_package/websocket_server.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""A class to help start/stop the PyWebSocket server used by layout tests.""" + + +import logging +import optparse +import os +import subprocess +import sys +import tempfile +import time + +import path_utils +import platform_utils +import http_server + +# So we can import httpd_utils below to make ui_tests happy. +sys.path.append(path_utils.PathFromBase('tools', 'python')) +import google.httpd_utils + +_LOG_PREFIX = 'pywebsocket.log-' + +_DEFAULT_WS_PORT = 8880 +_DEFAULT_ROOT = '.' + + +def RemoveLogFiles(folder, starts_with): + files = os.listdir(folder) + for file in files: + if file.startswith(starts_with) : + full_path = os.path.join(folder, file) + os.remove(full_path) + +class PyWebSocketNotStarted(Exception): + pass + +class PyWebSocket(http_server.Lighttpd): + def __init__(self, output_dir, background=False, port=_DEFAULT_WS_PORT, + root=_DEFAULT_ROOT): + """Args: + output_dir: the absolute path to the layout test result directory + """ + self._output_dir = output_dir + self._process = None + self._port = port + self._root = root + if self._port: + self._port = int(self._port) + # Webkit tests + try: + self._webkit_tests = path_utils.PathFromBase( + 'third_party', 'WebKit', 'LayoutTests', 'websocket', 'tests') + except path_utils.PathNotFound: + self._webkit_tests = None + + def Start(self): + if not self._webkit_tests: + logging.info('No need to start PyWebSocket server.') + return + if self.IsRunning(): + raise PyWebSocketNotStarted('PyWebSocket is already running.') + + time_str = time.strftime('%d%b%Y-%H%M%S') + log_file_name = _LOG_PREFIX + time_str + '.txt' + error_log = os.path.join(self._output_dir, log_file_name) + + # Remove old log files. We only need to keep the last ones. + RemoveLogFiles(self._output_dir, _LOG_PREFIX) + + python_interp = sys.executable + pywebsocket_base = path_utils.PathFromBase('third_party', 'pywebsocket') + pywebsocket_script = path_utils.PathFromBase( + 'third_party', 'pywebsocket', 'mod_pywebsocket', 'standalone.py') + start_cmd = [ + python_interp, pywebsocket_script, + '-p', str(self._port), + '-d', self._webkit_tests, + ] + + # Put the cygwin directory first in the path to find cygwin1.dll + env = os.environ + if sys.platform in ('cygwin', 'win32'): + env['PATH'] = '%s;%s' % ( + path_utils.PathFromBase('third_party', 'cygwin', 'bin'), + env['PATH']) + + if sys.platform == 'win32' and self._register_cygwin: + setup_mount = path_utils.PathFromBase('third_party', 'cygwin', + 'setup_mount.bat') + subprocess.Popen(setup_mount).wait() + + env['PYTHONPATH'] = pywebsocket_base + os.path.pathsep + env['PYTHONPATH'] + + logging.info('Starting PyWebSocket server.') + self._process = subprocess.Popen(start_cmd, env=env) + + # Wait a bit before checking the liveness of the server. + time.sleep(0.5) + + url = 'http://127.0.0.1:%d/' % self._port + if not google.httpd_utils.UrlIsAlive(url): + raise PyWebSocketNotStarted( + 'Failed to start PyWebSocket server on port %s.' % self._port) + + # Our process terminated already + if self._process.returncode != None: + raise PyWebSocketNotStarted('Failed to start PyWebSocket server.') + + def Stop(self, force=False): + if not force and not self.IsRunning(): + return + + logging.info('Shutting down PyWebSocket server.') + platform_utils.KillProcess(self._process.pid) + self._process = None + + # Wait a bit to make sure the ports are free'd up + time.sleep(2) + + +if '__main__' == __name__: + # Provide some command line params for starting the PyWebSocket server + # manually. + option_parser = optparse.OptionParser() + option_parser.add_option('-p', '--port', dest='port', + default=_DEFAULT_WS_PORT, help='Port to listen on') + option_parser.add_option('-r', '--root', dest='root', default='.', + help='Absolute path to DocumentRoot') + options, args = option_parser.parse_args() + + pywebsocket = PyWebSocket(tempfile.gettempdir(), + port=options.port, + root=options.root) + pywebsocket.Start() |