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 """Base class for running tests on a single device.""" | 5 """Base class for running tests on a single device.""" |
6 | 6 |
7 import contextlib | 7 import contextlib |
8 import httplib | 8 import httplib |
9 import logging | 9 import logging |
10 import os | 10 import os |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 self._http_server = lighttpd_server.LighttpdServer( | 118 self._http_server = lighttpd_server.LighttpdServer( |
119 document_root, port=port, extra_config_contents=extra_config_contents) | 119 document_root, port=port, extra_config_contents=extra_config_contents) |
120 if self._http_server.StartupHttpServer(): | 120 if self._http_server.StartupHttpServer(): |
121 logging.info('http server started: http://localhost:%s', | 121 logging.info('http server started: http://localhost:%s', |
122 self._http_server.port) | 122 self._http_server.port) |
123 else: | 123 else: |
124 logging.critical('Failed to start http server') | 124 logging.critical('Failed to start http server') |
125 self.StartForwarderForHttpServer() | 125 self.StartForwarderForHttpServer() |
126 return (self._forwarder_device_port, self._http_server.port) | 126 return (self._forwarder_device_port, self._http_server.port) |
127 | 127 |
128 def _CreateAndRunForwarder( | 128 def _ForwardPort(self, port_pairs): |
129 self, adb, port_pairs, tool, host_name, build_type): | 129 """Creates a forwarder instance if needed and forward a port.""" |
130 """Creates and run a forwarder.""" | 130 if not self._forwarder: |
131 forwarder = Forwarder(adb, build_type) | 131 self._forwarder = Forwarder(self.adb, self.build_type) |
132 forwarder.Run(port_pairs, tool, host_name) | 132 self._forwarder.Run(port_pairs, self.tool, '127.0.0.1') |
133 return forwarder | |
134 | 133 |
135 def StartForwarder(self, port_pairs): | 134 def StartForwarder(self, port_pairs): |
136 """Starts TCP traffic forwarding for the given |port_pairs|. | 135 """Starts TCP traffic forwarding for the given |port_pairs|. |
137 | 136 |
138 Args: | 137 Args: |
139 host_port_pairs: A list of (device_port, local_port) tuples to forward. | 138 host_port_pairs: A list of (device_port, local_port) tuples to forward. |
140 """ | 139 """ |
141 if self._forwarder: | 140 self._ForwardPort(port_pairs) |
142 self._forwarder.Close() | |
143 self._forwarder = self._CreateAndRunForwarder( | |
144 self.adb, port_pairs, self.tool, '127.0.0.1', self.build_type) | |
145 | 141 |
146 def StartForwarderForHttpServer(self): | 142 def StartForwarderForHttpServer(self): |
147 """Starts a forwarder for the HTTP server. | 143 """Starts a forwarder for the HTTP server. |
148 | 144 |
149 The forwarder forwards HTTP requests and responses between host and device. | 145 The forwarder forwards HTTP requests and responses between host and device. |
150 """ | 146 """ |
151 self.StartForwarder([(self._forwarder_device_port, self._http_server.port)]) | 147 self._ForwardPort([(self._forwarder_device_port, self._http_server.port)]) |
152 | 148 |
153 def RestartHttpServerForwarderIfNecessary(self): | 149 def RestartHttpServerForwarderIfNecessary(self): |
154 """Restarts the forwarder if it's not open.""" | 150 """Restarts the forwarder if it's not open.""" |
155 # Checks to see if the http server port is being used. If not forwards the | 151 # Checks to see if the http server port is being used. If not forwards the |
156 # request. | 152 # request. |
157 # TODO(dtrainor): This is not always reliable because sometimes the port | 153 # TODO(dtrainor): This is not always reliable because sometimes the port |
158 # will be left open even after the forwarder has been killed. | 154 # will be left open even after the forwarder has been killed. |
159 if not ports.IsDevicePortUsed(self.adb, | 155 if not ports.IsDevicePortUsed(self.adb, |
160 self._forwarder_device_port): | 156 self._forwarder_device_port): |
161 self.StartForwarderForHttpServer() | 157 self.StartForwarderForHttpServer() |
(...skipping 18 matching lines...) Expand all Loading... |
180 If the spawning server is reused for multiple tests, this should be called | 176 If the spawning server is reused for multiple tests, this should be called |
181 after each test to prevent tests affecting each other. | 177 after each test to prevent tests affecting each other. |
182 """ | 178 """ |
183 if self._spawning_server: | 179 if self._spawning_server: |
184 self._spawning_server.CleanupState() | 180 self._spawning_server.CleanupState() |
185 | 181 |
186 def LaunchChromeTestServerSpawner(self): | 182 def LaunchChromeTestServerSpawner(self): |
187 """Launches test server spawner.""" | 183 """Launches test server spawner.""" |
188 server_ready = False | 184 server_ready = False |
189 error_msgs = [] | 185 error_msgs = [] |
| 186 # TODO(pliard): deflake this function. The for loop should be removed as |
| 187 # well as IsHttpServerConnectable(). spawning_server.Start() should also |
| 188 # block until the server is ready. |
190 # Try 3 times to launch test spawner server. | 189 # Try 3 times to launch test spawner server. |
191 for i in xrange(0, 3): | 190 for i in xrange(0, 3): |
192 # Do not allocate port for test server here. We will allocate | |
193 # different port for individual test in TestServerThread. | |
194 self.test_server_spawner_port = ports.AllocateTestServerPort() | 191 self.test_server_spawner_port = ports.AllocateTestServerPort() |
| 192 self._ForwardPort( |
| 193 [(self.test_server_spawner_port, self.test_server_spawner_port)]) |
195 self._spawning_server = SpawningServer(self.test_server_spawner_port, | 194 self._spawning_server = SpawningServer(self.test_server_spawner_port, |
196 self.adb, | 195 self.adb, |
197 self.tool, | 196 self.tool, |
| 197 self._forwarder, |
198 self.build_type) | 198 self.build_type) |
199 self._spawning_server.Start() | 199 self._spawning_server.Start() |
200 server_ready, error_msg = ports.IsHttpServerConnectable( | 200 server_ready, error_msg = ports.IsHttpServerConnectable( |
201 '127.0.0.1', self.test_server_spawner_port, path='/ping', | 201 '127.0.0.1', self.test_server_spawner_port, path='/ping', |
202 expected_read='ready') | 202 expected_read='ready') |
203 if server_ready: | 203 if server_ready: |
204 break | 204 break |
205 else: | 205 else: |
206 error_msgs.append(error_msg) | 206 error_msgs.append(error_msg) |
207 self._spawning_server.Stop() | 207 self._spawning_server.Stop() |
208 # Wait for 2 seconds then restart. | 208 # Wait for 2 seconds then restart. |
209 time.sleep(2) | 209 time.sleep(2) |
210 if not server_ready: | 210 if not server_ready: |
211 logging.error(';'.join(error_msgs)) | 211 logging.error(';'.join(error_msgs)) |
212 raise Exception('Can not start the test spawner server.') | 212 raise Exception('Can not start the test spawner server.') |
213 self._PushTestServerPortInfoToDevice() | 213 self._PushTestServerPortInfoToDevice() |
214 self._spawner_forwarder = self._CreateAndRunForwarder( | |
215 self.adb, | |
216 [(self.test_server_spawner_port, self.test_server_spawner_port)], | |
217 self.tool, '127.0.0.1', self.build_type) | |
OLD | NEW |