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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 ports_tried = [] |
50 try: | 50 try: |
51 fp_lock = open(constants.TEST_SERVER_PORT_LOCKFILE, 'w') | 51 fp_lock = open(constants.TEST_SERVER_PORT_LOCKFILE, 'w') |
52 fcntl.flock(fp_lock, fcntl.LOCK_EX) | 52 fcntl.flock(fp_lock, fcntl.LOCK_EX) |
53 # Get current valid port and calculate next valid port. | 53 # Get current valid port and calculate next valid port. |
54 assert os.path.exists(constants.TEST_SERVER_PORT_FILE) | 54 if not os.path.exists(constants.TEST_SERVER_PORT_FILE): |
| 55 ResetTestServerPortAllocation() |
55 with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp: | 56 with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp: |
56 port = int(fp.read()) | 57 port = int(fp.read()) |
57 ports_tried.append(port) | 58 ports_tried.append(port) |
58 while IsHostPortUsed(port): | 59 while IsHostPortUsed(port): |
59 port += 1 | 60 port += 1 |
60 ports_tried.append(port) | 61 ports_tried.append(port) |
61 if (port > constants.TEST_SERVER_PORT_LAST or | 62 if (port > constants.TEST_SERVER_PORT_LAST or |
62 port < constants.TEST_SERVER_PORT_FIRST): | 63 port < constants.TEST_SERVER_PORT_FIRST): |
63 port = 0 | 64 port = 0 |
64 else: | 65 else: |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 client_error = ('Bad response: %s %s version %s\n ' % | 159 client_error = ('Bad response: %s %s version %s\n ' % |
159 (r.status, r.reason, r.version) + | 160 (r.status, r.reason, r.version) + |
160 '\n '.join([': '.join(h) for h in r.getheaders()])) | 161 '\n '.join([': '.join(h) for h in r.getheaders()])) |
161 except (httplib.HTTPException, socket.error) as e: | 162 except (httplib.HTTPException, socket.error) as e: |
162 # Probably too quick connecting: try again. | 163 # Probably too quick connecting: try again. |
163 exception_error_msgs = traceback.format_exception_only(type(e), e) | 164 exception_error_msgs = traceback.format_exception_only(type(e), e) |
164 if exception_error_msgs: | 165 if exception_error_msgs: |
165 client_error = ''.join(exception_error_msgs) | 166 client_error = ''.join(exception_error_msgs) |
166 # Only returns last client_error. | 167 # Only returns last client_error. |
167 return (False, client_error or 'Timeout') | 168 return (False, client_error or 'Timeout') |
OLD | NEW |