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