diff --git a/asl_articles/tests/__init__.py b/asl_articles/tests/__init__.py index e69de29..03a6d7b 100644 --- a/asl_articles/tests/__init__.py +++ b/asl_articles/tests/__init__.py @@ -0,0 +1,3 @@ +""" Module definitions. """ + +pytest_options = None diff --git a/asl_articles/tests/test_startup.py b/asl_articles/tests/test_startup.py index 1ea21dc..dbe61c0 100644 --- a/asl_articles/tests/test_startup.py +++ b/asl_articles/tests/test_startup.py @@ -1,11 +1,15 @@ """ Test the startup process. """ +import pytest + import asl_articles.startup from asl_articles.tests.utils import init_tests, wait_for, find_child, set_toast_marker, check_toast +from asl_articles.tests import pytest_options # --------------------------------------------------------------------- +@pytest.mark.skipif( pytest_options.flask_url is not None, reason="Testing against a remote Flask server." ) def test_startup_messages( webdriver, flask_app, dbconn ): """Test startup messages.""" diff --git a/conftest.py b/conftest.py index c660209..dbcc54c 100644 --- a/conftest.py +++ b/conftest.py @@ -17,10 +17,12 @@ import alembic.config import asl_articles from asl_articles import app from asl_articles.utils import to_bool -from asl_articles.tests import utils +from asl_articles import tests as asl_articles_tests _FLASK_SERVER_URL = ( "localhost", 5001 ) # nb: for the test Flask server we spin up +_pytest_options = None + # --------------------------------------------------------------------- def pytest_addoption( parser ): @@ -60,6 +62,15 @@ def pytest_addoption( parser ): help="Database connection string." ) +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +def pytest_configure( config ): + """Called after command-line options have been parsed.""" + global _pytest_options + _pytest_options = config.option + # notify the test suite about the pytest options + asl_articles_tests.pytest_options = _pytest_options + # --------------------------------------------------------------------- @pytest.fixture( scope="session" ) @@ -106,7 +117,7 @@ def flask_app( request ): return False except Exception as ex: #pylint: disable=broad-except assert False, "Unexpected exception: {}".format( ex ) - utils.wait_for( 5, is_ready ) + asl_articles_tests.utils.wait_for( 5, is_ready ) # return the server to the caller try: diff --git a/requirements.txt b/requirements.txt index e4fa01b..cc6bb4e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ psycopg2-binary==2.8.6 alembic==1.4.3 pyyaml==5.3.1 lxml==4.6.2 +waitress==2.0.0 diff --git a/run_server.py b/run_server.py index 3087bd4..62e78fd 100755 --- a/run_server.py +++ b/run_server.py @@ -28,6 +28,9 @@ for fspec in ["config","static","templates"] : # initialize from asl_articles import app +flask_host = app.config.get( "FLASK_HOST", "localhost" ) +flask_port = app.config.get( "FLASK_PORT_NO", 5000 ) +flask_debug = app.config.get( "FLASK_DEBUG", False ) # FUDGE! Startup can take some time (e.g. because we have to build the search index over a large database), # and since we do that on first request, it's annoying to have started the server up, if we don't do that @@ -37,15 +40,13 @@ from asl_articles import app def _force_init(): time.sleep( 5 ) try: - # figoure out the URL for the request we're going to make + # figure out the URL for the request we're going to make with app.test_request_context() as req: url = url_for( "ping" ) host = req.request.host_url - # FUDGE! There doesn't seem to be a way to get the port number Flask is listening on :-/ - port = app.config.get( "FLASK_PORT_NO", 5000 ) if host.endswith( "/" ): host = host[:-1] - url = "{}:{}{}".format( host, port, url ) + url = "{}:{}{}".format( host, flask_port, url ) # make the request _ = urllib.request.urlopen( url ).read() except Exception as ex: #pylint: disable=broad-except @@ -53,9 +54,20 @@ def _force_init(): threading.Thread( target=_force_init ).start() # run the server -app.run( - host = app.config.get( "FLASK_HOST", "localhost" ), - port = app.config.get( "FLASK_PORT_NO" ), - debug = app.config.get( "FLASK_DEBUG", False ), - extra_files = extra_files -) +if flask_debug: + # NOTE: It's useful to run the webapp using the Flask development server, since it will + # automatically reload itself when the source files change. + app.run( + host=flask_host, port=flask_port, + debug=flask_debug, + extra_files=extra_files + ) +else: + import waitress + # FUDGE! Browsers tend to send a max. of 6-8 concurrent requests per server, so we increase + # the number of worker threads to avoid task queue warnings :-/ + nthreads = app.config.get( "WAITRESS_THREADS", 8 ) + waitress.serve( app, + host=flask_host, port=flask_port, + threads=nthreads + )