OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Functions that deals with local and device ports.""" | 5 """Functions that deals with local and device ports.""" |
6 | 6 |
7 import contextlib | 7 import contextlib |
8 import fcntl | 8 import fcntl |
9 import httplib | 9 import httplib |
10 import logging | 10 import logging |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 | 40 |
41 def AllocateTestServerPort(): | 41 def AllocateTestServerPort(): |
42 """Allocate a port incrementally. | 42 """Allocate a port incrementally. |
43 | 43 |
44 Returns: | 44 Returns: |
45 Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and | 45 Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and |
46 TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used. | 46 TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used. |
47 """ | 47 """ |
48 port = 0 | 48 port = 0 |
| 49 ports_tried = [] |
49 try: | 50 try: |
50 fp_lock = open(constants.TEST_SERVER_PORT_LOCKFILE, 'w') | 51 fp_lock = open(constants.TEST_SERVER_PORT_LOCKFILE, 'w') |
51 fcntl.flock(fp_lock, fcntl.LOCK_EX) | 52 fcntl.flock(fp_lock, fcntl.LOCK_EX) |
52 # Get current valid port and calculate next valid port. | 53 # Get current valid port and calculate next valid port. |
53 assert os.path.exists(constants.TEST_SERVER_PORT_FILE) | 54 assert os.path.exists(constants.TEST_SERVER_PORT_FILE) |
54 with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp: | 55 with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp: |
55 port = int(fp.read()) | 56 port = int(fp.read()) |
| 57 ports_tried.append(port) |
56 while IsHostPortUsed(port): | 58 while IsHostPortUsed(port): |
57 port += 1 | 59 port += 1 |
| 60 ports_tried.append(port) |
58 if (port > constants.TEST_SERVER_PORT_LAST or | 61 if (port > constants.TEST_SERVER_PORT_LAST or |
59 port < constants.TEST_SERVER_PORT_FIRST): | 62 port < constants.TEST_SERVER_PORT_FIRST): |
60 port = 0 | 63 port = 0 |
61 else: | 64 else: |
62 fp.seek(0, os.SEEK_SET) | 65 fp.seek(0, os.SEEK_SET) |
63 fp.write('%d' % (port + 1)) | 66 fp.write('%d' % (port + 1)) |
64 except Exception as e: | 67 except Exception as e: |
65 logging.info(e) | 68 logging.info(e) |
66 finally: | 69 finally: |
67 if fp_lock: | 70 if fp_lock: |
68 fcntl.flock(fp_lock, fcntl.LOCK_UN) | 71 fcntl.flock(fp_lock, fcntl.LOCK_UN) |
69 fp_lock.close() | 72 fp_lock.close() |
70 logging.info('Allocate port %d for test server.', port) | 73 if port: |
| 74 logging.info('Allocate port %d for test server.', port) |
| 75 else: |
| 76 logging.error('Could not allocate port for test server. ' |
| 77 'List of ports tried: %s', str(ports_tried)) |
71 return port | 78 return port |
72 | 79 |
73 | 80 |
74 def IsHostPortUsed(host_port): | 81 def IsHostPortUsed(host_port): |
75 """Checks whether the specified host port is used or not. | 82 """Checks whether the specified host port is used or not. |
76 | 83 |
77 Uses -n -P to inhibit the conversion of host/port numbers to host/port names. | 84 Uses -n -P to inhibit the conversion of host/port numbers to host/port names. |
78 | 85 |
79 Args: | 86 Args: |
80 host_port: Port on host we want to check. | 87 host_port: Port on host we want to check. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 client_error = ('Bad response: %s %s version %s\n ' % | 158 client_error = ('Bad response: %s %s version %s\n ' % |
152 (r.status, r.reason, r.version) + | 159 (r.status, r.reason, r.version) + |
153 '\n '.join([': '.join(h) for h in r.getheaders()])) | 160 '\n '.join([': '.join(h) for h in r.getheaders()])) |
154 except (httplib.HTTPException, socket.error) as e: | 161 except (httplib.HTTPException, socket.error) as e: |
155 # Probably too quick connecting: try again. | 162 # Probably too quick connecting: try again. |
156 exception_error_msgs = traceback.format_exception_only(type(e), e) | 163 exception_error_msgs = traceback.format_exception_only(type(e), e) |
157 if exception_error_msgs: | 164 if exception_error_msgs: |
158 client_error = ''.join(exception_error_msgs) | 165 client_error = ''.join(exception_error_msgs) |
159 # Only returns last client_error. | 166 # Only returns last client_error. |
160 return (False, client_error or 'Timeout') | 167 return (False, client_error or 'Timeout') |
OLD | NEW |