Skip to content

Fix problem with tests failing sometimes #56

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

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Changes from all 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
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
"""Pytest configuration."""
import os
import threading
import time
import logging
import pytest
from requests.exceptions import HTTPError
from tests.lib.query_handler import GraphQLQueryHandler
from eiffel_graphql_api.graphql.api import APP
from eiffel_graphql_api.graphql.db.database import get_database
from eiffel_graphql_api.graphql.db.database import get_client


logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger(__name__)


@pytest.fixture
def mock_mongo():
"""Inject a MongoDB client connected to a mock server.
Expand All @@ -39,6 +47,26 @@ def start():
APP.run("127.0.0.1", 12345)


def wait_for_webserver_connection():
"""Wait for the webserver to respond to HTTP requests."""
LOGGER.info("Testing for webserver connectivity.")
query_handler = GraphQLQueryHandler("http://127.0.0.1:12345/graphql")
timeout = time.time() + 30
while time.time() < timeout:
try:
query_handler.execute("{nothing}")
LOGGER.info("Up and running")
return
except HTTPError: # BadRequest means the webserver came up.
LOGGER.info("Up and running")
return
except Exception as exception: # pylint: disable=broad-except
LOGGER.error("%r", exception)
LOGGER.warning("Connection could not be established. Trying again in 1s..")
time.sleep(1)
raise TimeoutError("Timeout waiting for the webserver to start.")
Comment on lines +50 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the retry package instead?

@retry.retry(exceptions=Exception, tries=30, delay=1, logger=LOGGER)
def wait_for_webserver_connection():
    query_handler = GraphQLQueryHandler("http://127.0.0.1:12345/graphql")
    try:
        query_handler.execute("{nothing}")
    except HTTPError as err:
        if err.response.status_code != 400:
            raise

It won't raise TimeoutError if the startup fails though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the only place in the entire code-base where we do this kind of retry, I would say that the additional "overhead" of installing the retry package feels unnecessary.

If we were to add more retries, then definitely.



@pytest.fixture(scope="session", autouse=True)
def start_server(
request,
Expand All @@ -48,6 +76,7 @@ def start_server(
thread = threading.Thread(target=start)
thread.daemon = True
thread.start()
wait_for_webserver_connection()
client.drop_database(os.getenv("MONGODB_DATABASE"))

def start_server_fin(): # pylint:disable=unused-variable
Expand Down