| 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 deal with local and device ports.""" | 5 """Functions that deal 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 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 Returns: | 31 Returns: |
| 32 Returns True if reset successes. Otherwise returns False. | 32 Returns True if reset successes. Otherwise returns False. |
| 33 """ | 33 """ |
| 34 try: | 34 try: |
| 35 with open(_TEST_SERVER_PORT_FILE, 'w') as fp: | 35 with open(_TEST_SERVER_PORT_FILE, 'w') as fp: |
| 36 fp.write('%d' % _TEST_SERVER_PORT_FIRST) | 36 fp.write('%d' % _TEST_SERVER_PORT_FIRST) |
| 37 if os.path.exists(_TEST_SERVER_PORT_LOCKFILE): | 37 if os.path.exists(_TEST_SERVER_PORT_LOCKFILE): |
| 38 os.unlink(_TEST_SERVER_PORT_LOCKFILE) | 38 os.unlink(_TEST_SERVER_PORT_LOCKFILE) |
| 39 return True | 39 return True |
| 40 except Exception as e: | 40 except Exception: # pylint: disable=broad-except |
| 41 logging.error(e) | 41 logging.exception('Error while resetting port allocation') |
| 42 return False | 42 return False |
| 43 | 43 |
| 44 | 44 |
| 45 def AllocateTestServerPort(): | 45 def AllocateTestServerPort(): |
| 46 """Allocates a port incrementally. | 46 """Allocates a port incrementally. |
| 47 | 47 |
| 48 Returns: | 48 Returns: |
| 49 Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and | 49 Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and |
| 50 TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used. | 50 TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used. |
| 51 """ | 51 """ |
| (...skipping 10 matching lines...) Expand all Loading... |
| 62 ports_tried.append(port) | 62 ports_tried.append(port) |
| 63 while not IsHostPortAvailable(port): | 63 while not IsHostPortAvailable(port): |
| 64 port += 1 | 64 port += 1 |
| 65 ports_tried.append(port) | 65 ports_tried.append(port) |
| 66 if (port > _TEST_SERVER_PORT_LAST or | 66 if (port > _TEST_SERVER_PORT_LAST or |
| 67 port < _TEST_SERVER_PORT_FIRST): | 67 port < _TEST_SERVER_PORT_FIRST): |
| 68 port = 0 | 68 port = 0 |
| 69 else: | 69 else: |
| 70 fp.seek(0, os.SEEK_SET) | 70 fp.seek(0, os.SEEK_SET) |
| 71 fp.write('%d' % (port + 1)) | 71 fp.write('%d' % (port + 1)) |
| 72 except Exception as e: | 72 except Exception: # pylint: disable=broad-except |
| 73 logging.error(e) | 73 logging.exception('ERror while allocating port') |
| 74 finally: | 74 finally: |
| 75 if fp_lock: | 75 if fp_lock: |
| 76 fcntl.flock(fp_lock, fcntl.LOCK_UN) | 76 fcntl.flock(fp_lock, fcntl.LOCK_UN) |
| 77 fp_lock.close() | 77 fp_lock.close() |
| 78 if port: | 78 if port: |
| 79 logging.info('Allocate port %d for test server.', port) | 79 logging.info('Allocate port %d for test server.', port) |
| 80 else: | 80 else: |
| 81 logging.error('Could not allocate port for test server. ' | 81 logging.error('Could not allocate port for test server. ' |
| 82 'List of ports tried: %s', str(ports_tried)) | 82 'List of ports tried: %s', str(ports_tried)) |
| 83 return port | 83 return port |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 client_error = ('Bad response: %s %s version %s\n ' % | 169 client_error = ('Bad response: %s %s version %s\n ' % |
| 170 (r.status, r.reason, r.version) + | 170 (r.status, r.reason, r.version) + |
| 171 '\n '.join([': '.join(h) for h in r.getheaders()])) | 171 '\n '.join([': '.join(h) for h in r.getheaders()])) |
| 172 except (httplib.HTTPException, socket.error) as e: | 172 except (httplib.HTTPException, socket.error) as e: |
| 173 # Probably too quick connecting: try again. | 173 # Probably too quick connecting: try again. |
| 174 exception_error_msgs = traceback.format_exception_only(type(e), e) | 174 exception_error_msgs = traceback.format_exception_only(type(e), e) |
| 175 if exception_error_msgs: | 175 if exception_error_msgs: |
| 176 client_error = ''.join(exception_error_msgs) | 176 client_error = ''.join(exception_error_msgs) |
| 177 # Only returns last client_error. | 177 # Only returns last client_error. |
| 178 return (False, client_error or 'Timeout') | 178 return (False, client_error or 'Timeout') |
| OLD | NEW |