Skip to content

Commit

Permalink
Clean up code duplication in layout test http_server.
Browse files Browse the repository at this point in the history
Refactored out UrlIsAlive to live outside the ApacheHttpd class and imported http_utils.py into http_server.py

Now they share common code.

BUG=6784
TEST=started and stopped http_server successfully.

Review URL: http://codereview.chromium.org/243022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28351 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
[email protected] committed Oct 7, 2009
1 parent 01c022a commit d9735af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 58 deletions.
52 changes: 26 additions & 26 deletions tools/python/google/httpd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@

class HttpdNotStarted(Exception): pass

def UrlIsAlive(url):
"""Checks to see if we get an http response from |url|.
We poll the url 5 times with a 1 second delay. If we don't
get a reply in that time, we give up and assume the httpd
didn't start properly.
Args:
url: The URL to check.
Return:
True if the url is alive.
"""
wait_time = 5
while wait_time > 0:
try:
response = urllib.urlopen(url)
# Server is up and responding.
return True
except IOError:
pass
wait_time -= 1
# Wait a second and try again.
time.sleep(1)

return False

def ApacheConfigDir(start_dir):
"""Returns a path to the directory holding the Apache config files."""
return google.path_utils.FindUpward(start_dir, 'tools', 'python',
Expand Down Expand Up @@ -122,34 +147,9 @@ def StartServer(self):

# Ensure that the server is running on all the desired ports.
for port in self._port_list:
if not self._UrlIsAlive('http://127.0.0.1:%s/' % str(port)):
if not UrlIsAlive('http://127.0.0.1:%s/' % str(port)):
raise HttpdNotStarted('Failed to start httpd on port %s' % str(port))

def _UrlIsAlive(self, url):
"""Checks to see if we get an http response from |url|.
We poll the url 5 times with a 1 second delay. If we don't
get a reply in that time, we give up and assume the httpd
didn't start properly.
Args:
url: The URL to check.
Return:
True if the url is alive.
"""
wait_time = 5
while wait_time > 0:
try:
response = urllib.urlopen(url)
# Server is up and responding.
return True
except IOError:
pass
wait_time -= 1
# Wait a second and try again.
time.sleep(1)

return False

def StopServer(self, force=False):
"""If we started an httpd.exe process, or if force is True, call
self._stop_command (passed in on init so it can be platform-dependent).
Expand Down
38 changes: 6 additions & 32 deletions webkit/tools/layout_tests/layout_package/http_server.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import urllib

import path_utils
import google.httpd_utils

def RemoveLogFiles(folder, starts_with):
files = os.listdir(folder)
Expand All @@ -25,9 +26,6 @@ def RemoveLogFiles(folder, starts_with):
full_path = os.path.join(folder, file)
os.remove(full_path)

class HttpdNotStarted(Exception):
pass

class Lighttpd:
# Webkit tests
try:
Expand Down Expand Up @@ -220,38 +218,14 @@ def Start(self):
for mapping in mappings:
url = 'http%s://127.0.0.1:%d/' % ('sslcert' in mapping and 's' or '',
mapping['port'])
if not self._UrlIsAlive(url):
raise HttpdNotStarted('Failed to start httpd on port %s' %
str(mapping['port']))
if not google.httpd_utils.UrlIsAlive(url):
raise google.httpd_utils.HttpdNotStarted('Failed to start httpd on ',
'port %s' %
str(mapping['port']))

# Our process terminated already
if self._process.returncode != None:
raise HttpdNotStarted('Failed to start httpd.')

def _UrlIsAlive(self, url):
"""Checks to see if we get an http response from |url|.
We poll the url 5 times with a 3 second delay. If we don't
get a reply in that time, we give up and assume the httpd
didn't start properly.
Args:
url: The URL to check.
Return:
True if the url is alive.
"""
attempts = 5
while attempts > 0:
try:
response = urllib.urlopen(url)
# Server is up and responding.
return True
except IOError:
pass
attempts -= 1
# Wait 3 seconds and try again.
time.sleep(3)

return False
raise google.httpd_utils.HttpdNotStarted('Failed to start httpd.')

# TODO(deanm): Find a nicer way to shutdown cleanly. Our log files are
# probably not being flushed, etc... why doesn't our python have os.kill ?
Expand Down

0 comments on commit d9735af

Please sign in to comment.