Skip to content

Commit 952a3d2

Browse files
committed
fix conftest waits and travis config [chapter_04_service_layer_ends]
1 parent b537364 commit 952a3d2

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: python
33
python: 3.8
44

55
install:
6-
- pip3 install sqlalchemy
6+
- pip3 install -r requirements.txt
77

88
script:
9-
- make test
9+
- make all
1010

1111
branches:
1212
except:

conftest.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from pathlib import Path
44

55
import pytest
6+
import requests
7+
from requests.exceptions import ConnectionError
8+
from sqlalchemy.exc import OperationalError
69
from sqlalchemy import create_engine
710
from sqlalchemy.orm import sessionmaker, clear_mappers
811

912
from orm import metadata, start_mappers
10-
from config import get_postgres_uri
13+
import config
1114

1215

1316
@pytest.fixture
@@ -24,9 +27,31 @@ def session(in_memory_db):
2427
clear_mappers()
2528

2629

30+
def wait_for_postgres_to_come_up(engine):
31+
deadline = time.time() + 10
32+
while time.time() < deadline:
33+
try:
34+
return engine.connect()
35+
except OperationalError:
36+
time.sleep(0.5)
37+
pytest.fail("Postgres never came up")
38+
39+
40+
def wait_for_webapp_to_come_up():
41+
deadline = time.time() + 10
42+
url = config.get_api_url()
43+
while time.time() < deadline:
44+
try:
45+
return requests.get(url)
46+
except ConnectionError:
47+
time.sleep(0.5)
48+
pytest.fail("API never came up")
49+
50+
2751
@pytest.fixture(scope="session")
2852
def postgres_db():
29-
engine = create_engine(get_postgres_uri())
53+
engine = create_engine(config.get_postgres_uri())
54+
wait_for_postgres_to_come_up(engine)
3055
metadata.create_all(engine)
3156
return engine
3257

@@ -78,4 +103,5 @@ def _add_stock(lines):
78103
@pytest.fixture
79104
def restart_api():
80105
(Path(__file__).parent / "flask_app.py").touch()
81-
time.sleep(0.3)
106+
time.sleep(0.5)
107+
wait_for_webapp_to_come_up()

0 commit comments

Comments
 (0)