How to use _server_thread method in autotest

Best Python code snippet using autotest_python

_background_server.py

Source:_background_server.py Github

copy

Full Screen

1# Copyright 2018 Google LLC2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tornado server running in a background thread."""15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18import threading19import portpicker20import tornado21import tornado.httpserver22import tornado.ioloop23import tornado.web24class _BackgroundServer(object):25 """HTTP server that runs in a background thread."""26 def __init__(self, app):27 """Initialize (but do not start) background server.28 Args:29 app: server application to run.30 """31 self._app = app32 # These will be initialized when starting the server.33 self._port = None34 self._server_thread = None35 self._ioloop = None36 self._server = None37 @property38 def port(self):39 """Returns the current port or error if the server is not started.40 Raises:41 RuntimeError: If server has not been started yet.42 Returns:43 The port being used by the server.44 """45 if self._server_thread is None:46 raise RuntimeError('Server not running.')47 return self._port48 def stop(self):49 """Stops the server thread.50 Raises:51 RuntimeError: if server is already stopped.52 """53 if self._server_thread is None:54 raise RuntimeError('stop() called on stopped server')55 def shutdown():56 self._server.stop()57 self._ioloop.stop()58 try:59 self._ioloop.add_callback(shutdown)60 self._server_thread.join()61 self._ioloop.close(all_fds=True)62 finally:63 self._server_thread = None64 def start(self, port=None, timeout=1):65 """Starts a server in a thread using the WSGI application provided.66 Will wait until the thread has started calling with an already serving67 application will simple return.68 Args:69 port: Number of the port to use for the application, will find an open70 port if a nonzero port is not provided.71 timeout: Http timeout in seconds. Note that this is only respected under72 tornado v4.73 Raises:74 RuntimeError: if server is already started.75 """76 if self._server_thread is not None:77 raise RuntimeError('start() called on running background server.')78 self._port = port or portpicker.pick_unused_port()79 # Support both internal & external colab (tornado v3 vs. v4)80 # TODO(b/35548011): remove tornado v3 handling81 if tornado.version[0] >= '4':82 kwds = {'idle_connection_timeout': timeout, 'body_timeout': timeout}83 else:84 kwds = {}85 self._server = tornado.httpserver.HTTPServer(self._app, **kwds)86 self._ioloop = tornado.ioloop.IOLoop()87 def start_server(httpd, ioloop, port):88 # TODO(b/147233568): Restrict this to local connections.89 host = '' # Bind to all90 ioloop.make_current()91 httpd.listen(port=port, address=host)92 ioloop.start()93 self._server_thread = threading.Thread(94 target=start_server,95 kwargs={96 'httpd': self._server,97 'ioloop': self._ioloop,98 'port': self._port99 })100 started = threading.Event()101 self._ioloop.add_callback(started.set)102 self._server_thread.start()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful