Skip to content

Commit 7be6b21

Browse files
authored
Fix problem with tests failing sometimes (#56)
1 parent 5bffbbd commit 7be6b21

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
1717
"""Pytest configuration."""
1818
import os
1919
import threading
20+
import time
21+
import logging
2022
import pytest
23+
from requests.exceptions import HTTPError
24+
from tests.lib.query_handler import GraphQLQueryHandler
2125
from eiffel_graphql_api.graphql.api import APP
2226
from eiffel_graphql_api.graphql.db.database import get_database
2327
from eiffel_graphql_api.graphql.db.database import get_client
2428

2529

30+
logging.basicConfig(level=logging.DEBUG)
31+
LOGGER = logging.getLogger(__name__)
32+
33+
2634
@pytest.fixture
2735
def mock_mongo():
2836
"""Inject a MongoDB client connected to a mock server.
@@ -39,6 +47,26 @@ def start():
3947
APP.run("127.0.0.1", 12345)
4048

4149

50+
def wait_for_webserver_connection():
51+
"""Wait for the webserver to respond to HTTP requests."""
52+
LOGGER.info("Testing for webserver connectivity.")
53+
query_handler = GraphQLQueryHandler("http://127.0.0.1:12345/graphql")
54+
timeout = time.time() + 30
55+
while time.time() < timeout:
56+
try:
57+
query_handler.execute("{nothing}")
58+
LOGGER.info("Up and running")
59+
return
60+
except HTTPError: # BadRequest means the webserver came up.
61+
LOGGER.info("Up and running")
62+
return
63+
except Exception as exception: # pylint: disable=broad-except
64+
LOGGER.error("%r", exception)
65+
LOGGER.warning("Connection could not be established. Trying again in 1s..")
66+
time.sleep(1)
67+
raise TimeoutError("Timeout waiting for the webserver to start.")
68+
69+
4270
@pytest.fixture(scope="session", autouse=True)
4371
def start_server(
4472
request,
@@ -48,6 +76,7 @@ def start_server(
4876
thread = threading.Thread(target=start)
4977
thread.daemon = True
5078
thread.start()
79+
wait_for_webserver_connection()
5180
client.drop_database(os.getenv("MONGODB_DATABASE"))
5281

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

0 commit comments

Comments
 (0)