Skip to content

Commit

Permalink
fix conftest waits and travis config [chapter_04_service_layer_ends]
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Feb 24, 2021
1 parent b537364 commit 952a3d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ language: python
python: 3.8

install:
- pip3 install sqlalchemy
- pip3 install -r requirements.txt

script:
- make test
- make all

branches:
except:
Expand Down
32 changes: 29 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from pathlib import Path

import pytest
import requests
from requests.exceptions import ConnectionError
from sqlalchemy.exc import OperationalError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, clear_mappers

from orm import metadata, start_mappers
from config import get_postgres_uri
import config


@pytest.fixture
Expand All @@ -24,9 +27,31 @@ def session(in_memory_db):
clear_mappers()


def wait_for_postgres_to_come_up(engine):
deadline = time.time() + 10
while time.time() < deadline:
try:
return engine.connect()
except OperationalError:
time.sleep(0.5)
pytest.fail("Postgres never came up")


def wait_for_webapp_to_come_up():
deadline = time.time() + 10
url = config.get_api_url()
while time.time() < deadline:
try:
return requests.get(url)
except ConnectionError:
time.sleep(0.5)
pytest.fail("API never came up")


@pytest.fixture(scope="session")
def postgres_db():
engine = create_engine(get_postgres_uri())
engine = create_engine(config.get_postgres_uri())
wait_for_postgres_to_come_up(engine)
metadata.create_all(engine)
return engine

Expand Down Expand Up @@ -78,4 +103,5 @@ def _add_stock(lines):
@pytest.fixture
def restart_api():
(Path(__file__).parent / "flask_app.py").touch()
time.sleep(0.3)
time.sleep(0.5)
wait_for_webapp_to_come_up()

0 comments on commit 952a3d2

Please sign in to comment.