| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """A "Test Server Spawner" that handles killing/stopping per-test test servers. | 5 """A "Test Server Spawner" that handles killing/stopping per-test test servers. |
| 6 | 6 |
| 7 It's used to accept requests from the device to spawn and kill instances of the | 7 It's used to accept requests from the device to spawn and kill instances of the |
| 8 chrome test server on the host. | 8 chrome test server on the host. |
| 9 """ | 9 """ |
| 10 # pylint: disable=W0702 | |
| 11 | 10 |
| 12 import BaseHTTPServer | 11 import BaseHTTPServer |
| 13 import json | 12 import json |
| 14 import logging | 13 import logging |
| 15 import os | 14 import os |
| 16 import select | 15 import select |
| 17 import struct | 16 import struct |
| 18 import subprocess | 17 import subprocess |
| 19 import sys | 18 import sys |
| 20 import threading | 19 import threading |
| 21 import time | 20 import time |
| 22 import urlparse | 21 import urlparse |
| 23 | 22 |
| 24 from pylib import constants | 23 import constants |
| 25 from pylib import ports | 24 import ports |
| 26 | 25 |
| 27 from pylib.forwarder import Forwarder | 26 from pylib.forwarder import Forwarder |
| 28 | 27 |
| 29 | |
| 30 # Path that are needed to import necessary modules when launching a testserver. | 28 # Path that are needed to import necessary modules when launching a testserver. |
| 31 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' | 29 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' |
| 32 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), | 30 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), |
| 33 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), | 31 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), |
| 34 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', | 32 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', |
| 35 'src'), | 33 'src'), |
| 36 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), | 34 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), |
| 37 os.path.join(constants.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) | 35 os.path.join(constants.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) |
| 38 | 36 |
| 39 | 37 |
| 40 SERVER_TYPES = { | 38 SERVER_TYPES = { |
| 41 'http': '', | 39 'http': '', |
| 42 'ftp': '-f', | 40 'ftp': '-f', |
| 43 'sync': '', # Sync uses its own script, and doesn't take a server type arg. | 41 'sync': '', # Sync uses its own script, and doesn't take a server type arg. |
| 44 'tcpecho': '--tcp-echo', | 42 'tcpecho': '--tcp-echo', |
| 45 'udpecho': '--udp-echo', | 43 'udpecho': '--udp-echo', |
| 46 } | 44 } |
| 47 | 45 |
| 48 | 46 |
| 49 # The timeout (in seconds) of starting up the Python test server. | 47 # The timeout (in seconds) of starting up the Python test server. |
| 50 TEST_SERVER_STARTUP_TIMEOUT = 10 | 48 TEST_SERVER_STARTUP_TIMEOUT = 10 |
| 51 | 49 |
| 52 def _WaitUntil(predicate, max_attempts=5): | 50 def _WaitUntil(predicate, max_attempts=5): |
| 53 """Blocks until the provided predicate (function) is true. | 51 """Blocks until the provided predicate (function) is true. |
| 54 | 52 |
| 55 Returns: | 53 Returns: |
| 56 Whether the provided predicate was satisfied once (before the timeout). | 54 Whether the provided predicate was satisfied once (before the timeout). |
| 57 """ | 55 """ |
| 58 sleep_time_sec = 0.025 | 56 sleep_time_sec = 0.025 |
| 59 for _ in xrange(1, max_attempts): | 57 for attempt in xrange(1, max_attempts): |
| 60 if predicate(): | 58 if predicate(): |
| 61 return True | 59 return True |
| 62 time.sleep(sleep_time_sec) | 60 time.sleep(sleep_time_sec) |
| 63 sleep_time_sec = min(1, sleep_time_sec * 2) # Don't wait more than 1 sec. | 61 sleep_time_sec = min(1, sleep_time_sec * 2) # Don't wait more than 1 sec. |
| 64 return False | 62 return False |
| 65 | 63 |
| 66 | 64 |
| 67 def _CheckPortStatus(port, expected_status): | 65 def _CheckPortStatus(port, expected_status): |
| 68 """Returns True if port has expected_status. | 66 """Returns True if port has expected_status. |
| 69 | 67 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 self.tool = tool | 119 self.tool = tool |
| 122 self.test_server_process = None | 120 self.test_server_process = None |
| 123 self.is_ready = False | 121 self.is_ready = False |
| 124 self.host_port = self.arguments['port'] | 122 self.host_port = self.arguments['port'] |
| 125 assert isinstance(self.host_port, int) | 123 assert isinstance(self.host_port, int) |
| 126 # The forwarder device port now is dynamically allocated. | 124 # The forwarder device port now is dynamically allocated. |
| 127 self.forwarder_device_port = 0 | 125 self.forwarder_device_port = 0 |
| 128 # Anonymous pipe in order to get port info from test server. | 126 # Anonymous pipe in order to get port info from test server. |
| 129 self.pipe_in = None | 127 self.pipe_in = None |
| 130 self.pipe_out = None | 128 self.pipe_out = None |
| 131 self.process = None | |
| 132 self.command_line = [] | 129 self.command_line = [] |
| 133 | 130 |
| 134 def _WaitToStartAndGetPortFromTestServer(self): | 131 def _WaitToStartAndGetPortFromTestServer(self): |
| 135 """Waits for the Python test server to start and gets the port it is using. | 132 """Waits for the Python test server to start and gets the port it is using. |
| 136 | 133 |
| 137 The port information is passed by the Python test server with a pipe given | 134 The port information is passed by the Python test server with a pipe given |
| 138 by self.pipe_out. It is written as a result to |self.host_port|. | 135 by self.pipe_out. It is written as a result to |self.host_port|. |
| 139 | 136 |
| 140 Returns: | 137 Returns: |
| 141 Whether the port used by the test server was successfully fetched. | 138 Whether the port used by the test server was successfully fetched. |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 427 |
| 431 def CleanupState(self): | 428 def CleanupState(self): |
| 432 """Cleans up the spawning server state. | 429 """Cleans up the spawning server state. |
| 433 | 430 |
| 434 This should be called if the test server spawner is reused, | 431 This should be called if the test server spawner is reused, |
| 435 to avoid sharing the test server instance. | 432 to avoid sharing the test server instance. |
| 436 """ | 433 """ |
| 437 if self.server.test_server_instance: | 434 if self.server.test_server_instance: |
| 438 self.server.test_server_instance.Stop() | 435 self.server.test_server_instance.Stop() |
| 439 self.server.test_server_instance = None | 436 self.server.test_server_instance = None |
| OLD | NEW |