| 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 |
| 11 import os | 11 import os |
| 12 import re | |
| 13 import socket | 12 import socket |
| 14 import traceback | 13 import traceback |
| 15 | 14 |
| 16 import cmd_helper | 15 import cmd_helper |
| 17 import constants | 16 import constants |
| 18 | 17 |
| 19 | 18 |
| 20 # The following two methods are used to allocate the port source for various | 19 # The following two methods are used to allocate the port source for various |
| 21 # types of test servers. Because some net-related tests can be run on shards at | 20 # types of test servers. Because some net-related tests can be run on shards at |
| 22 # same time, it's important to have a mechanism to allocate the port | 21 # same time, it's important to have a mechanism to allocate the port |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 logging.info('Allocate port %d for test server.', port) | 75 logging.info('Allocate port %d for test server.', port) |
| 77 else: | 76 else: |
| 78 logging.error('Could not allocate port for test server. ' | 77 logging.error('Could not allocate port for test server. ' |
| 79 'List of ports tried: %s', str(ports_tried)) | 78 'List of ports tried: %s', str(ports_tried)) |
| 80 return port | 79 return port |
| 81 | 80 |
| 82 | 81 |
| 83 def IsHostPortUsed(host_port): | 82 def IsHostPortUsed(host_port): |
| 84 """Checks whether the specified host port is used or not. | 83 """Checks whether the specified host port is used or not. |
| 85 | 84 |
| 86 Uses -n -P to inhibit the conversion of host/port numbers to host/port names. | |
| 87 | |
| 88 Args: | 85 Args: |
| 89 host_port: Port on host we want to check. | 86 host_port: Port on host we want to check. |
| 90 | 87 |
| 91 Returns: | 88 Returns: |
| 92 True if the port on host is already used, otherwise returns False. | 89 True if the port on host is already used, otherwise returns False. |
| 93 """ | 90 """ |
| 94 port_info = '(\*)|(127\.0\.0\.1)|(localhost):%d' % host_port | 91 try: |
| 95 # TODO(jnd): Find a better way to filter the port. Note that connecting to the | 92 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 96 # socket and closing it would leave it in the TIME_WAIT state. Setting | 93 sock.connect(('127.0.0.1', host_port)) |
| 97 # SO_LINGER on it and then closing it makes the Python HTTP server crash. | |
| 98 re_port = re.compile(port_info, re.MULTILINE) | |
| 99 if re_port.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])): | |
| 100 return True | 94 return True |
| 101 return False | 95 except socket.error as error: |
| 96 return False |
| 97 finally: |
| 98 sock.close() |
| 102 | 99 |
| 103 | 100 |
| 104 def IsDevicePortUsed(adb, device_port, state=''): | 101 def IsDevicePortUsed(adb, device_port, state=''): |
| 105 """Checks whether the specified device port is used or not. | 102 """Checks whether the specified device port is used or not. |
| 106 | 103 |
| 107 Args: | 104 Args: |
| 108 adb: Instance of AndroidCommands for talking to the device. | 105 adb: Instance of AndroidCommands for talking to the device. |
| 109 device_port: Port on device we want to check. | 106 device_port: Port on device we want to check. |
| 110 state: String of the specified state. Default is empty string, which | 107 state: String of the specified state. Default is empty string, which |
| 111 means any state. | 108 means any state. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 client_error = ('Bad response: %s %s version %s\n ' % | 164 client_error = ('Bad response: %s %s version %s\n ' % |
| 168 (r.status, r.reason, r.version) + | 165 (r.status, r.reason, r.version) + |
| 169 '\n '.join([': '.join(h) for h in r.getheaders()])) | 166 '\n '.join([': '.join(h) for h in r.getheaders()])) |
| 170 except (httplib.HTTPException, socket.error) as e: | 167 except (httplib.HTTPException, socket.error) as e: |
| 171 # Probably too quick connecting: try again. | 168 # Probably too quick connecting: try again. |
| 172 exception_error_msgs = traceback.format_exception_only(type(e), e) | 169 exception_error_msgs = traceback.format_exception_only(type(e), e) |
| 173 if exception_error_msgs: | 170 if exception_error_msgs: |
| 174 client_error = ''.join(exception_error_msgs) | 171 client_error = ''.join(exception_error_msgs) |
| 175 # Only returns last client_error. | 172 # Only returns last client_error. |
| 176 return (False, client_error or 'Timeout') | 173 return (False, client_error or 'Timeout') |
| OLD | NEW |