Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing improvements. #2036

Merged
merged 8 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions dash/testing/application_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,20 @@ def __init__(self, keep_open=False, stop_timeout=3):
super().__init__(keep_open=keep_open, stop_timeout=stop_timeout)
self.thread = None

def running_and_accessible(self, url):
if self.thread.is_alive():
return self.accessible(url)
raise DashAppLoadingError("Thread is not alive.")

# pylint: disable=arguments-differ
def start(self, app, **kwargs):
def start(self, app, start_timeout=3, **kwargs):
"""Start the app server in threading flavor."""

def _handle_error():
self.stop()

app.server.errorhandler(500)(_handle_error)

if self.thread and self.thread.is_alive():
self.stop()

def run():
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
Expand All @@ -170,16 +172,24 @@ def run():
app.run(threaded=True, **options)
except SystemExit:
logger.info("Server stopped")
except Exception as error:
logger.exception(error)
raise error

retries = 0

while not self.started and retries < 3:
try:
if self.thread and self.thread.is_alive():
self.stop()

self.thread = KillerThread(target=run)
self.thread.daemon = True
self.thread.start()
# wait until server is able to answer http request
wait.until(lambda: self.accessible(self.url), timeout=2)
wait.until(
lambda: self.running_and_accessible(self.url), timeout=start_timeout
)
self.started = self.thread.is_alive()
except Exception as err: # pylint: disable=broad-except
logger.exception(err)
Expand Down
16 changes: 8 additions & 8 deletions dash/testing/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,33 @@ def pytest_runtest_makereport(item, call): # pylint: disable=unused-argument


@pytest.fixture
def dash_thread_server():
def dash_thread_server() -> ThreadedRunner:
"""Start a local dash server in a new thread."""
with ThreadedRunner() as starter:
yield starter


@pytest.fixture
def dash_process_server():
def dash_process_server() -> ProcessRunner:
"""Start a Dash server with subprocess.Popen and waitress-serve."""
with ProcessRunner() as starter:
yield starter


@pytest.fixture
def dashr_server():
def dashr_server() -> RRunner:
with RRunner() as starter:
yield starter


@pytest.fixture
def dashjl_server():
def dashjl_server() -> JuliaRunner:
with JuliaRunner() as starter:
yield starter


@pytest.fixture
def dash_br(request, tmpdir):
def dash_br(request, tmpdir) -> Browser:
with Browser(
browser=request.config.getoption("webdriver"),
remote=request.config.getoption("remote"),
Expand All @@ -137,7 +137,7 @@ def dash_br(request, tmpdir):


@pytest.fixture
def dash_duo(request, dash_thread_server, tmpdir):
def dash_duo(request, dash_thread_server, tmpdir) -> DashComposite:
with DashComposite(
dash_thread_server,
browser=request.config.getoption("webdriver"),
Expand All @@ -154,7 +154,7 @@ def dash_duo(request, dash_thread_server, tmpdir):


@pytest.fixture
def dashr(request, dashr_server, tmpdir):
def dashr(request, dashr_server, tmpdir) -> DashRComposite:
with DashRComposite(
dashr_server,
browser=request.config.getoption("webdriver"),
Expand All @@ -171,7 +171,7 @@ def dashr(request, dashr_server, tmpdir):


@pytest.fixture
def dashjl(request, dashjl_server, tmpdir):
def dashjl(request, dashjl_server, tmpdir) -> DashJuliaComposite:
with DashJuliaComposite(
dashjl_server,
browser=request.config.getoption("webdriver"),
Expand Down