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 | 10 |
11 import BaseHTTPServer | 11 import BaseHTTPServer |
12 import json | 12 import json |
13 import logging | 13 import logging |
14 import os | 14 import os |
15 import select | 15 import select |
16 import struct | 16 import struct |
17 import subprocess | 17 import subprocess |
18 import sys | 18 import sys |
19 import threading | 19 import threading |
20 import time | 20 import time |
21 import urlparse | 21 import urlparse |
22 | 22 |
23 import constants | 23 from pylib import constants |
24 import ports | 24 from pylib import ports |
25 | 25 |
26 from pylib.forwarder import Forwarder | 26 from pylib.forwarder import Forwarder |
27 | 27 |
28 # 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. |
29 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' |
30 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), | 30 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), |
31 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), | 31 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), |
32 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', | 32 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', |
33 'src'), | 33 'src'), |
34 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), | 34 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), |
(...skipping 12 matching lines...) Expand all Loading... |
47 # The timeout (in seconds) of starting up the Python test server. | 47 # The timeout (in seconds) of starting up the Python test server. |
48 TEST_SERVER_STARTUP_TIMEOUT = 10 | 48 TEST_SERVER_STARTUP_TIMEOUT = 10 |
49 | 49 |
50 def _WaitUntil(predicate, max_attempts=5): | 50 def _WaitUntil(predicate, max_attempts=5): |
51 """Blocks until the provided predicate (function) is true. | 51 """Blocks until the provided predicate (function) is true. |
52 | 52 |
53 Returns: | 53 Returns: |
54 Whether the provided predicate was satisfied once (before the timeout). | 54 Whether the provided predicate was satisfied once (before the timeout). |
55 """ | 55 """ |
56 sleep_time_sec = 0.025 | 56 sleep_time_sec = 0.025 |
57 for attempt in xrange(1, max_attempts): | 57 for _ in xrange(1, max_attempts): |
58 if predicate(): | 58 if predicate(): |
59 return True | 59 return True |
60 time.sleep(sleep_time_sec) | 60 time.sleep(sleep_time_sec) |
61 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. |
62 return False | 62 return False |
63 | 63 |
64 | 64 |
65 def _CheckPortStatus(port, expected_status): | 65 def _CheckPortStatus(port, expected_status): |
66 """Returns True if port has expected_status. | 66 """Returns True if port has expected_status. |
67 | 67 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 self.tool = tool | 119 self.tool = tool |
120 self.test_server_process = None | 120 self.test_server_process = None |
121 self.is_ready = False | 121 self.is_ready = False |
122 self.host_port = self.arguments['port'] | 122 self.host_port = self.arguments['port'] |
123 assert isinstance(self.host_port, int) | 123 assert isinstance(self.host_port, int) |
124 # The forwarder device port now is dynamically allocated. | 124 # The forwarder device port now is dynamically allocated. |
125 self.forwarder_device_port = 0 | 125 self.forwarder_device_port = 0 |
126 # Anonymous pipe in order to get port info from test server. | 126 # Anonymous pipe in order to get port info from test server. |
127 self.pipe_in = None | 127 self.pipe_in = None |
128 self.pipe_out = None | 128 self.pipe_out = None |
| 129 self.process = None |
129 self.command_line = [] | 130 self.command_line = [] |
130 | 131 |
131 def _WaitToStartAndGetPortFromTestServer(self): | 132 def _WaitToStartAndGetPortFromTestServer(self): |
132 """Waits for the Python test server to start and gets the port it is using. | 133 """Waits for the Python test server to start and gets the port it is using. |
133 | 134 |
134 The port information is passed by the Python test server with a pipe given | 135 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|. | 136 by self.pipe_out. It is written as a result to |self.host_port|. |
136 | 137 |
137 Returns: | 138 Returns: |
138 Whether the port used by the test server was successfully fetched. | 139 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 | 428 |
428 def CleanupState(self): | 429 def CleanupState(self): |
429 """Cleans up the spawning server state. | 430 """Cleans up the spawning server state. |
430 | 431 |
431 This should be called if the test server spawner is reused, | 432 This should be called if the test server spawner is reused, |
432 to avoid sharing the test server instance. | 433 to avoid sharing the test server instance. |
433 """ | 434 """ |
434 if self.server.test_server_instance: | 435 if self.server.test_server_instance: |
435 self.server.test_server_instance.Stop() | 436 self.server.test_server_instance.Stop() |
436 self.server.test_server_instance = None | 437 self.server.test_server_instance = None |
OLD | NEW |