17
17
"""Pytest configuration."""
18
18
import os
19
19
import threading
20
+ import time
21
+ import logging
20
22
import pytest
23
+ from requests .exceptions import HTTPError
24
+ from tests .lib .query_handler import GraphQLQueryHandler
21
25
from eiffel_graphql_api .graphql .api import APP
22
26
from eiffel_graphql_api .graphql .db .database import get_database
23
27
from eiffel_graphql_api .graphql .db .database import get_client
24
28
25
29
30
+ logging .basicConfig (level = logging .DEBUG )
31
+ LOGGER = logging .getLogger (__name__ )
32
+
33
+
26
34
@pytest .fixture
27
35
def mock_mongo ():
28
36
"""Inject a MongoDB client connected to a mock server.
@@ -39,6 +47,26 @@ def start():
39
47
APP .run ("127.0.0.1" , 12345 )
40
48
41
49
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
+
42
70
@pytest .fixture (scope = "session" , autouse = True )
43
71
def start_server (
44
72
request ,
@@ -48,6 +76,7 @@ def start_server(
48
76
thread = threading .Thread (target = start )
49
77
thread .daemon = True
50
78
thread .start ()
79
+ wait_for_webserver_connection ()
51
80
client .drop_database (os .getenv ("MONGODB_DATABASE" ))
52
81
53
82
def start_server_fin (): # pylint:disable=unused-variable
0 commit comments